后端可以直接返回hashmap,不用再包一层对象

后端可以直接返回HashMap,不用外面再包对象
不行的话再@ResponseBody HashMap

好的,这里提供一个使用 Spring MVC 框架实现的图片上传和生成缩略图的示例代码,供你参考: ```java @Controller public class ImageController { @RequestMapping(value = "/upload", method = RequestMethod.POST) @ResponseBody public Map<String, Object> uploadImage(@RequestParam("file") MultipartFile file) { Map<String, Object> result = new HashMap<>(); if (!file.isEmpty()) { try { // 保存原始图片到服务器上的文件系统中 String originalFilename = file.getOriginalFilename(); String extensionName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1); String fileName = UUID.randomUUID().toString() + "." + extensionName; File destFile = new File("/path/to/save/image/" + fileName); file.transferTo(destFile); // 生成缩略图并保存到服务器上的文件系统中 BufferedImage originalImage = ImageIO.read(destFile); int width = originalImage.getWidth(); int height = originalImage.getHeight(); int scaledWidth = 100; int scaledHeight = (int) ((double) scaledWidth / width * height); BufferedImage scaledImage = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_RGB); Graphics2D g = scaledImage.createGraphics(); g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null); g.dispose(); String thumbnailFileName = UUID.randomUUID().toString() + "." + extensionName; File thumbnailFile = new File("/path/to/save/thumbnail/" + thumbnailFileName); ImageIO.write(scaledImage, extensionName, thumbnailFile); // 将缩略图路径记录到数据库中 String thumbnailUrl = "/thumbnail/" + thumbnailFileName; // TODO: 将 thumbnailUrl 存入数据库中 // 返回上传结果 result.put("success", true); result.put("message", "上传成功"); result.put("imageUrl", "/image/" + fileName); result.put("thumbnailUrl", thumbnailUrl); } catch (Exception e) { e.printStackTrace(); result.put("success", false); result.put("message", "上传失败:" + e.getMessage()); } } else { result.put("success", false); result.put("message", "上传失败:文件为空"); } return result; } } ``` 在这个示例中,我们使用了 Spring MVC 框架来处理文件上传请求,并使用 ImageIO 来生成缩略图。上传的图片文件会保存到 "/path/to/save/image/" 目录下,生成的缩略图会保存到 "/path/to/save/thumbnail/" 目录下,缩略图路径会存入数据库中。最终,我们会返回一个 JSON 对象,包含上传结果、原始图片路径和缩略图路径等信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值