我可以在一个HttpServletResponse中附加多个附件 [英] Can i attach multiple attachments in one HttpServletResponse

查看:204
本文介绍了我可以在一个HttpServletResponse中附加多个附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我想在一个响应中下载一个zip文件和一个csv文件。除了在一个zip文件中压缩这两个文件之外,还有什么办法吗?

For example, i would like to download one zip file and one csv file in one response. Is there any way other than compressing these two files in one zip file.

推荐答案

虽然ServletResponse不是为了做到这一点,但我们可以通过编程方式调整它来发送多个文件,除了IE之外的所有客户端浏览器似乎处理得当。下面给出了示例代码段。

Although ServletResponse is not meant to do this, we could programmatically tweak it to send multiple files, which all client browsers except IE seems to handle properly. A sample code snippet is given below.

response.setContentType("multipart/x-mixed-replace;boundary=END");
ServletOutputStream out = response.getOutputStream();
out.println("--END");
for(File f:files){
      FileInputStream fis = new FileInputStream(file);
      BufferedInputStream fif = new BufferedInputStream(fis);
      int data = 0;
      out.println("--END");
      while ((data = fif.read()) != -1) {
        out.write(data);
      }
      fif.close();
      out.println("--END");
      out.flush();
}
out.flush();
out.println("--END--");
out.close();

这在IE浏览器中不起作用。
N.B - 尝试不包括Catch块

This will not work in IE browsers. N.B - Try Catch blocks not included

这篇关于我可以在一个HttpServletResponse中附加多个附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆