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 31 32 33 34 35 36 37 38 39 40 41 42
| import lombok.Cleanup; import lombok.SneakyThrows; import lombok.val; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.dromara.hutool.core.io.IoUtil; import org.dromara.hutool.core.net.url.URLUtil; import org.springframework.http.MediaType; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.commons.CommonsMultipartFile;
public class MultiFileUtils {
@SneakyThrows public static MultipartFile getMultipartFile(String url, String fileName) { @Cleanup val inputStream = URLUtil.url(url).openStream(); FileItemFactory factory = new DiskFileItemFactory(16, null); FileItem fileItem = factory.createItem(fileName, MediaType.MULTIPART_FORM_DATA_VALUE, true, fileName); val bytes = IoUtil.readBytes(inputStream); @Cleanup val outputStream = fileItem.getOutputStream(); IoUtil.write(outputStream, false, bytes); return new CommonsMultipartFile(fileItem); }
}
|