我用三個servlet來提供文件下載:的Java servlet:問題與損壞的文件下載
- ByteArrayDownloadServlet:用於小文件,如
- FileDownloadServlet從數據庫報告或文件:用於小大文件
- MultipleFileDownloadServlet:創建請求文件的zip和流它
他們根據以下實現: link text
我所收到的有關損壞下載多次投訴。問題是,我不能模擬或發現在錯誤的模式:
-
有時
- 大文件
- 有時當用戶請求一個以上的文件下載和zip文件,並動態創建 有時
- 較小的文件,但正在被許多用戶要求同時
在上面提到的意見都有人報告類似的問題這篇文章的,但沒有解決方案。我也讀了很多來自這裏的線索和這個我得到更近: link text
有沒有人經歷過類似的問題或有一些示例代碼的作品?
謝謝, 菲利普
@Override
@SuppressWarnings("unchecked")
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
HttpSession session = request.getSession();
List<File> selectedFileList = (List<File>) session.getAttribute("selectedFileList");
if(selectedFileList == null)
{
response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED, "Lista de arquivos não informada");
return;
}
response.reset();
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=\""
+ "atualizacoes_"
+ new Date().getTime() + ".zip" + "\"");
ZipOutputStream output = null;
try
{
output = new ZipOutputStream(response.getOutputStream());
for(File file : selectedFileList)
{
InputStream input = new FileInputStream(file);
output.putNextEntry(new ZipEntry(file.getName()));
byte[] buffer = new byte[DownloadHandler.DEFAULT_BUFFER_SIZE];
int length;
while((length = input.read(buffer)) > 0)
{
output.write(buffer, 0, length);
}
output.closeEntry();
input.close();
}
output.finish();
output.flush();
output.close();
}
catch(Exception e)
{
if(!(e instanceof ClientAbortException))
{
new ExceptionMail(getClass().getSimpleName(), e);
}
}
finally
{
session.removeAttribute("selectedFileList");
}
你終於發現BalusC:http://stackoverflow.com/users/157882/balusc – 2010-10-04 13:19:36
'FileServlet'你改變對原來的什麼?沒有一些代碼或其他細節,很難確切地知道發生了什麼問題。 – 2010-10-04 14:34:56