简体   繁体   中英

File Download Servlet with Firefox

I write a servlet which initiate a file download. The data of the file is received in the request from the client. It works in ie, chrome & safari, but not in Firefox. What is the reason for this? Is firefox handling downloads different than other browsers?

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(); 
}

If you want the browser to always display the download dialog (and not try to open the file itself), try forcing the content type to application/octet-stream instead of setting it based on the file type.

In other words, try replacing these lines:

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);

by this one:

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

I don't know how you are triggering the download, but if you are using a hidden frame, my guess is that Firefox tries to open the file directly in the hidden frame because it recognizes the file type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM