コード例 #1
0
  /*
   @RequestMapping(value = "/uploadfile.html", method = RequestMethod.GET)
   public String uploadFile(){
   	return "uploadfile";
   }
  */
  @RequestMapping(value = "/uploadfile.html", method = RequestMethod.POST)
  public String processUploadPreview(
      @RequestParam("file") MultipartFile file,
      @RequestParam("id") Integer recordId,
      Map<String, Object> respMap) {
    Record record = recordService.getRecord(recordId);
    respMap.put("record", record);
    if (record == null) {
      // respMap.put("errMesg", "Incorect or not available record");
      return "redirect:records.html";
    }
    respMap.put("id", recordId);
    if (file.getSize() > 5242880) {
      respMap.put("errMesg", "Max file size 5MB");
      return "redirect:editrecord.html";
    }
    if (file.getName().equals("")) {
      respMap.put("errMesg", "File not selected");
      return "redirect:editrecord.html";
    }

    File newFile = new File();
    newFile.setRecord(record);
    newFile.setFilename(file.getOriginalFilename());
    newFile.setType(file.getName());
    try {
      newFile.setFile(file.getBytes());
      fileService.addFilele(newFile);
    } catch (Exception e) {
      respMap.put("errMesg", "Server error");
      e.printStackTrace();
      return "redirect:editrecord.html";
    }
    return "redirect:editrecord.html";
  }
コード例 #2
0
 @RequestMapping(value = "/getfile")
 public String dovnloadFile(@RequestParam("id") Integer fileId, HttpServletResponse response) {
   File file = null;
   try {
     file = fileService.getFile(fileId);
   } catch (HibernateException e) {
   }
   if (file != null) {
     if ((getCurrentUser().equals(file.getRecord().getUser()))
         || file.getRecord().getAccess().getId() == 2) {
       response.setContentType(file.getType());
       response.setContentLength(file.getFile().length);
       response.setHeader(
           "Content-Disposition", "attachment; filename=\"" + file.getFilename() + "\"");
       try {
         FileCopyUtils.copy(file.getFile(), response.getOutputStream());
       } catch (IOException e) {
         e.printStackTrace();
       }
     }
   }
   return null;
 }