Nemo

Nemo 关注TA

路漫漫其修远兮,吾将上下而求索。

Nemo

Nemo

关注TA

路漫漫其修远兮,吾将上下而求索。

  •  普罗旺斯
  • 负责帅就完事了
  • 写了1,495,102字

该文章投稿至Nemo社区   Java  板块 复制链接


SpringMVC文件上传工具

发布于 2016/06/16 17:35 1,749浏览 0回复 6,829

/**
* 根据得到的Spring上传的附件自动的加载保存

* @param files
* @return List<Attachment>
* @throws IllegalStateException
* @throws IOException
*/
@Transactional(readOnly = false)
public List<Attachment> saveAllUploadFile(List<MultipartFile> files)
    throws IllegalStateException, IOException {
    if (files == null || files.size() == 0) {
        return null;
    }

    List<Attachment> attachs = Lists.newArrayList();

    //预设存储目录

    String fileToPath = Global.getFileRootPath();
    for (MultipartFile f : files) {
          logger.info(f.getOriginalFilename() + "|" + f.getContentType()
                 + "|" + f.getSize());
          if (!f.isEmpty()) {
            // 判断目录 
            String saveRelativePath = new DateTime().toString("yyyyMM");
            String savePath = fileToPath + saveRelativePath;
            File path = new File(savePath);
            // 没有路径就直接创建
            if (!(path.exists() && path.isDirectory())) {
                path.mkdirs();
            }
          // 放到规定目录,用的Spring的自带的transferTo没有用FileUtils()
          String saveName = System.currentTimeMillis() + "";
          f.transferTo(new File(path + "/" + saveName));
          // 然后保存到Attachment
          String fileSuffix = f.getOriginalFilename().substring(
          f.getOriginalFilename().lastIndexOf("."));

        

         Attachment attach = new Attachment();

         attach.setSaveName(saveName);
         attach.setSavePath(savePath + "/");
         attach.setFileName(f.getOriginalFilename());
         attach.setFileSize(f.getSize());
         attach.setFileSuffix(fileSuffix);
         attach.setContentType(f.getContentType());
         attach.setSaveRelativePath(saveRelativePath + "/");
         attach.setFileType("upload");
         attachs.add(attach);
         dao.save(attach);
       }
    }
  return attachs;
}

@Transactional(readOnly = false)
public Attachment saveUploadFile(MultipartFile file) throws IllegalStateException, IOException {
    if (file == null) {
          return null;

      }

      //预设存储目录

     String fileToPath = Global.getFileRootPath();
     Attachment attach = new Attachment();
     if (!file.isEmpty()) {
     // 判断目录
     String saveRelativePath = new DateTime().toString("yyyyMM");
     String savePath = fileToPath + saveRelativePath;
     File path = new File(savePath); 
     // 没有路径就直接创建
     if (!(path.exists() && path.isDirectory())) {
          path.mkdirs();
     }
     // 放到规定目录,用的Spring的自带的transferTo没有用FileUtils()
     String saveName = System.currentTimeMillis() + "";
     file.transferTo(new File(path + "\\" + saveName));

     // 然后保存到Attachment 
     String fileSuffix = file.getOriginalFilename().substring(
     file.getOriginalFilename().lastIndexOf("."));

     attach.setSaveName(saveName);
     attach.setSavePath(savePath + "/");
     attach.setFileName(file.getOriginalFilename());
     attach.setFileSize(file.getSize());
     attach.setFileSuffix(fileSuffix);
     attach.setContentType(file.getContentType());
     attach.setSaveRelativePath(saveRelativePath + "/");
     attach.setFileType("upload");
     dao.save(attach);
  }
  return attach;
}

/**
* 导入文件

* @param file
* @return
* @throws IllegalStateException
* @throws IOException
*/
@Transactional(readOnly = false)
public Attachment saveImportUploadFile(MultipartFile file) throws IllegalStateException, IOException {
     if (file == null) {
         return null;

     }

     //预设存储目录

     String fileToPath = Global.getFileImportRootPath();
     Attachment attach = new Attachment();
     if (!file.isEmpty()) {
     // 判断目录
     String saveRelativePath = new DateTime().toString("yyyyMM");
     String savePath = fileToPath + saveRelativePath;
     File path = new File(savePath);
     // 没有路径就直接创建
     if (!(path.exists() && path.isDirectory())) {
           path.mkdirs();
      }
     // 放到规定目录,用的Spring的自带的transferTo没有用FileUtils()
     String saveName = System.currentTimeMillis() + "";
     file.transferTo(new File(path + "\\" + saveName));
 
     // 然后保存到Attachment
     String fileSuffix = file.getOriginalFilename().substring(
     file.getOriginalFilename().lastIndexOf("."));

     attach.setSaveName(saveName);
     attach.setSavePath(savePath + "/");
     attach.setFileName(file.getOriginalFilename());
     attach.setFileSize(file.getSize());
     attach.setFileSuffix(fileSuffix);
     attach.setFileType("import");
     attach.setContentType(file.getContentType());
     attach.setSaveRelativePath(saveRelativePath + "/");
     dao.save(attach);
   }
  return attach;
}    
点赞(0)

上一个文章:[NodeJS]Util.inherits,原型继承

下一个文章:ExcelUtil

点了个评