WebApp [JSP] 파일 다운로드 테스트

황제낙엽 2021.04.12 15:01 조회 수 : 123

sitelink1  
sitelink2  
sitelink3  
sitelink4  
sitelink5  
sitelink6  

샘플 다운로드 -> 91975.7z

 

[[CODE]]

 

<%@ page import="java.io.File" %>

<%@ page import="java.io.*"%>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <title>파일 다운로드 테스트</title>

    </head>

    <body>

 

<%

    String fileName = request.getParameter("fileName");

    fileName = "/TOPS_RP/91975/ExcelExport_Sample.xlsx";

 

    ServletContext context = getServletContext();

    String realFolder = context.getRealPath("/");

    String filePath = realFolder + "\\" + fileName;

    out.println("filePath="+filePath);

 

    try{

        out.clear();

        out = pageContext.pushBody();

 

        File file = new File(filePath);

        byte b[] = new byte[4096];

 

        response.reset();

        response.setContentType("application/octet-stream");

 

        String Encoding = new String(fileName.getBytes("UTF-8"), "8859_1");

        response.setHeader("Content-Disposition", "attatchment; filename = " + Encoding);

 

        FileInputStream is = new FileInputStream(filePath);

        ServletOutputStream sos = response.getOutputStream();

 

        int numRead;

        while((numRead = is.read(b,0,b.length)) != -1){

            sos.write(b,0,numRead);

        }

 

        sos.flush();

        sos.close();

        is.close();

    }catch(Exception e){

        e.printStackTrace();

    }

%>

 

    </body>

</html>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

번호 제목 글쓴이 날짜 조회 수
55 [Gemini] HttpURLConnection 클래스를 이용한 데이터 전송 방식 비교 황제낙엽 2024.03.14 7
54 servlet name 에 camel case 를 사용해서는 안되는 이유 황제낙엽 2023.10.24 2
53 org.apache.commons.fileupload.servlet.ServletFileUpload 이용 예시 황제낙엽 2023.08.25 2
52 ServletFileUpload.parseRequest() 실행 결과 리스트가 비어있는 오류 황제낙엽 2023.08.25 18
51 어떤 multi-part 설정도 제공되지 않았기 때문에, part들을 처리할 수 없습니다. file 황제낙엽 2023.08.23 99
50 (Bard) the request was rejected because no multipart boundary was found file 황제낙엽 2023.08.23 9
49 (Bard) multipart/form-data 요청 처리를 위한 MultipartHttpServletRequest file 황제낙엽 2023.08.21 4
48 ajax 로 post 데이터를 servlet 으로 전달 받기 (with nexacro) [1] secret 황제낙엽 2023.02.26 0
47 HttpServletRequest, HttpServletResponse, JSONObject, POST 황제낙엽 2022.01.12 31
» [JSP] 파일 다운로드 테스트 file 황제낙엽 2021.04.12 123
45 [HttpURLConnection] Authorization 헤더를 넣어 GET Request 황제낙엽 2020.08.12 376
44 [AWS, 웹 프로젝트] AWS+MySQL+SpringFrameWork5+JAVA8+ React+Gradle+Webpack+GIT+Jenkins file 황제낙엽 2020.04.08 48
43 세션의 timeout 설정 >> HttpSession.setMaxInactiveInterval() 황제낙엽 2019.07.03 8311
42 jQuery JSON 데이터 통신의 특성 (HttpServletRequest) 황제낙엽 2019.06.23 103
41 [HttpURLConnection] 서버와의 통신 시도 시점 관련 황제낙엽 2019.06.23 116
40 응답 헤더의 Content-disposition 속성 황제낙엽 2019.04.16 534
39 Java에서 User-Agent 파써 사용하기 황제낙엽 2017.11.20 418
38 서버구동시 주기적으로 동작을 수행하는 스레드를 함께 실행하는 서블릿 황제낙엽 2017.08.02 131
37 JSP 파일에서 getOutputStream() has already been called for this response 에러 황제낙엽 2013.04.24 11479
36 [The type HttpUtils is deprecated] javax.servlet.http.HttpUtils 황제낙엽 2013.03.20 276