拉链,我不明白这是为什么这么难,每个人都有自己的执行...下载从一个servlet的Java
所以在我的服务器上,我产生一个.zip
文件,我希望用户可以能够点击下载。
因此,我设置了服务器成功接收的请求,现在,我正在努力将字节数组写入输出。
这里是我的响应代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("Downloading clusters.zip");
/* Generate the directory on the server, then zip it. */
clustersToFiles();
zipClusters();
/* Now the zip is saved on zipFullPath */
System.out.println("Done generating the .zip");
String parent_dir = System.getProperty("catalina.base");
String filename = "clusters.zip";
String zipFullPath = parent_dir + "/" + filename;
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
OutputStream out = response.getOutputStream();
FileInputStream fis = new FileInputStream(zipFullPath);
int bytes;
while ((bytes = fis.read()) != -1) {
System.out.println(bytes);
out.write(bytes);
}
fis.close();
response.flushBuffer();
System.out.println(".zip file downloaded at client successfully");
}
可能的复制http://stackoverflow.com/a/23645826/1495050 –
@pandaadb如果你认为'框架'可能会帮助我在这里,一切手段...... – Jack
其实,我认为你的问题不是servlet部分,而是复制部分。如果你喜欢,我可以发布一个球衣示例下载。至于框架,将任何文件复制到输出流中可以用例如google commons:'Files.copy(new File(“path/to/zip/file”),output);'另外,如果这适用于普通文件,你确定你生成的zip文件不只是空的吗?它不应该有什么区别你试图复制什么文件 – pandaadb