当前位置:网站首页>项目中生成MultipartFile并调用其它服务
项目中生成MultipartFile并调用其它服务
2022-07-21 04:13:00 【起风哥】
微服务项目中服务调用需要生成multipartFile问题的几种解决方案
1、引入test包,使用test包中的MockMultipartFile
2、自定义实现一个MockMultipartFile(推荐)
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
/** * @Description TODO * @Author 姚仲杰 * @Date 2022/7/15 11:12 */
public class MockMultipartFile implements MultipartFile {
private final String name;
private String originalFilename;
@Nullable
private 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 null");
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));
}
@Override
public String getName() {
return this.name;
}
@Override
public String getOriginalFilename() {
return this.originalFilename;
}
@Override
@Nullable
public String getContentType() {
return this.contentType;
}
@Override
public boolean isEmpty() {
return this.content.length == 0;
}
@Override
public long getSize() {
return (long)this.content.length;
}
@Override
public byte[] getBytes() throws IOException {
return this.content;
}
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(this.content);
}
@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
FileCopyUtils.copy(this.content, dest);
}
}
边栏推荐
- Preparation of paclitaxel combined with 2-methoxyestradiol albumin nanoparticles / Piper longum amide albumin nanoparticles
- 蚓激酶白蛋白纳米粒/红细胞膜定向包裹血红蛋白-白蛋白纳米粒的研究制备
- OSPF Foundation
- Loop structure: while and do while structures
- 电磁场与电磁波实验三 熟悉Mathematica软件在电磁场领域的应用
- 元素类型为 “resultMap“ 的内容必须匹配 “(constructor?,id*,result*,association*,collection*,discriminato?)“
- 解决報錯:Uncaught TypeError: Cannot read properties of undefined (reading ‘install‘)
- 如何去除百度广告
- 【.net core】yisha框架动态加载表格表头
- sort内部实现原理
猜你喜欢
攻防世界----lottery
H5 customized sharing in wechat
Preparation of paclitaxel combined with 2-methoxyestradiol albumin nanoparticles / Piper longum amide albumin nanoparticles
MySQL (2)
Fcrp-d --- simulation questions on the official website of sail software, kettle module
OSPF Foundation
受众分析与卸载分析全面升级,HMS Core分析服务6.6.0版本上新
动态内存管理2之柔性数组
Lombok简化开发
Bram for FPGA logic resource evaluation (taking Xilinx as an example)
随机推荐
深度学习——(4)VGG16 图像分类
Verilog grammar basics HDL bits training 03
如何去除百度广告
Preparation of paclitaxel combined with 2-methoxyestradiol albumin nanoparticles / Piper longum amide albumin nanoparticles
受众分析与卸载分析全面升级,HMS Core分析服务6.6.0版本上新
2022 the hottest free Chinese version of the whole network - free sharing of "Application Tutorial of deep learning in image processing"
Software testing interview question: talk about your understanding of the two strategies of top-down integration and bottom-up integration in integration testing, and talk about their respective advan
Basic knowledge of trigger (I)
Classic examples of C language: 21-30 examples: insertion sort, Hill sort 1, quick sort, Hill sort 2, recursion, completion, Fibonacci sequence, common divisor and common multiple, judging the number
Deep learning - (4) vgg16 image classification
Find a single dog -- repeat the number twice to find a single number
Es custom analyzer
【揭秘】年薪100万的项目经理复盘秘诀
mysql安装提示应用程序无法正常启动(0xc000007b,如何解决?
使用JPofiler工具分析OOM原因
Liunx kills processes with the same name in batches
动作活体检测能力,构建安全可靠的支付级“刷脸”体验
Hcip day 4
26. Gd32f103c8t6 introductory tutorial -- use of quadrature encoder
Luogu p3313 [sdoi2014] Travel Solution