java下载zip_从Servlet Java下载zip
我不明白為什么這么難,每個人都有自己的實現…
因此,在服務器中,我生成了一個.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");
}
總結
以上是生活随笔為你收集整理的java下载zip_从Servlet Java下载zip的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java生成xml_java 生成xml
- 下一篇: java main是多线程的吗_Java