java对图片按照指定尺寸压缩

news/2024/7/5 3:41:02

1.pom.xml引入依赖

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

2.接口

/*** 上传人员头像* @param multipartFile* @return*/@Log(title = "人员头像", businessType = BusinessType.UPDATE)@PostMapping("/avatar")@SneakyThrowspublic AjaxResult avatar(@RequestParam("multipartFile") MultipartFile multipartFile){if (!multipartFile.isEmpty()){String fileName = FileUploadUtils.extractFilename(multipartFile);byte[] bytes = multipartFile.getBytes();bytes = ThumbnailUtis.compressPictureForScale(bytes,92,128);//将字节数组写入到文件String filePath= RuoYiConfig.getAvatarPath()+"/"+fileName;FileUtils.writeByteArrayToFile(new File(filePath),bytes);//上传压缩后的图片//String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file);String url=FileUploadUtils.getPathFileName(RuoYiConfig.getAvatarPath(),fileName);log.info("url={}",url);AjaxResult ajax = AjaxResult.success();ajax.put("url", url);return ajax;}return AjaxResult.error("上传头像异常,请联系管理员");}

3.工具类

/*** 文件压缩处理类** @author xiaoss* @since 1.0, 2022年05月26日 17:32:12*/
@Slf4j
public class ThumbnailUtis {/*** 根据指定尺寸压缩图片** @param imageBytes  源图片字节数组* @param width* @param hight* @return 压缩质量后的图片字节数组*/@SneakyThrowspublic static byte[] compressPictureForScale(byte[] imageBytes,int width,int hight) {if (imageBytes == null || imageBytes.length <= 0) {return imageBytes;}ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);ByteArrayOutputStream outputStream = new ByteArrayOutputStream(imageBytes.length);Thumbnails.of(inputStream).size(width,hight).keepAspectRatio(false).toOutputStream(outputStream);imageBytes = outputStream.toByteArray();return imageBytes;}}


http://lihuaxi.xjx100.cn/news/244076.html

相关文章

Linux man命令

【功能说明】&#xff1a;详细记录着命令或文件的操作说明&#xff08;manual&#xff09;【语法格式】&#xff1a;man [命令|文件名]【实践操作】&#xff1a;man ls常用代号说明代号 代表内容1User Commands&#xff08;一般用户可使用的命令&#xff1a;ls&#xff09;4Lin…

runtime实践之Method Swizzling

利用 Objective-C 的 Runtime 特性&#xff0c;我们可以给语言做扩展&#xff0c;帮助解决项目开发中的一些设计和技术问题。这一篇&#xff0c;我们来探索一些利用 Objective-C Runtime 的黑色技巧。这些技巧中最具争议的或许就是 Method Swizzling 。 介绍一个技巧&#xff0…

适用于Mac上的SQL Server

适用于Mac上的SQL Server&#xff1f; 众所周知&#xff0c;很多人都在电脑上安装了SQL Server软件&#xff0c;普通用户直接去官网下载安装即可&#xff0c;Mac用户则该如何在Mac电脑上安装SQL Server呢&#xff1f;想要一款适用于Mac上的SQL Server&#xff1f;点击进入&…

SpringBoot+Vue实现学生管理系统

1.技术架构 前后端分离项目&#xff0c;后台&#xff1a;springBootmysqlmybatis 前端&#xff1a;vue框架 2.安装环境 IDEA/eclipse均可 jdk1.7或1.8 maven项目 mysql数据库版本为8.0.17 node 3.功能说明 用户角色&#xff1a;学生、家长或教师、管理员 学生&#…

Python开发环境配置

好久没有写博客了&#xff0c;自从6月份毕业后&#xff0c;进入一家做书法、字画文化宣传的互联网公司&#xff08;www.manyiaby.com&#xff09;&#xff0c;这段时间一直在进行前端开发&#xff0c;对于后端的使用很少了&#xff0c;整天都是什么html、css、javascript、jque…

hdu 1247

Problem DescriptionA hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.You are to find all the hat’s words in a dictionary.InputStandard input consists of a number of lowercase words, one per li…

从虚拟化前端Bug学习分析Kernel Dump

前言 也许大家都知道&#xff0c;分析 Kernel Dump 有个常用的工具叫 Crash&#xff0c;在我刚开始学习分析 Kernel Dump 的时候&#xff0c;总是花大量的时间折腾这个工具的用法&#xff0c;却总是记不住这个工具的功能。后来有一次在参加某次内部分享的时候&#xff0c;有位大…

SpringBoot默认包扫描问题

SpringBootApplication注解默认扫描路径是&#xff1a; 自动扫描主程序所在包及其下面的所有子包里面的组件 在maven多模块项目中&#xff0c;如果想让扫描到&#xff0c;需要在子模块下面创建相同的包 如&#xff1a; 如果包名不同就需要使用ComponentScan注解来扫描 但是…