我有一个问题。我想将BufferedImage
转换为MultipartFile
。
首先,在我的UI上,我将base64
发送到服务器,然后在服务器上,我将其转换为BufferedImage
,之后我想将BufferedImage
转换为MultipartFile
并保存在本地存储中。
这是我的方法:
@PostMapping("/saveCategory")
@ResponseStatus(HttpStatus.OK)
public void createCategory(@RequestBody String category ) {
BufferedImage image = null;
OutputStream stream;
byte[] imageByte;
try {
BASE64Decoder decoder = new BASE64Decoder();
imageByte = decoder.decodeBuffer(category);
ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
image = ImageIO.read(bis);
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
String fileName = fileStorageService.storeFile(image );
我的存储方法:
public String storeFile(MultipartFile file) {
// Normalize file name
String fileName = StringUtils.cleanPath(file.getOriginalFilename());
try {
// Check if the file's name contains invalid characters
if (fileName.contains("..")) {
throw new FileStorageException("Sorry! Filename contains invalid path sequence " + fileName);
}
// Copy file to the target location (Replacing existing file with the same name)
Path targetLocation = this.fileStorageLocation.resolve(fileName);
Files.copy(file.getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING);
return fileName;
} catch (IOException ex) {
System.out.println(ex);
throw new FileStorageException("Could not store file " + fileName + ". Please try again!", ex);
}
}
MultipartFile
参数被注释掉了? - undefinedbase64
文件转换为MultipartFile。这可行吗? - undefined