将二进制文件流转化为MockMultipartFile 文件
·
一、名词解释及业务解释
1.具体业务流程
-
获取二进制文件流:
- 文件可以来自多个来源,如用户上传的文件、数据库存储的二进制数据、文件系统中的文件等。
- 读取文件内容,转换为字节数组。
-
创建
MockMultipartFile对象:- 使用
MockMultipartFile类,将字节数组、文件名和文件类型作为参数传入构造函数。 MockMultipartFile是 Spring 提供的一个类,主要用于在测试中模拟文件上传。
- 使用
-
处理上传逻辑:
- 将
MockMultipartFile传递给业务逻辑层,进行文件的保存、处理或解析等操作。 - 你可以在控制器中定义文件上传的接口,处理接收到的文件。
- 将
-
返回响应:
- 根据业务处理结果,返回相应的响应信息给客户端。
2.转换对象解释
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); } } - 构造参数:
name: 表单中对应的字段名(如"file")。originalFilename: 上传文件的原始文件名(如"test.txt")。contentType: 文件的内容类型(如"text/plain")。content: 文件的字节内容(如byte[])。
2. 二进制文件流
- 定义:文件的原始数据以字节形式存储,可以是任何类型的文件(如图片、文档等)。
- 获取方式:
- 从输入流读取(如
InputStream)。 - 从数据库中读取二进制数据。
- 直接从文件系统中读取。
- 从输入流读取(如
二、编码过程
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) {
//创建缓存输出流
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();
}更多推荐



所有评论(0)