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

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

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>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

번호 제목 글쓴이 날짜 조회 수
60 [ChatGPT] javax.* 패키지와 jakarta.* 패키지의 혼합 사용 방안 황제낙엽 2025.04.15 582
59 [ChatGPT] *.servlet.http.HttpServlet 클래스를 사용할 수 있는 주요 WAS 목록 황제낙엽 2025.04.14 534
58 jakarta.servlet 패키지를 이용한 File Upload 구현 (2024.03.03, Servlet 5.0) 황제낙엽 2025.01.02 676
57 [ChatGPT] Java EE가 Jakarta EE로 전환 황제낙엽 2025.01.01 817
56 [ChatGPT] <jsp:include>에서 flush="true"를 설정시의 몇 가지 문제점 과 대안 황제낙엽 2024.07.19 794
55 [Copilot] JSP, Servlet, web.xml, mysql 한글 설정 황제낙엽 2024.07.12 673
54 Base64 클래스의 encode, decode 에서 URL-safe 황제낙엽 2024.07.10 906
53 [Copilot] JSP 에서 다른 페이지를 현재 페이지에 포함시키는 문법 (include) 황제낙엽 2024.06.26 746
52 servlet name 에 camel case 를 사용해서는 안되는 이유 황제낙엽 2023.10.24 734
51 org.apache.commons.fileupload.servlet.ServletFileUpload 이용 예시 황제낙엽 2023.08.25 1165
50 ServletFileUpload.parseRequest() 실행 결과 리스트가 비어있는 오류 황제낙엽 2023.08.25 1237
49 어떤 multi-part 설정도 제공되지 않았기 때문에, part들을 처리할 수 없습니다. file 황제낙엽 2023.08.23 1518
48 (Bard) the request was rejected because no multipart boundary was found file 황제낙엽 2023.08.23 749
47 (Bard) multipart/form-data 요청 처리를 위한 MultipartHttpServletRequest file 황제낙엽 2023.08.21 608
46 ajax 로 post 데이터를 servlet 으로 전달 받기 (with nexacro) [1] secret 황제낙엽 2023.02.26 0
» [JSP] 파일 다운로드 테스트 file 황제낙엽 2021.04.12 4280
44 [HttpURLConnection] Authorization 헤더를 넣어 GET Request 황제낙엽 2020.08.12 1778
43 [AWS, 웹 프로젝트] AWS+MySQL+SpringFrameWork5+JAVA8+ React+Gradle+Webpack+GIT+Jenkins file 황제낙엽 2020.04.08 990
42 세션의 timeout 설정 >> HttpSession.setMaxInactiveInterval() 황제낙엽 2019.07.03 8936
41 [HttpURLConnection] 서버와의 통신 시도 시점 관련 황제낙엽 2019.06.23 896