File类型 转换 MultipartFile 类型

public class Test {
  public static void main(String[] args) {
       String path = ”E:/test/test.txt“ //文件路径
       MultipartFile abc = single(path); //MultipartFile 类型
    }
  //传入文件路径,返回MultipartFile 类型.
  public static MultipartFile single(String path) throws IOException {
        Resource resource = new ClassPathResource(path);
        String fileName = resource.getFilename();
        String fileNameNoExtension = getFileNameNoExtension(fileName);
        InputStream inputStream = resource.getInputStream();
        return new MockMultipartFile(fileNameNoExtension, fileName, "text/plain", inputStream);
    }
}

你可能感兴趣的:(File类型 转换 MultipartFile 类型)