为什么调用 OutputStream.flush() 时我的浏览器没有收到数据?

发布于 2024-12-24 02:16:52 字数 2213 浏览 0 评论 0原文

我有一个 servlet,它只读取文件并将其发送到浏览器。 文件读取正确,但在 OutputStream.flush() 上,浏览器未收到任何数据。

火狐浏览器 说: “内容损坏错误 无法显示您尝试查看的页面,因为检测到数据传输错误。”。Firebug 显示状态“已中止”。IE

打开或保存空文件。 我尝试过小文件或大文件。

代码是:

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // Use a ServletOutputStream because we may pass binary information
        response.reset();
        OutputStream out = response.getOutputStream();

        // Get the file to view
        String file = request.getParameter("path");

        // Get and set the type and size of the file
        String contentType = getServletContext().getMimeType(file);
        response.setContentType(contentType);
        long fileSize = (new File(file)).length();
        response.setHeader("Content-Length:", "" + fileSize);

        File f = new File(file);
        response.setHeader("Content-Disposition", "attachment;filename="+f.getName()); 
        response.setContentLength((int) fileSize);

        // Return the file
        try {
            returnFile(file, out, response);
        } catch (Exception e) {
            Logger.getLogger(AffichageItemsServlet.class).error("", e);
        } finally {
            out.close();
        }
    }

    // Send the contents of the file to the output stream
    public static void returnFile(String filename, OutputStream out, HttpServletResponse resp)
            throws FileNotFoundException, IOException {
        FileInputStream fis = null;

        try {
            fis = new FileInputStream(filename);
            byte[] buff = new byte[8* 1024];

            int nbRead = 0;
            while ((nbRead = fis.read(buff, 0, buff.length)) !=-1) {
                out.write(buff, 0, nbRead);
            }

            out.flush();
        } finally {
            if (fis != null) {
                fis.close();
            }
        }
    }

响应在“out.flush”上发送。

有什么想法吗?

I have a servlet which just read a file and send it to the browser.
The file is readen correctly, but on OutputStream.flush(), the browser receive no data.

Firefox says :
"Corrupted Content Error
The page you are trying to view cannot be shown because an error in the data transmission was detected.". Firebug shows the status "Aborted".

IE open or save an empty file.
I tried little or big files.

The code is :

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // Use a ServletOutputStream because we may pass binary information
        response.reset();
        OutputStream out = response.getOutputStream();

        // Get the file to view
        String file = request.getParameter("path");

        // Get and set the type and size of the file
        String contentType = getServletContext().getMimeType(file);
        response.setContentType(contentType);
        long fileSize = (new File(file)).length();
        response.setHeader("Content-Length:", "" + fileSize);

        File f = new File(file);
        response.setHeader("Content-Disposition", "attachment;filename="+f.getName()); 
        response.setContentLength((int) fileSize);

        // Return the file
        try {
            returnFile(file, out, response);
        } catch (Exception e) {
            Logger.getLogger(AffichageItemsServlet.class).error("", e);
        } finally {
            out.close();
        }
    }

    // Send the contents of the file to the output stream
    public static void returnFile(String filename, OutputStream out, HttpServletResponse resp)
            throws FileNotFoundException, IOException {
        FileInputStream fis = null;

        try {
            fis = new FileInputStream(filename);
            byte[] buff = new byte[8* 1024];

            int nbRead = 0;
            while ((nbRead = fis.read(buff, 0, buff.length)) !=-1) {
                out.write(buff, 0, nbRead);
            }

            out.flush();
        } finally {
            if (fis != null) {
                fis.close();
            }
        }
    }

The response is sent on "out.flush".

Any idea ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫南 2024-12-31 02:16:52

一方面,删除这一行(您在其下面调用 setContentLength()):

response.setHeader("Content-Length:", "" + fileSize);

此外,您可以尝试将 getOutputStream() 调用移到开始使用流之前。

For one thing, remove this line (you call setContentLength() below that):

response.setHeader("Content-Length:", "" + fileSize);

Also, you might try moving the getOutputStream() call to just before you start using the stream.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文