NDeveloper
NDeveloper

Reputation: 3177

File download code gives error in IE, All's fine in other browsers

I wanted to write a code to download a file from some location.

String filePath = policyLocation;

    File f = new File(filePath+"/"+fileName);

    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
    response.setContentLength((int) f.length());

    BufferedInputStream fileInput = new BufferedInputStream(new FileInputStream(f));
    byte buffer[] = new byte[8 * 1024];
    java.io.PrintWriter out = response.getWriter();
    OutputStream out_s = new Writer2Stream(out);
    copyStreamsWithoutClose(fileInput, out_s, buffer);
    fileInput.close();
    out_s.flush();

public static void copyStreamsWithoutClose(InputStream in, OutputStream out, byte[] buffer)
throws IOException {
    int b;
    while ((b = in.read(buffer)) != -1)
        out.write(buffer, 0, b);
}

but it gives an error for IE browsers only. All's fine for Firefox and Chrome

java.io.FileNotFoundException: D:\jboss-4.2.0.GA\Policies\HR (Access is denied)
    java.io.FileInputStream.open(Native Method)
    java.io.FileInputStream.<init>(FileInputStream.java:106)
    com.edifixio.ems.policiesandforms.action.PolicyFileDownloader.downloadHRPolicyFile(PolicyFileDownloader.java:34)
    com.edifixio.ems.servlet.FileDisplayServlet.doPost(FileDisplayServlet.java:271)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

Upvotes: 0

Views: 336

Answers (2)

Joop Eggen
Joop Eggen

Reputation: 109547

The content disposition filename needs to be encoded, unfortunately differently for IE and the rest of the world. One search gave this answer, but that seems different from a version I made once. But try it.

Or ensure the name is pure ASCII, and do not use a "path" (\) inside the name.

Upvotes: 0

Stephen C
Stephen C

Reputation: 718778

The stacktrace seems to be saying that your server is attempting to open a particular file from the file system, and the operating system is saying "No! Not allowed!".

But how it can possibly matter what the browser is escapes me ... unless there is something causing IE to request a different file to the one that is being requested in Firefox and Chrome.

I suggest that you capture the requests that are being made in IE, Firefox and Chrome cases ... and check to see if the request URLs and request parameters are the same.

Upvotes: 1

Related Questions