阅读各种教程时,我遇到了显示图像、返回字节数组的可能性。我需要显示结果图像,但作为回报,我在页面上得到一组字节数。我需要更改/添加哪些内容才能工作?
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody byte[] handleFileUpload(@RequestParam("file") MultipartFile file) {
try {
String extension = FilenameUtils.getExtension(file.getOriginalFilename()); //find the file extension
if (!extension.equals("jpg") && !extension.equals("jpeg") &&
!extension.equals("bmp") && !extension.equals("png")) throw new Exception();
InputStream in = new ByteArrayInputStream(file.getBytes());
BufferedImage bufferedImage= ImageIO.read(in);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, extension, os);
InputStream is = new ByteArrayInputStream(os.toByteArray());
return os.toByteArray();
} catch (Exception e) {
}
return new byte[0];
}
不要注意错误处理。刻意移除身体护理
发布于 2018-07-29 03:57:02
我将@RequestMappting produces = {"image/jpg, image/jpeg, image/png, image/bmp"}
添加到主体中,一切都正常了
https://stackoverflow.com/questions/51574481
复制相似问题