Ejemplo n.º 1
0
  @Transactional(readOnly = false, rollbackFor = Exception.class)
  private Map<String, String> upload(MultipartFile multipartFile, String uploadPath) {
    String fileUniqueName = String.valueOf(System.nanoTime());

    String originFileName = multipartFile.getOriginalFilename();
    String uploadedFileFullPath =
        new StringBuffer(uploadPath)
            .append(fileUniqueName)
            .append("_")
            .append(originFileName)
            .toString();

    String uploadedPath = uploadedFileFullPath.substring(0, uploadedFileFullPath.lastIndexOf("/"));
    File uploadedPathFile = new File(uploadedPath);
    if (!uploadedPathFile.exists()) {
      uploadedPathFile.mkdirs();
    }

    File uploadedFile = new File(uploadedFileFullPath);
    try {
      FileCopyUtils.copy(multipartFile.getInputStream(), new FileOutputStream(uploadedFile));
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

    UploadedFile uploadedFileBean = new UploadedFile();
    uploadedFileBean.setFileName(originFileName);
    uploadedFileBean.setFilePath(uploadedFileFullPath);
    uploadedFileBean.setFileSize(String.valueOf(multipartFile.getSize()));

    fileRepository.save(uploadedFileBean);

    Map<String, String> fileInfo = new HashMap<String, String>();
    fileInfo.put("name", originFileName);
    fileInfo.put("path", uploadedFileFullPath);
    fileInfo.put("id", String.valueOf(uploadedFileBean.getId()));

    return fileInfo;
  }
Ejemplo n.º 2
0
 @Override
 @Transactional(readOnly = true)
 public Object getUploadedImage(Long id) {
   return fileRepository.findOne(id);
 }