我一直在開發一個java web應用程序,我想添加一個下載功能。 我想下載位於「C:\ apache-tomcat-6.0.36 \ webapps \ xml \ XML.zip」中的zip文件。 我已經將文件轉換爲InputStream,但我仍然困惑如何從InputStream獲取輸入流數據?如何從輸入流文件獲取輸入流?
當我點擊下載按鈕,它會返回zip文件
這裏的0(零)字節是處理下載壓縮文件控制器:
@RequestMapping("download")
public String Download(HttpServletResponse response) {
ZipInputStream zis = null;
try {
InputStream is = new FileInputStream("C:\\apache-tomcat-6.0.36\\webapps\\xml\\XML.zip");
zis = new ZipInputStream(is);
response.setHeader("Content-Disposition", "inline;filename=\"" + "XML.zip" + "\"");
OutputStream out = response.getOutputStream();
response.setContentType("application/zip");
IOUtils.copy(zis.getInputStream, out);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
這條線是導致零字節拉鍊文件:
IOUtils.copy(**zis.getInputStream**, out);
您的文件路徑未被引用(「C:\\ apache」)。這是在你的代碼中嗎? – SJuan76
我很疑惑......爲什麼包含下載按鈕的圖片很重要? – ppeterka
另外,如果你想得到整個ZIP文件的下載,你不必使用ZipInputStream ...這是訪問_the ZIP文件的內容...而不是'zis.getInputStream()'使用'是.getInputStream()',並刪除與ZipInputStream相關的代碼... – ppeterka