一、名词解释及业务解释
1.具体业务流程
获取二进制文件流:
- 文件可以来自多个来源,如用户上传的文件、数据库存储的二进制数据、文件系统中的文件等。
- 读取文件内容,转换为字节数组。
创建 MockMultipartFile
对象:
- 使用
MockMultipartFile
类,将字节数组、文件名和文件类型作为参数传入构造函数。 -
MockMultipartFile
是 Spring 提供的一个类,主要用于在测试中模拟文件上传。
处理上传逻辑:
- 将
MockMultipartFile
传递给业务逻辑层,进行文件的保存、处理或解析等操作。 - 你可以在控制器中定义文件上传的接口,处理接收到的文件。
返回响应:
- 根据业务处理结果,返回相应的响应信息给客户端。
2.转换对象解释
1. MockMultipartFile
功能:用于模拟文件上传场景,特别是在测试中非常有用。
具体源码:
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | 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); } } |
构造参数:
-
name
: 表单中对应的字段名(如"file"
)。 -
originalFilename
: 上传文件的原始文件名(如"test.txt"
)。 -
contentType
: 文件的内容类型(如"text/plain"
)。 -
content
: 文件的字节内容(如byte[]
)。
2. 二进制文件流
定义:文件的原始数据以字节形式存储,可以是任何类型的文件(如图片、文档等)。
获取方式:
- 从输入流读取(如
InputStream
)。 - 从数据库中读取二进制数据。
- 直接从文件系统中读取。
二、编码过程
1.引入spring依赖
1 2 | //引入spring-test依赖 org.springframeworkspring-test5.3.8 |
2.写方法
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 43 44 45 46 47 48 49 | private String extracted( byte [] data) { //创建缓存输出流 BufferedOutputStream bos = null ; // FileOutputStream流是指文件字节输出流,专用于输出原始字节流如图像数据等,其继承OutputStream类,拥有输出流的基本特性 FileOutputStream fos = null ; //创建文件对象 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(); } } } //创建 MockMultipartFile 对象 MockMultipartFile implements MultipartFile MockMultipartFile mockMultipartFile = null ; try { //将本地的文件 转换为输出流 进行 转格式 FileInputStream inputStream = new FileInputStream(file); //通过 MockMultipartFile 带参构造进行 创建对象 及赋值 mockMultipartFile = new MockMultipartFile(file.getName(), file.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream); } catch (IOException e) { e.printStackTrace(); } |
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持IT俱乐部。