카테고리 없음 자바 zip 파일 다운로드 건강코더 2019. 4. 9. 09:35 써블릿에서 zip파일 만들어 다운로드 하기 package org.kodejava.example.servlet; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import <a href="http://javax.servlet.http">javax.servlet.http</a>.HttpServlet; import <a href="http://javax.servlet.http">javax.servlet.http</a>.HttpServletRequest; import <a href="http://javax.servlet.http">javax.servlet.http</a>.HttpServletResponse; import <a href="http://java.io">java.io</a>.*; import <a href="http://java.util.zip">java.util.zip</a>.ZipEntry; import <a href="http://java.util.zip">java.util.zip</a>.ZipOutputStream; public class ZipDownloadServlet extends HttpServlet { public static final String FILE_SEPARATOR = System.getProperty("file.separator"); protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { // The path below is the root directory of data to be // compressed. String path = getServletContext().getRealPath("data"); File directory = new File(path); String[] files = <a href="http://directory.list">directory.list</a>(); // Checks to see if the directory contains some files. if (files != null && <a href="http://files.length">files.length</a> > 0) { // Call the zipFiles method for creating a zip stream. byte[] zip = zipFiles(directory, files); // Sends the response back to the user / browser. The // content for zip file type is "application/zip". We // also set the content disposition as attachment for // the browser to show a dialog that will let user // choose what action will he do to the sent content. ServletOutputStream sos = response.getOutputStream(); response.setContentType("application/zip"); response.setHeader("Content-Disposition", "attachment; filename="DATA.ZIP""); <a href="http://sos.write">sos.write</a>(zip); <a href="http://sos.flush">sos.flush</a>(); } } catch (Exception e) { e.printStackTrace(); } } /** * Compress the given directory with all its files. */ private byte[] zipFiles(File directory, String[] files) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(baos); byte bytes[] = new byte[2048]; for (String fileName : files) { FileInputStream fis = new FileInputStream(directory.getPath() + ZipDownloadServlet.FILE_SEPARATOR + fileName); BufferedInputStream bis = new BufferedInputStream(fis); zos.putNextEntry(new ZipEntry(fileName)); int bytesRead; while ((bytesRead = <a href="http://bis.read">bis.read</a>(bytes)) != -1) { <a href="http://zos.write">zos.write</a>(bytes, 0, bytesRead); } zos.closeEntry(); <a href="http://bis.close">bis.close</a>(); <a href="http://fis.close">fis.close</a>(); } <a href="http://zos.flush">zos.flush</a>(); <a href="http://baos.flush">baos.flush</a>(); <a href="http://zos.close">zos.close</a>(); <a href="http://baos.close">baos.close</a>(); return baos.toByteArray(); } } web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee <a href="http://java.sun.com">http://java.sun.com</a>/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <servlet> <servlet-name>ZipDownloadServlet</servlet-name> <servlet-class>org.kodejava.example.servlet.ZipDownloadServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ZipDownloadServlet</servlet-name> <url-pattern>/zipservlet</url-pattern> </servlet-mapping> </web-app> 출처 : https://kodejava.org/how-do-i-create-zip-file-in-servlet-for-download/ 공유하기 게시글 관리 건강한 코딩 저작자표시 비영리 변경금지