Beispiel #1
0
 /**
  * 下载文件
  *
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return
  * @throws Exception
  */
 public ActionForward downloadFile(
     ActionMapping mapping,
     ActionForm form,
     HttpServletRequest request,
     HttpServletResponse response)
     throws Exception {
   BaseActionForm aForm = (BaseActionForm) form;
   Dto dto = aForm.getParamAsDto(request);
   String fileid = dto.getAsString("fileid");
   Dto fileDto = (BaseDto) g4Reader.queryForObject("Demo.queryFileByFileID", fileid);
   // 这里可稍做优化,根据文件类型动态设置此属性
   // response.setContentType("application/vnd.ms-excel");
   String filename = G4Utils.encodeChineseDownloadFileName(request, fileDto.getAsString("title"));
   response.setHeader("Content-Disposition", "attachment; filename=" + filename + ";");
   String path = fileDto.getAsString("path");
   File file = new File(path);
   BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
   ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
   byte[] temp = new byte[1024];
   int size = 0;
   while ((size = in.read(temp)) != -1) {
     out.write(temp, 0, size);
   }
   in.close();
   ServletOutputStream os = response.getOutputStream();
   os.write(out.toByteArray());
   os.flush();
   os.close();
   return mapping.findForward(null);
 }