【问题标题】:Creating zip using java and make it downloadable使用 java 创建 zip 并使其可下载
【发布时间】:2020-05-15 18:14:03
【问题描述】:

我有这个程序,我试图用它来创建位于目录中的文件的 zip 文件。

程序运行但在 chrome 中下载失败,提示网络错误。

在 Mozilla 中,它说 Ut0ij4ld.ZIP.part 无法保存,因为无法读取源文件。

我做错了什么,有更好的方法吗?

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String path = "D:\\Test\\";
    File directory = new File(path);
    String[] files = directory.list();

    //check if directories have files
    if (files != null && files.length > 0) {

        //create zip stream
        byte[] zip = zipFiles(directory, files);


        // Sends the response back to the user / browser with zip content
        ServletOutputStream sos = response.getOutputStream();
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment; filename=\"DATA.ZIP\"");

        sos.write(zip);
        sos.flush();
    }

    request.setAttribute("DownloadMessage", "Successfully");
    request.getRequestDispatcher("DownloadZipFile.jsp").forward(request, response);

}

private byte[] zipFiles(File directory, String[] files) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(baos);
    byte bytes[] = new byte[4096];

    for (String fileName : files) {
        try (FileInputStream fis = new FileInputStream(directory.getPath()
                + "/" + fileName);

                BufferedInputStream bis = new BufferedInputStream(fis)) {

            zos.putNextEntry(new ZipEntry(fileName));

            int bytesRead;
            while ((bytesRead = bis.read(bytes)) != -1) {
                zos.write(bytes, 0, bytesRead);
            }
            zos.closeEntry();
        }
    }
    zos.flush();
    baos.flush();
    zos.close();
    baos.close();

    return baos.toByteArray();
}

【问题讨论】:

标签: java


【解决方案1】:

这行得通,

 @Override
protected void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    //set the content type to zip
    response.setContentType("application/zip");
    response.setHeader("Content-Disposition", "attachment; filename=\"DATA.ZIP\"");

    //to write it over http
    ServletOutputStream ouputStream = response.getOutputStream();

    //for writing files in the ZIP file format. Includes support for both compressed and uncompressed entries
    ZipOutputStream zos= new ZipOutputStream(ouputStream);

    //your file root folder
    File rootFolder= new File ("D:\\Test\\") ;

    // Looping through all the files

    for (File file: rootFolder.listFiles()){
        try {
            writeToZip(zos,file);
        } catch (Exception ex) {
            Logger.getLogger(Zipper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    zos.close();
    ouputStream.close();
}



private static void writeToZip(ZipOutputStream zos,File file) throws Exception{


    FileInputStream fis=new FileInputStream(file);
    ZipEntry zipEntry= new ZipEntry(file.getName());
    zos.putNextEntry(zipEntry);

    final byte[] bytes = new byte[1024];
    int length;
    while ((length = fis.read(bytes)) >= 0) {
        zos.write(bytes, 0, length);
    }
    zos.closeEntry();
    fis.close();

} }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    • 2014-07-01
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多