文件流 byte数组 base64加密 如何转换成MultipartFile类型返回
public class FileUtils {
public MultipartFile generateMultipartFile(String fileStr, String fileName, String filePath, Boolean decode) {
this.getFile(fileStr, filePath, fileName, decode);
File file = new File(filePath + fileName);
MultipartFile multipartFile = null;
try {
FileInputStream fileInputStream = new FileInputStream(file);
multipartFile = new MockMultipartFile(file.getName(), fileName, null, fileInputStream);
} catch (IOException e) {
log.info("异常信息:" + e);
}
return multipartFile;
}
/**
* 根据byte数组,生成文件
*/
private void getFile(String fileValue, String filePath, String fileName, Boolean decode) {
byte[] bfile = Base64.getDecoder().decode(fileValue);
if(null != decode && !decode){
bfile = fileValue.getBytes();
}
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
//判断文件目录是否存在
if (!dir.exists() && !dir.isDirectory()) {
dir.mkdirs();
}
file = new File(filePath + fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
都有Byte[]了,直接转file