java 文件上传后下载

public String downFile()throws Exception{
  System.out.println(fId+"========="+mailId);
  MailAttach mailAttach = this.mailAttachManager.get(fId);

  String filePath = ZbwtFileUploadUtil.getBigFileUpload()+mailAttach.getAttachFilePath();  //文件路径
  File file = new File(filePath);
  if (file.exists()) {
   HttpServletResponse response = Struts2Utils.getResponse();
   HttpServletRequest request = Struts2Utils.getRequest();
   response.setHeader("Pragma", "No-Cache");
   response.setHeader("Cache-Control", "No-Cache");
   response.setDateHeader("Expires", 0);
   response.setHeader("Accept-Ranges", "bytes");
   response.setContentType("application/octet-stream");
   OutputStream out = response.getOutputStream();
   response.setHeader("Content-Type", "application/x-msdownload");
   String agent = (String)request.getHeader("USER-AGENT");  
   if(agent != null && agent.indexOf("MSIE") == -1) {   //用于中间有空格时 空格后名称无法出现
    // FF   
    String enableFileName = "=?UTF-8?B?" + (new String(Base64.encodeBase64(mailAttach.getAttachSolidName().getBytes("UTF-8")))) + "?=";  
    response.setHeader("Content-Disposition", "attachment; filename=" + enableFileName); } 
   else {  
    // IE   
    String enableFileName = new String(mailAttach.getAttachSolidName().getBytes("GBK"), "ISO-8859-1");    response.setHeader("Content-Disposition", "attachment; filename=" + enableFileName);
    response.setHeader("Content-Disposition", "attachment; filename=" + enableFileName);  
   }
   FileInputStream input = new FileInputStream(file);
   int line = 0;
   byte[] bytes = new byte[2048];
   while ((line = input.read(bytes)) != -1) {
    out.write(bytes, 0, line);
   }
   input.close();
   out.close();
  }
  return null;
 }

你可能感兴趣的:(下载文件,java File)