我编写了一个从DB(mySQL)上传和下载文件(.doc,.xls和.txt)的程序。它是使用Spring MVC编写的。上传和下载在IE中正常工作,但在Firefox中,它不能作为例外工作。
要下载文件,请在JSP中提供一个链接,点击该链接将弹出“文件下载”对话框。在IE中弹出这个对话框,允许以正确的格式打开和/或保存文件(即.doc,.xls和/或.txt)。
在Firefox中,点击链接时会执行以下操作:
public class FileController extends AbstractFormController{
SearchResultService searchResultService;
public void setSearchResultService(SearchResultService searchResultService){
this.searchResultService = searchResultService;
}
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
ResultSet result = searchResultService.getAttachment(Integer.parseInt(arg0.getParameter("prbId")));
byte[] buff = new byte[100000];
try {
result.next();
if(!(result.getBinaryStream(1)==null)){
String filename = "fname."+result.getString("file_ext");
if(result.getString("file_ext").equals("txt")){
InputStream is = result.getBinaryStream(1);
OutputStream out = arg1.getOutputStream();
arg1.reset();
int bytesRead;
while ((bytesRead = is.read(buff)) != -1) {
out.write(buff, 0, bytesRead);
}
arg1.setContentType("");
arg1.setHeader("content-disposition", "attachment; filename="+filename);
is.close();
out.flush();
out.close();
}else{
arg1.reset();
if(result.getString("file_ext").equals("doc")||result.getString("file_ext").equals("docx")){
arg1.setContentType("application/msword");
}else if(result.getString("file_ext").equals("xls")||result.getString("file_ext").equals("xlsx")){
arg1.setContentType("application/vnd.ms-excel");
}else if(result.getString("file_ext").equals("pdf")){
arg1.setContentType("application/pdf");
}
arg1.setHeader("Content-Desposition","attachment; filename="+filename);
byte[] bytesGot = result.getBytes(1);
ServletOutputStream outs = arg1.getOutputStream();
outs.write(bytesGot);
outs.flush();
outs.close();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return new ModelAndView();
}
}
<a href="<%=contextPath%>/file.do?prbId=<%=desc.getProblemId() %>">Download File</a>
正确地在db中保存/检索所有扩展。请帮忙。
答案 0 :(得分:0)
对于第2点 - 设置标题时出现拼写错误。
arg1.setHeader("Content-Desposition","attachment; filename="+filename);
应该是Content-Disposition而不是Content-Desposition。
对于第1点 - 尝试重置浏览器首选项。有关详细信息,请参阅this链接。
答案 1 :(得分:0)
您需要设置正确的标头。我正在设置这些标题以下载我的.xls文件及其工作。
response.setHeader("Pragma", "public");
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Content-type", "application-download");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
response.setHeader("Content-Transfer-Encoding", "binary");
答案 2 :(得分:0)
public class FileController extends AbstractFormController{ SearchResultService searchResultService; public void setSearchResultService(SearchResultService searchResultService){ this.searchResultService = searchResultService; } @Override protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { ResultSet result = searchResultService.getAttachment(Integer.parseInt(arg0.getParameter("prbId"))); byte[] buff = new byte[100000]; try { result.next(); if(!(result.getBinaryStream(1)==null)){ String filename = "fname."+result.getString("file_ext"); if(result.getString("file_ext").equals("txt")){ InputStream is = result.getBinaryStream(1); OutputStream out = arg1.getOutputStream(); arg1.reset(); int bytesRead; while ((bytesRead = is.read(buff)) != -1) { out.write(buff, 0, bytesRead); } response.setContentType("text/plain"); arg1.setHeader("content-disposition", "attachment; filename="+filename); is.close(); out.flush(); out.close(); }else{ arg1.reset(); if(result.getString("file_ext").equals("doc")||result.getString("file_ext").equals("docx")){ arg1.setContentType("application/msword"); }else if(result.getString("file_ext").equals("xls")||result.getString("file_ext").equals("xlsx")){ arg1.setContentType("application/msexcel"); }else if(result.getString("file_ext").equals("pdf")){ arg1.setContentType("application/pdf"); } arg1.setHeader("Content-Disposition","attachment; filename="+filename); byte[] bytesGot = result.getBytes(1); ServletOutputStream outs = arg1.getOutputStream(); outs.write(bytesGot); outs.flush(); outs.close(); } } } catch (SQLException e) { e.printStackTrace(); } return new ModelAndView(); } }