コード例 #1
0
 @RequestMapping(value = "download")
 public void download(String id, HttpServletRequest request, HttpServletResponse response) {
   BaseMessage msg = null;
   if (StringUtils.isBlank(id)) {
     return;
   }
   Video video = videoService.queryVideo(id);
   String path = video.getUrl();
   String srcFolderPath = context.getRealPath("/") + path;
   FileInputStream fis = null;
   try {
     fis = new FileInputStream(new File(srcFolderPath));
     // 设置响应头和保存文件名
     //            response.setContentType("APPLICATION/OCTET-STREAM");
     response.addHeader("Content-Length", new File(srcFolderPath).length() + "");
     response.setHeader(
         "Content-Disposition",
         "attachment; filename=\"" + video.getName() + ".mp4" + "\""); // 写出流信息
     int b = 0;
     try {
       OutputStream out = response.getOutputStream();
       while ((b = fis.read()) != -1) {
         out.write(b);
       }
       out.close();
       out.close();
     } catch (FileNotFoundException e) {
       e.printStackTrace();
     } catch (IOException e) {
       e.printStackTrace();
     }
   } catch (FileNotFoundException e) {
     e.printStackTrace();
   }
 }