Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.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 Firefox文件下载Servlet_Java_Google App Engine_Gwt - Fatal编程技术网

Java Firefox文件下载Servlet

Java Firefox文件下载Servlet,java,google-app-engine,gwt,Java,Google App Engine,Gwt,我编写了一个servlet来启动文件下载。从客户端的请求中接收文件的数据。它适用于ie、chrome和safari,但不适用于Firefox。这是什么原因?firefox处理下载的方式与其他浏览器不同吗 private static final int BUFFER_SIZE = 4096; protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletExceptio

我编写了一个servlet来启动文件下载。从客户端的请求中接收文件的数据。它适用于ie、chrome和safari,但不适用于Firefox。这是什么原因?firefox处理下载的方式与其他浏览器不同吗

private static final int BUFFER_SIZE = 4096; 
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    String before = req.getReader().readLine();
    String filename = "";
    String content = "";

    if(before == null) {
        resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "No data received!");
        return;
    } else {        
        String decoded = URLDecoder.decode(before, "UTF-8");
        {
            int i = 0;
            StringTokenizer tokenizer = new StringTokenizer(decoded, "\n");
            while (tokenizer.hasMoreTokens()) {
                if(i == 0) {
                    filename += tokenizer.nextToken();
                } else {
                    content += tokenizer.nextToken();
                }
                i++;
            }
        }           
        filename = filename.replace("hidden=", "").replace("\n", "").replace("\r", "");
    }

    ByteArrayInputStream inputStream = new ByteArrayInputStream(content.getBytes()); 
    int fileLength = inputStream.available(); 

    ServletContext context = getServletContext();

    // sets MIME type for the file download
    String mimeType = context.getMimeType(filename);
    if (mimeType == null) {        
        mimeType = "application/octet-stream";
    }              

    // set content properties and header attributes for the response
    resp.setContentType(mimeType);
    resp.setContentLength(fileLength);
    String headerKey = "Content-Disposition";
    String headerValue = String.format("attachment; filename=\"%s\"", filename);
    resp.setHeader(headerKey, headerValue);

    // writes the file to the client
    OutputStream outStream = resp.getOutputStream();

    byte[] buffer = new byte[BUFFER_SIZE];
    int bytesRead = -1;

    while ((bytesRead = inputStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, bytesRead);
    }

    inputStream.close();
    outStream.close(); 
}

如果希望浏览器始终显示下载对话框(而不是尝试打开文件本身),请尝试强制将内容类型设置为
application/octet stream
,而不是根据文件类型进行设置

换句话说,请尝试替换以下行:

ServletContext context = getServletContext();

// sets MIME type for the file download
String mimeType = context.getMimeType(filename);
if (mimeType == null) {        
    mimeType = "application/octet-stream";
}              

// set content properties and header attributes for the response
resp.setContentType(mimeType);
通过这个:

resp.setContentType("application/octet-stream");

我不知道您是如何触发下载的,但如果您使用的是隐藏帧,我猜Firefox会尝试直接在隐藏帧中打开文件,因为它可以识别文件类型。

请展开“不工作”部分。调试问题的近似值是非常困难的……在ie、chrome&safari中,下载会显示正确的名称和数据,但在firefox中不会。这是显而易见的结果,但它不会帮助您调试错误。在这里发布相关的源代码和任何其他有用的信息。你说Firefox中不会出现was。到底发生了什么?确实没有设置为自动开始下载吗?