Junit 模拟 MockMultipartFile 文件测试
2023-09-14 09:06:15 时间
//str是图片base64编码
String str = "+a4g9dgl00KLjm7pLDL3Rkcum6Bh43dQfNog/F7quSXTUoLKia/NKa。。。。。"
String property = System.getProperty("user.dir");
Base64Util.base64StrToImage(str, property + "\\test.jpg");
File file = new File(property + "\\test.jpg");
FileInputStream fileInputStream = new FileInputStream(file);
MockMultipartFile mfile = new MockMultipartFile(file.getName(), fileInputStream);
mfile 就是模拟的 MockMultipartFile 文件了
/**
* base64编码字符串转换为图片
* @param imgStr base64编码字符串
* @param path 图片路径
* @return
*/
public static boolean base64StrToImage(String imgStr, String path) {
if (imgStr == null) {
return false;}
Base64.Decoder decoder = Base64.getDecoder();
try {
// 解密
byte[] b = decoder.decode(imgStr);
// 处理数据
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
//文件夹不存在则自动创建
File tempFile = new File(path);
if (!tempFile.getParentFile().exists()) {
tempFile.getParentFile().mkdirs();
}
OutputStream out = new FileOutputStream(tempFile);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
return false;
}
}
相关文章
- WinForm实现简单的拖拽文件到出题的功能(C#)(3)
- chm文件打开空白无内容
- Jmeter_前端RSA加密下的登陆模拟_引用js文件实现(转)
- g++: error trying to exec 'cc1plus': execvp: 没有那个文件或目录
- vue.js3: 使用全局css样式文件(vue@3.2.37)
- php解析mpp文件中的前置任务
- 关于实现一个基于文件持久化的EventStore的核心构思
- ML之FE:数据随机抽样之利用pandas的sample函数对超大样本的数据集进行随机采样,并另存为csv文件
- 成功解决Win系统下执行exe文件一闪而过的问题
- Http服务器实现文件上传与下载(五)
- 在J2EE的Web应用中,编译后的class文件存放的目录为(选择1项)
- Py经典案例:利用Python调用数据库历史记录文件,实现BTC、LTC等Encrypted currency找出最佳出仓价、收益比的加密币模拟交易系统