Jbe*_*erg 14
昨天必须做同样的事情.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(baos);
.... populate ZipOutputStream
String filename = "out.zip";
// the response variable is just a standard HttpServletResponse
response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
response.setContentType("application/zip");
try{
response.getOutputStream().write(baos.toByteArray());
response.flushBuffer();
}
catch (IOException e){
e.printStackTrace();
}
finally{
baos.close();
}
Run Code Online (Sandbox Code Playgroud)
注意我正在使用ByteArrayOutputStream包装器和toByteArray但您可能只需使用标准InputStream.read()OutputStream.write()循环将任何其他类型的Outputstream直接写入响应.
我实际上不确定哪个更快,但我怀疑我在这里使用ByteArrayOutputStream可能不是最有记忆意识的方法:
| 归档时间: |
|
| 查看次数: |
12847 次 |
| 最近记录: |