-1
我正在創建一個網站。我的兩個文件位於不同的位置,我想將這兩個文件壓縮到一個zip文件中。如果我點擊下載選項,兩個不同位置的文件可以下載到一個zip文件中並提供給用戶。如何在JSP中做到這一點?下載兩個文件並將其製作爲zip文件
我嘗試在JSP中,但我不知道該怎麼辦......
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.io.BufferedReader,java.io.IOException,java.io.InputStreamReader,java.util.Enumeration,java.util.zip.*,java.util.zip.ZipFile"%>
<html>
<body>
<%
//getting the zip file path
String path=request.getParameter("path");
response.setContentType("application/zip");
//response.setHeader("Content-Disposition", "attachment; filename=\"" + path + "\"");
byte[] _buf = new byte[1024];
//zos is used to write files in zip
ZipOutputStream zos = null;
//zip is used to read files
ZipInputStream zis = null;
boolean bsuccess = true;
zos = new ZipOutputStream(new java.io.FileOutputStream("D:/temp/SSO.zip"));
zis = new ZipInputStream(new java.io.FileInputStream(path));
ZipEntry ze = null;
while ((ze = zis.getNextEntry()) != null)
{
String fname = ze.getName();
if (fname!=null)
{
//copy the entry from zis to zos
int bytes = 0;
//deal with password protected files
zos.putNextEntry(new ZipEntry(fname));
while ((bytes = zis.read(_buf)) > 0)
{
zos.write(_buf, 0, bytes);
}
zis.closeEntry();
zos.closeEntry();
}
}
if (zis != null)
zis.close();
if (zos != null)
zos.close();
%>
你提的問題是過於寬泛。請告訴我們您到目前爲止嘗試過什麼 –
...使用Java Zip API將它們壓縮在一起,然後將其流式傳輸到客戶端。這些東西都很容易搜索,並有示例。儘管這種類型的工作確實屬於視圖層之外。 –