Java servlet:损坏的文件下载问题 [英] Java servlet: problem with corrupt file download

查看:458
本文介绍了Java servlet:损坏的文件下载问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用三个servlet来提供文件下载:

I use three servlets to serve files for download:


  • ByteArrayDownloadServlet:用于小文件,例如数据库中的报告或文件

  • FileDownloadServlet:用于小到大的文件

  • MultipleFileDownloadServlet:使用请求的文件创建一个zip并流式传送

它们基于以下实现:
链接文本

我收到了关于损坏的下载的几个投诉。问题是我无法模拟或找到错误中的模式:

I have received several complaints about corrupted downloads. The problem is that I can't simulate or find a pattern in the errors:


  • 有时与大文件

  • 有时当用户请求多个文件下载和压缩文件,并且动态地创建

  • 有时使用较小的文件,但是许多用户同时请求

  • sometimes with large files
  • sometimes when the user requests more than one file to download and a zip file and is created dynamically
  • sometimes with smaller files, but that are being requested by many users simultaneously

在上面提到的帖子中,有人报告了类似的问题,但没有解决方案。我也从这里读了很多线程,这更接近我:
链接文本

In the post's mentioned above comments there are people reporting similar problems, but no solution. I also read a lot of threads from here and this the closer I got: link text

有没有人遇到类似的问题或有一些示例代码可行?

Has anyone went through similar problem or have some sample code that works?

谢谢,
Felipe

Thanks, Felipe

@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");        
  }


推荐答案

最常见的原因是随机从servlet的损坏的下载是servlet不是线程安全的和/或它正在读字节为字符。在同一会话或servletcontext中的请求之间共享请求或基于会话的数据也是此问题的可能原因。

The most common causes for randomly corrupted downloads from a servlet is that the servlet is not threadsafe and/or that it is reading bytes as characters. Sharing request or session based data among requests in the same session or servletcontext is also a possible cause for this problem.

这篇关于Java servlet:损坏的文件下载问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆