1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package com.manpower.util; import cn.hutool.core.io.FileUtil; import org.apache.commons.io.FileUtils; import org.springframework.mock.web.MockMultipartFile; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; @Component public class MFtoFileUtil { public static MultipartFile MFtoFile(MultipartFile file, String filename) throws Exception { assert file.getOriginalFilename() != null; File tempFile = new File(file.getOriginalFilename()); FileUtils.copyInputStreamToFile(file.getInputStream(), tempFile); tempFile = FileUtil.rename(tempFile, filename, true, true); InputStream inputStream = new FileInputStream(tempFile); String newFilename = tempFile.getName().substring(0, tempFile.getName().lastIndexOf('.')); file = new MockMultipartFile(newFilename, tempFile.getName(), "multipart/form-data", inputStream); tempFile.delete(); return file; } }
|