我想编写一个代码来从某个位置下载文件。
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);
}
但它只对 IE 浏览器给出错误。Firefox 和 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)