byte[]转file,file转MultipartFile

public static File byte2File(byte[] buf, String filePath, String fileName){
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 + File.separator + fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(buf);
}catch (Exception 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();
}
}
}
return file;
}

public static MultipartFile getMultipartFile(File file){
FileInputStream fileInputStream = null;
MultipartFile multipartFile = null;
try {
fileInputStream = new FileInputStream(file);
multipartFile = new MockMultipartFile(file.getName(),file.getName(),
ContentType.APPLICATION_OCTET_STREAM.toString(),fileInputStream);
} catch (Exception e) {
e.printStackTrace();
}
return multipartFile;
}
posted @ 2022-06-06 16:27  DarkerbeS  阅读(840)  评论(0编辑  收藏  举报