为什么恢复下载会在Java应用程序中下载损坏的文件?

时间:2018-12-31 11:50:42

标签: java io struts2 inputstream downloading

在基于Struts的Java Web应用程序中,我实现了具有暂停/恢复功能的下载功能。让我解释一下我的下载是如何工作的,实际上文件(例如图像文件,doc)将在服务器中转换为pdf,然后开始下载,然后不暂停,它已成功下载了文件并且没有损坏,但是当我暂停并恢复时它可以成功下载文件,但会损坏pdf。我不知道我在想什么。我的代码如下

{           in = new FileInputStream(f);
            int DOWNLOAD_BUFFER_SIZE = Integer.parseInt(CacheManagement.getInstance().getSystemPropertyByAlias().get("DOWNLOAD_BUFFER_SIZE").getPropertyValue());

            response.setHeader("Accept-Ranges", "bytes");
            response.setContentType("application/x-download");
            response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
            response.setBufferSize(DOWNLOAD_BUFFER_SIZE);

            // SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
            // Date lastModifiedDate = formatter.parse(String.valueOf(f.lastModified()));
            Date lastModifiedDate = new Date(f.lastModified());
            response.setHeader("ETag", f.length() + "_" + lastModifiedDate.getTime());
            response.setDateHeader("Last-Modified", lastModifiedDate.getTime());

            String range = request.getHeader("Range");
            if (range != null && !range.isEmpty())
            {
                ArrayList<Range> ranges = getRangeList(range, f.length());
                Range r = ranges.get(0);
                response.setHeader("Content-Range", "bytes " + r.getStart() + "-" + r.getEnd() + "/" + r.getTotal());
                response.setHeader("Content-Length", String.valueOf(r.getLength()));
                response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
            }
            else
            {
                response.setHeader("Content-Length", String.valueOf(f.length()));
                response.setHeader("Content-Range", "bytes 0-" + (f.length() - 1) + "/" + f.length());
            }

            o = response.getOutputStream();
            buf = new BufferedInputStream(in);
            byte[] dataBytes = new byte[DOWNLOAD_BUFFER_SIZE];
            int readed = 0;
            while ((readed = buf.read(dataBytes)) != -1)
            {
                try
                {
                    o.write(dataBytes, 0, readed);
                }
                catch (Exception e)
                {
                    // System.out.println("-- Client Aborted Download --");
                    logger.error("-- Client Aborted Download --");
                    break;
                }
            } 

1 个答案:

答案 0 :(得分:1)

不要使用 buf = new BufferedInputStream(in); 读取剩余的字节,您需要使用 RandomAccessFile 类和 seek() >使用剩余字节开始下载的方法。

    RandomAccessFile raFile = new RandomAccessFile(f, "r");
    o = response.getOutputStream();
    // buf = new BufferedInputStream(in);
    byte[] dataBytes = new byte[DOWNLOAD_BUFFER_SIZE];
    int readed;
    raFile.seek(r.getStart());
    while ((readed = raFile.read(dataBytes)) != -1)
    {
        try
        {
            o.write(dataBytes, 0, readed);
        }
        catch (Exception e)
        {
            logger.error("-- Client Aborted Download --");
            break;
        }
    }