Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的浏览器在调用OutputStream.flush()时没有收到数据?_Java_File Io_Outputstream - Fatal编程技术网

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

Java 为什么我的浏览器在调用OutputStream.flush()时没有收到数据?,java,file-io,outputstream,Java,File Io,Outputstream,我有一个servlet,它只读取一个文件并将其发送到浏览器。 文件已正确读取,但在OutputStream.flush()上,浏览器未接收任何数据 Firefox说: “损坏的内容错误 无法显示您试图查看的页面,因为检测到数据传输错误。“。Firebug显示“已中止”状态 IE打开或保存一个空文件。 我试过小文件或大文件 代码是: /** * Processes requests for both HTTP <code>GET</code> and &

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

Firefox说: “损坏的内容错误 无法显示您试图查看的页面,因为检测到数据传输错误。“。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”上发送


有什么想法吗?

首先,删除这一行(在下面调用setContentLength()):


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

首先,删除此行(在该行下方调用setContentLength()):


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

getParameter(“path”)是返回文件名的绝对路径还是仅返回文件名?getParameter(“path”)是返回文件名的绝对路径还是仅返回文件名?谢谢!删除此行:response.setHeader(“内容长度:”,“+fileSize”);解决了我的问题,谢谢!删除此行:response.setHeader(“内容长度:”,“+fileSize”);解决了我的问题。
response.setHeader("Content-Length:", "" + fileSize);