java中File轉(zhuǎn)為MultipartFile的四種實現(xiàn)方式
問題背景
項目中需要調(diào)用別人的接口上傳一個文件,別人的接口參數(shù)為MultipartFile類型,需要對File文件進行一個轉(zhuǎn)換再進行上傳
File轉(zhuǎn)MultipartFile
1.方法一
public static MultipartFile getMultipartFile(File file) { FileItem item = new DiskFileItemFactory().createItem("file" , MediaType.MULTIPART_FORM_DATA_VALUE , true , file.getName()); try (InputStream input = new FileInputStream(file); OutputStream os = item.getOutputStream()) { // 流轉(zhuǎn)移 IOUtils.copy(input, os); } catch (Exception e) { throw new IllegalArgumentException("Invalid file: " + e, e); } return new CommonsMultipartFile(item); }
可以設(shè)置為靜態(tài)方法,也可以使用對象進行調(diào)用
File file = new File("D:\\a.txt"); MultipartFile cMultiFile = getMultipartFile(file);
2.方法二
// 第二種方式 public static MultipartFile getMultipartFile(File file) { DiskFileItem item = new DiskFileItem("file" , MediaType.MULTIPART_FORM_DATA_VALUE , true , file.getName() , (int)file.length() , file.getParentFile()); try { OutputStream os = item.getOutputStream(); os.write(FileUtils.readFileToByteArray(file)); } catch (IOException e) { e.printStackTrace(); } return new CommonsMultipartFile(item); }
可以設(shè)置為靜態(tài)方法,也可以使用對象進行調(diào)用
File file = new File("D:\\a.txt"); MultipartFile cMultiFile = getMultipartFile(file);
3.方法三
創(chuàng)建FileItem
public static FileItem createFileItem(String filePath, String fileName){ String fieldName = "file"; FileItemFactory factory = new DiskFileItemFactory(16, null); FileItem item = factory.createItem(fieldName, "text/plain", false,fileName); File newfile = new File(filePath); int bytesRead = 0; byte[] buffer = new byte[8192]; try (FileInputStream fis = new FileInputStream(newfile); OutputStream os = item.getOutputStream()) { while ((bytesRead = fis.read(buffer, 0, 8192))!= -1) { os.write(buffer, 0, bytesRead); } } catch (IOException e) { e.printStackTrace(); } return item; }
File file = new File("D:\\a.txt"); FileItem fileItem = createFileItem(file.getPath(),file.getName()); MultipartFile cMultiFile = new CommonsMultipartFile(fileItem);
4.方法4
添加依賴
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency>
import org.springframework.mock.web.MockMultipartFile; File file = new File("D:\\a.txt"); MultipartFile cMultiFile = new MockMultipartFile("file", file.getName(), null, new FileInputStream(file));
5.如果傳輸有點問題可能傳輸?shù)念愋陀悬c不同
MediaType.MULTIPART_FORM_DATA_VALUE
更改為
MediaType.TEXT_PLAIN_VALUE
總結(jié)
方法有很多,自己選一種合適的
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
spring啟動錯誤Singleton bean creation not al
本文主要介紹了spring啟動錯誤Singleton bean creation not allowed while the singletons of this factory are indestruction,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2023-07-07Java Quartz觸發(fā)器CronTriggerBean配置用法詳解
這篇文章主要介紹了Java Quartz觸發(fā)器CronTriggerBean配置用法詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08Java concurrency線程池之線程池原理(三)_動力節(jié)點Java學(xué)院整理
這篇文章主要為大家詳細介紹了Java concurrency線程池之線程池原理第三篇,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06Java使用注解實現(xiàn)BigDecimal的四舍五入
BigDecimal是Java中的一個類,位于java.math包中,它提供了任意精度的有符號十進制數(shù)字的表示,以及對這些數(shù)字進行算術(shù)運算的方法,本文介紹了Java使用注解實現(xiàn)BigDecimal的四舍五入的相關(guān)知識,需要的朋友可以參考下2024-09-09SpringBoot 實戰(zhàn) 之 優(yōu)雅終止服務(wù)的方法
本篇文章主要介紹了SpringBoot 實戰(zhàn) 之 優(yōu)雅終止服務(wù)的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05在Windows系統(tǒng)下安裝Thrift的方法與使用講解
今天小編就為大家分享一篇關(guān)于在Windows系統(tǒng)下安裝Thrift的方法與使用講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12