我有一个jsp / servlet web应用程序,客户端可以通过下拉框选择“课程”和“作业”,然后单击按钮下载该课程下列出的数据库中的所有文件/作业组合。 servlet代码不能正常工作,因为zip文件没有作为附件发送到浏览器。我确实有一个工作代码一次下载一个文件,但是有些东西被这个代码卡在了压缩文件中。数据库中的所有文件实际上都是zip文件本身,所以我试图压缩一堆zip文件。我不认为这会要求他们的处理方式与压缩任何其他格式的文件不同。有谁能看到缺少的东西?这是我处理下载的servlet中的doGet方法代码。这些代码大部分都是在stackoverflow上找到的。请注意,FileSubmitted对象是我的DOA,包含数据库中每个文件的所有文件信息,包括Blob本身:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
List<FileSubmitted> fileList = new ArrayList<FileSubmitted>();
String course= request.getParameter("course");
String assignment = request.getParameter("assignment");
java.sql.PreparedStatement pstmt = null;
java.sql.Connection conn = null;
ResultSet rs;
String queryString;
try {
conn = ConnectionManager.getConnection();
conn.setAutoCommit(false);
queryString = "SELECT * FROM files WHERE courseID=\""+course+"\" AND assignmentID=\""+assignment+"\";";
pstmt = conn.prepareStatement(queryString);
rs = pstmt.executeQuery(queryString);
while(rs.next())
{
fileList.add(new FileSubmitted(rs.getString("username"),
rs.getString("courseID"),
rs.getString("assignmentID"),
rs.getString("fileName"),
rs.getString("mimeType"),
(Blob) rs.getBlob("contents")));
}
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=\"allfiles.zip\"");
ZipOutputStream output = null;
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
try {
output = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE));
for (FileSubmitted file : fileList)
{
InputStream input = null;
try {
input = new BufferedInputStream(file.getContents().getBinaryStream(), DEFAULT_BUFFER_SIZE);
output.putNextEntry(new ZipEntry(file.getFileName()));
for (int length = 0; (length = input.read(buffer)) > 0;)
{
output.write(buffer, 0, length);
}
}//try
catch (SQLException e) {e.printStackTrace();}
finally{}
output.closeEntry();
}//for
}//try
finally{}
}
catch (Exception e1) {e1.printStackTrace();}
finally{}
}
答案 0 :(得分:5)
如果这对其他人有帮助,我找到了问题的答案。上面发布的代码实际上非常适合从数据库下载多个文件并创建一个zip文件供客户端下载。问题是我通过ajax调用servlet,显然你无法通过ajax调用下载文件。所以我改变了我的jsp页面,通过提交表单来调用servlet,然后完美地下载到了浏览器。
答案 1 :(得分:2)
首先创建一个包含所有zip文件的zip文件。
使用 ServletOutputStream
代替 ZipOutputStream
。
然后使用以下代码
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) {
final String filename = "/usr/local/FileName" + ".zip";
BufferedInputStream buf = null;
ServletOutputStream myOut = null;
try {
myOut = response.getOutputStream();
File myfile = new File(filename);
if (myfile.exists()) {
//myfile.createNewFile();
//set response headers
response.setHeader("Cache-Control", "max-age=60");
response.setHeader("Cache-Control", "must-revalidate");
response.setContentType("application/zip");
response.addHeader("Content-Disposition", "attachment; filename=" + filename);
response.setContentLength((int) myfile.length());
FileInputStream input = new FileInputStream(myfile);
buf = new BufferedInputStream(input);
int readBytes = 0;
//read from the file; write to the ServletOutputStream
while ((readBytes = buf.read()) != -1) {
myOut.write(readBytes);
}
}
} catch (Exception exp) {
} finally {
//close the input/output streams
if (myOut != null) {
try {
myOut.close();
} catch (IOException ex) {
}
}
if (buf != null) {
try {
buf.close();
} catch (IOException ex) {
}
}
}
}