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

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

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>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

번호 제목 글쓴이 날짜 조회 수
63 [ChatGPT] javax.* 패키지와 jakarta.* 패키지의 혼합 사용 방안 황제낙엽 2025.04.15 27
62 [ChatGPT] *.servlet.http.HttpServlet 클래스를 사용할 수 있는 주요 WAS 목록 황제낙엽 2025.04.14 36
61 jakarta.servlet 패키지를 이용한 File Upload 구현 (2024.03.03, Servlet 5.0) 황제낙엽 2025.01.02 177
60 [ChatGPT] Java EE가 Jakarta EE로 전환 황제낙엽 2025.01.01 239
59 [ChatGPT] <jsp:include>에서 flush="true"를 설정시의 몇 가지 문제점 과 대안 황제낙엽 2024.07.19 208
58 [Copilot] JSP, Servlet, web.xml, mysql 한글 설정 황제낙엽 2024.07.12 179
57 Base64 클래스의 encode, decode 에서 URL-safe 황제낙엽 2024.07.10 273
56 [Copilot] JSP 에서 다른 페이지를 현재 페이지에 포함시키는 문법 (include) 황제낙엽 2024.06.26 224
55 [Gemini] HttpURLConnection 클래스를 이용한 데이터 전송 방식 비교 황제낙엽 2024.03.14 556
54 servlet name 에 camel case 를 사용해서는 안되는 이유 황제낙엽 2023.10.24 209
53 org.apache.commons.fileupload.servlet.ServletFileUpload 이용 예시 황제낙엽 2023.08.25 503
52 ServletFileUpload.parseRequest() 실행 결과 리스트가 비어있는 오류 황제낙엽 2023.08.25 659
51 어떤 multi-part 설정도 제공되지 않았기 때문에, part들을 처리할 수 없습니다. file 황제낙엽 2023.08.23 891
50 (Bard) the request was rejected because no multipart boundary was found file 황제낙엽 2023.08.23 337
49 (Bard) multipart/form-data 요청 처리를 위한 MultipartHttpServletRequest file 황제낙엽 2023.08.21 210
48 ajax 로 post 데이터를 servlet 으로 전달 받기 (with nexacro) [1] secret 황제낙엽 2023.02.26 0
47 HttpServletRequest, HttpServletResponse, JSONObject, POST 황제낙엽 2022.01.12 389
» [JSP] 파일 다운로드 테스트 file 황제낙엽 2021.04.12 3560
45 [HttpURLConnection] Authorization 헤더를 넣어 GET Request 황제낙엽 2020.08.12 1062
44 [AWS, 웹 프로젝트] AWS+MySQL+SpringFrameWork5+JAVA8+ React+Gradle+Webpack+GIT+Jenkins file 황제낙엽 2020.04.08 553