MultipartFile 和 File 互转

MultipartFile 转 File
public File M2F(MultipartFile file) throws Exception {
            File f=File.createTempFile(UUID.randomUUID().toString(), "." + FilenameUtils.getExtension(file.getOriginalFilename()));
            file.transferTo(f);
            return f;
        }

 

File 转 MultipartFile

导入

<dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-test</artifactId>
      </dependency>
public MultipartFile F2M(File file) throws Exception
{
MultipartFile multipartFil;
File file = new File("文件路径");
        if(file.exists()) {
            FileInputStream input = new FileInputStream(file);
            multipartFile =new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input));
        }
        return multipartFil;
}

 

上一篇:Java针对MultipartFile上传图片获取宽、高


下一篇:Luogu P2825 [HEOI2016/TJOI2016]游戏