在服务器上创建的 Zip 文件并使用 java 下载该 zip

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23645746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:29:30  来源:igfitidea点击:

Zip file created on server and download that zip, using java

javazipjava-io

提问by DDphp

I have the below code got from mkyong, to zip files on local. But, my requirement is to zip files on server and need to download that. Could any one help.

我有从 mkyong 获得的以下代码,用于在本地压缩文件。但是,我的要求是在服务器上压缩文件并需要下载。任何人都可以帮忙。

code wrote to zipFiles:

代码写入 zipFiles:

public void zipFiles(File contentFile, File navFile)
{
    byte[] buffer = new byte[1024];

    try{
        // i dont have idea on what to give here in fileoutputstream
        FileOutputStream fos = new FileOutputStream("C:\MyFile.zip");
        ZipOutputStream zos = new ZipOutputStream(fos);
        ZipEntry ze= new ZipEntry(contentFile.toString());
        zos.putNextEntry(ze);
        FileInputStream in = new FileInputStream(contentFile.toString());

        int len;
        while ((len = in.read(buffer)) > 0) {
            zos.write(buffer, 0, len);
        }

        in.close();
        zos.closeEntry();

        //remember close it
        zos.close();

        System.out.println("Done");

    }catch(IOException ex){
       ex.printStackTrace();
    }
}

what could i provide in fileoutputstream here? contentfile and navigationfile are files i created from code.

我可以在 fileoutputstream 中提供什么?contentfile 和 navigationfile 是我从代码创建的文件。

采纳答案by icza

If your server is a servlet container, just write an HttpServletwhich does the zipping and serving the file.

如果您的服务器是一个 servlet 容器,只需编写一个HttpServlet压缩和提供文件的文件。

You can pass the output stream of the servlet response to the constructor of ZipOutputStreamand the zip file will be sent as the servlet response:

您可以将 servlet 响应的输出流传递给 的构造函数,ZipOutputStreamzip 文件将作为 servlet 响应发送:

ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());

Don't forget to set the response mime type before zipping, e.g.:

不要忘记在压缩之前设置响应 mime 类型,例如:

response.setContentType("application/zip");

The whole picture:

全图:

public class DownloadServlet extends HttpServlet {

    @Override
    public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment; filename=data.zip");

        // You might also wanna disable caching the response
        // here by setting other headers...

        try ( ZipOutputStream zos = new ZipOutputStream(response.getOutputStream()) ) {
            // Add zip entries you want to include in the zip file
        }
    }
}

回答by AVINASH SHRIMALI

Try this:

尝试这个:

@RequestMapping(value="download", method=RequestMethod.GET)
public void getDownload(HttpServletResponse response) {

    // Get your file stream from wherever.
    InputStream myStream = someClass.returnFile();

    // Set the content type and attachment header.
    response.addHeader("Content-disposition", "attachment;filename=myfilename.txt");
    response.setContentType("txt/plain");

    // Copy the stream to the response's output stream.
    IOUtils.copy(myStream, response.getOutputStream());
    response.flushBuffer();
}

Reference

参考