如何將二進制文件流轉(zhuǎn)化為MockMultipartFile文件
一、名詞解釋及業(yè)務(wù)解釋
1.具體業(yè)務(wù)流程
獲取二進制文件流:
- 文件可以來自多個來源,如用戶上傳的文件、數(shù)據(jù)庫存儲的二進制數(shù)據(jù)、文件系統(tǒng)中的文件等。
- 讀取文件內(nèi)容,轉(zhuǎn)換為字節(jié)數(shù)組。
創(chuàng)建 MockMultipartFile
對象:
- 使用
MockMultipartFile
類,將字節(jié)數(shù)組、文件名和文件類型作為參數(shù)傳入構(gòu)造函數(shù)。 MockMultipartFile
是 Spring 提供的一個類,主要用于在測試中模擬文件上傳。
處理上傳邏輯:
- 將
MockMultipartFile
傳遞給業(yè)務(wù)邏輯層,進行文件的保存、處理或解析等操作。 - 你可以在控制器中定義文件上傳的接口,處理接收到的文件。
返回響應(yīng):
- 根據(jù)業(yè)務(wù)處理結(jié)果,返回相應(yīng)的響應(yīng)信息給客戶端。
2.轉(zhuǎn)換對象解釋
1. MockMultipartFile
功能:用于模擬文件上傳場景,特別是在測試中非常有用。
具體源碼:
package org.springframework.mock.web; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.FileCopyUtils; import org.springframework.web.multipart.MultipartFile; public class MockMultipartFile implements MultipartFile { private final String name; private final String originalFilename; @Nullable private final String contentType; private final byte[] content; public MockMultipartFile(String name, @Nullable byte[] content) { this(name, "", (String)null, (byte[])content); } public MockMultipartFile(String name, InputStream contentStream) throws IOException { this(name, "", (String)null, (byte[])FileCopyUtils.copyToByteArray(contentStream)); } public MockMultipartFile(String name, @Nullable String originalFilename, @Nullable String contentType, @Nullable byte[] content) { Assert.hasLength(name, "Name must not be empty"); this.name = name; this.originalFilename = originalFilename != null ? originalFilename : ""; this.contentType = contentType; this.content = content != null ? content : new byte[0]; } public MockMultipartFile(String name, @Nullable String originalFilename, @Nullable String contentType, InputStream contentStream) throws IOException { this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream)); } public String getName() { return this.name; } @NonNull public String getOriginalFilename() { return this.originalFilename; } @Nullable public String getContentType() { return this.contentType; } public boolean isEmpty() { return this.content.length == 0; } public long getSize() { return (long)this.content.length; } public byte[] getBytes() throws IOException { return this.content; } public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(this.content); } public void transferTo(File dest) throws IOException, IllegalStateException { FileCopyUtils.copy(this.content, dest); } }
構(gòu)造參數(shù):
name
: 表單中對應(yīng)的字段名(如"file"
)。originalFilename
: 上傳文件的原始文件名(如"test.txt"
)。contentType
: 文件的內(nèi)容類型(如"text/plain"
)。content
: 文件的字節(jié)內(nèi)容(如byte[]
)。
2. 二進制文件流
定義:文件的原始數(shù)據(jù)以字節(jié)形式存儲,可以是任何類型的文件(如圖片、文檔等)。
獲取方式:
- 從輸入流讀?。ㄈ?
InputStream
)。 - 從數(shù)據(jù)庫中讀取二進制數(shù)據(jù)。
- 直接從文件系統(tǒng)中讀取。
二、編碼過程
1.引入spring依賴
//引入spring-test依賴 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.8</version> </dependency>
2.寫方法
private String extracted(byte[] data) { //創(chuàng)建緩存輸出流 BufferedOutputStream bos = null; // FileOutputStream流是指文件字節(jié)輸出流,專用于輸出原始字節(jié)流如圖像數(shù)據(jù)等,其繼承OutputStream類,擁有輸出流的基本特性 FileOutputStream fos = null; //創(chuàng)建文件對象 File file = null; String fileName1 = "1.png"; String filePath = System.getProperty("user.dir") + File.separator + File.separator; try { File file1 = new File(filePath); if (!file1.exists() && file1.isDirectory()) { file1.mkdirs(); } // file 進行賦值 file = new File(filePath + "\\" + fileName1); fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); //將data寫入文件中 bos.write(data); } catch (IOException e) { e.printStackTrace(); } finally { if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } //創(chuàng)建 MockMultipartFile 對象 MockMultipartFile implements MultipartFile MockMultipartFile mockMultipartFile = null; try { //將本地的文件 轉(zhuǎn)換為輸出流 進行 轉(zhuǎn)格式 FileInputStream inputStream = new FileInputStream(file); //通過 MockMultipartFile 帶參構(gòu)造進行 創(chuàng)建對象 及賦值 mockMultipartFile = new MockMultipartFile(file.getName(), file.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream); } catch (IOException e) { e.printStackTrace(); }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
一次由Lombok的@AllArgsConstructor注解引發(fā)的錯誤及解決
這篇文章主要介紹了一次由Lombok的@AllArgsConstructor注解引發(fā)的錯誤及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09Java Socket+多線程實現(xiàn)多人聊天室功能
這篇文章主要為大家詳細介紹了Java Socket+多線程實現(xiàn)多人聊天室功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07SpringBoot RESTful風(fēng)格入門講解
RESTful是一種web軟件風(fēng)格,它不是標(biāo)準(zhǔn)也不是協(xié)議,它不一定要采用,只是一種風(fēng)格,它倡導(dǎo)的是一個資源定位(url)及資源操作的風(fēng)格,這篇文章主要介紹了SpringBoot使用RESTful接口2022-11-11Java編程一維數(shù)組轉(zhuǎn)換成二維數(shù)組實例代碼
這篇文章主要介紹了Java編程一維數(shù)組轉(zhuǎn)換成二維數(shù)組,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-01-01利用Spring boot如何創(chuàng)建簡單的web交互應(yīng)用
這篇文章主要介紹了利用Spring boot如何創(chuàng)建簡單的web交互應(yīng)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-04-04springboot實現(xiàn)excel表格導(dǎo)出幾種常見方法
在日常的開發(fā)中避免不了操作Excel,下面這篇文章主要給大家介紹了關(guān)于springboot實現(xiàn)excel表格導(dǎo)出的幾種常見方法,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2023-11-11