springMVC上传图片并压缩

今天IPad客户端突然找到我说IPad界面加载图片时内存暴增,需要我把图片压缩一下,在看了一些博客之后决定用谷歌的Thumbnailator,为啥?

压缩像素可调节、一行代码、压缩快,废话不多说来看看实现方式:

1、添加maven依赖

<dependency>
    <groupId>net.coobird</groupId>
    <artifactId>thumbnailator</artifactId>
    <version>0.4.8</version>
</dependency>


2、压缩代码

private Map<String, Object> saveFileAndThumbnail(MultipartFile file) throws BusinessException {  
        // 判断文件是否为空  
        if (!file.isEmpty()) {
            String root = fileUploadPath;
            String picturePath = "我是保存路径啊哈哈哈哈";
            String ext = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            String newName = new Date().getTime()+ext;
            File filedict = new File(root+picturePath);
            if(!filedict.exists()){
                filedict.mkdirs();
            }
            File targetFile=new File(root+picturePath+File.separator+newName);
            File newFile=new File(root+picturePath+File.separator+"s"+newName);
            try {
                
                file.transferTo(targetFile);
                Thumbnails.of(targetFile)
                .scale(0.1f)//指定图片大小    0-1f  1f是原图
                .outputQuality(1f)//图片质量  0-1f  1f是原图
                .toFile(newFile);
                BufferedImage bimg = ImageIO.read(targetFile);
                int width = bimg.getWidth();
                int height = bimg.getHeight();
                BufferedImage thumbnail = ImageIO.read(newFile);
                int thumbnailWidth = thumbnail.getWidth();
                int thumbnailHeight = thumbnail.getHeight();
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("pictureHeight",height);
                map.put("pictureWidth",width);
                map.put("picturePath",picturePath+"/"+newName);
                map.put("thumbnailHeight",thumbnailHeight);
                map.put("thumbnailWidth",thumbnailWidth);
                map.put("thumbnailPath",picturePath+"/s"+newName);
                return map;
            } catch (IllegalStateException e) {
                e.printStackTrace();
                throw new BusinessException("图片上传失败");
            } catch (IOException e) {
                e.printStackTrace();
                throw new BusinessException("图片上传失败");
            } catch (Exception e) {
                e.printStackTrace();
                throw new BusinessException("图片上传失败");
            }
        }
        return null;
    }


Map前面三个字段显示的是原图宽高和路径,后三个字段显示的是压缩后的图的宽高路径

 

转自:https://blog.csdn.net/qq_39019735/article/details/82687244

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Spring MVC中实现图片上传的步骤如下: 1. 在后台Controller中添加一个处理图片上传的方法,使用`@RequestMapping`注解指定请求路径。方法参数中可以使用`MultipartFile`类型的参数来接收上传的文件。在方法中可以对文件进行处理,比如获取文件的原始名称、存储文件等操作。最后,可以返回一个信息来表示上传成功或失败。\[1\] 2. 在前端页面中添加一个上传图片的按钮和文件域。可以使用HTML的`<form>`标签来创建一个表单,设置`enctype="multipart/form-data"`属性来支持文件上传。使用`<input type="file" name="upload"/>`来创建文件域,用户可以选择要上传的文件。最后,使用`<input type="submit" value="上传"/>`来创建一个提交按钮,用户点击该按钮后触发文件上传操作。\[2\] 3. 在Spring MVC的配置文件中添加文件上传的配置。可以使用`CommonsMultipartResolver`类来处理文件上传,设置`maxUploadSize`属性来限制上传文件的大小。同时,可以使用`<mvc:resources>`标签来配置图片文件夹的路径,以便地址栏可以正常访问。\[3\] 以上是实现Spring MVC图片上传的基本步骤。具体的实现细节可以根据项目需求进行调整。 #### 引用[.reference_title] - *1* *3* [Java springMVC上传图片](https://blog.csdn.net/weixin_44084713/article/details/113942251)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [适用SpringMVC实现图片上传功能](https://blog.csdn.net/l_zl2021/article/details/127242717)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值