Java图片压缩并上传
- public void uploadFile(@RequestParam(value = "file", required = false)MultipartFile[] file,HttpServletRequest request, HttpServletResponse response) throws IOException {
- //附件上传
- for (MultipartFile multipartFile : file) {
- //获取文件名
- String fileName = multipartFile.getOriginalFilename();
- if(null!=fileName && !"".equals(fileName)){
- try {
- String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();//文件扩展名
- String newfilename = StringUtil.getUUID() + "." + fileExt; //生成uuid文件名
- String savePath = "upload/images/" + newfilename;
- File folder=new File(PropUtils.get("web.root")+"upload/images");
- if(!folder.exists()){
- folder.mkdirs();//如果文件夹不存在 创建文件夹
- }
- //上传文件
- // multipartFile.transferTo(new File(PropUtils.get("web.root") + savePath));
- //压缩文件
- ImageHelper.compress(multipartFile.getInputStream(), new File(PropUtils.get("web.root") + savePath), 700);
- //数据库保存图片路径信息
- } catch (Exception e) {
- e.printStackTrace();
- return;
- }
- }
- }
- }
- package com.xypt.utils;
- import java.awt.Graphics2D;
- import java.awt.Rectangle;
- import java.awt.RenderingHints;
- import java.awt.geom.AffineTransform;
- import java.awt.image.BufferedImage;
- import java.awt.image.ColorModel;
- import java.awt.image.WritableRaster;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import javax.imageio.ImageIO;
- /**
- * 图片工具类
- * @author
- * @description 图片压缩 截取
- *
- */
- public class ImageHelper {
- /**
- * 实现图像的等比缩放
- *
- * @param source
- * @param targetW
- * @param targetH
- * @return
- */
- private static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
- // targetW,targetH分别表示目标长和宽
- int type = source.getType();
- BufferedImage target = null;
- double sx = (double) targetW / source.getWidth();
- double sy = (double) targetH / source.getHeight();
- // 这里想实现在targetW,targetH范围内实现等比缩放。如果不需要等比缩放
- // 则将下面的if else语句注释即可
- if (sx < sy) {
- sx = sy;
- targetW = (int) (sx * source.getWidth());
- } else {
- sy = sx;
- targetH = (int) (sy * source.getHeight());
- }
- if (type == BufferedImage.TYPE_CUSTOM) { // handmade
- ColorModel cm = source.getColorModel();
- WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
- boolean alphaPremultiplied = cm.isAlphaPremultiplied();
- target = new BufferedImage(cm, raster, alphaPremultiplied, null);
- } else
- target = new BufferedImage(targetW, targetH, type);
- Graphics2D g = target.createGraphics();
- // smoother than exlax:
- g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
- g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
- g.dispose();
- return target;
- }
- /**
- * 实现图像的等比缩放和缩放后的截取, 处理成功返回true, 否则返回false
- *
- * @param inFilePath
- * 要截取文件的路径
- * @param outFilePath
- * 截取后输出的路径
- * @param width
- * 要截取宽度
- * @param hight
- * 要截取的高度
- * @throws Exception
- */
- public static boolean compress(String inFilePath, String outFilePath, int width, int hight) {
- boolean ret = false;
- File file = new File(inFilePath);
- File saveFile = new File(outFilePath);
- InputStream in = null;
- try {
- in = new FileInputStream(file);
- ret = compress(in, saveFile, width, hight);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- ret = false;
- } finally {
- if (null != in) {
- try {
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return ret;
- }
- /**
- * 实现图像的等比缩放和缩放后的截取, 处理成功返回true, 否则返回false
- *
- * @param in
- * 要截取文件流
- * @param outFilePath
- * 截取后输出的路径
- * @param width
- * 要截取宽度
- * @param hight
- * 要截取的高度
- * @throws Exception
- */
- public static boolean compress(InputStream in, File saveFile, int width, int hight) {
- // boolean ret = false;
- BufferedImage srcImage = null;
- try {
- srcImage = ImageIO.read(in);
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- if (width > 0 || hight > 0) {
- // 原图的大小
- int sw = srcImage.getWidth();
- int sh = srcImage.getHeight();
- // 如果原图像的大小小于要缩放的图像大小,直接将要缩放的图像复制过去
- if (sw > width && sh > hight) {
- srcImage = resize(srcImage, width, hight);
- } else {
- String fileName = saveFile.getName();
- String formatName = fileName.substring(fileName.lastIndexOf('.') + 1);
- try {
- ImageIO.write(srcImage, formatName, saveFile);
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- return true;
- }
- }
- // 缩放后的图像的宽和高
- int w = srcImage.getWidth();
- int h = srcImage.getHeight();
- // 如果缩放后的图像和要求的图像宽度一样,就对缩放的图像的高度进行截取
- if (w == width) {
- // 计算X轴坐标
- int x = 0;
- int y = h / 2 - hight / 2;
- try {
- saveSubImage(srcImage, new Rectangle(x, y, width, hight), saveFile);
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- }
- // 否则如果是缩放后的图像的高度和要求的图像高度一样,就对缩放后的图像的宽度进行截取
- else if (h == hight) {
- // 计算X轴坐标
- int x = w / 2 - width / 2;
- int y = 0;
- try {
- saveSubImage(srcImage, new Rectangle(x, y, width, hight), saveFile);
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- }
- return true;
- }
- /**
- * 实现图像的等比缩放和缩放后的截取, 处理成功返回true, 否则返回false
- *
- * @param in
- * 图片输入流
- * @param saveFile
- * 压缩后的图片输出流
- * @param proportion
- * 压缩后宽度
- * @throws Exception
- */
- public static boolean compress(InputStream in, File saveFile, float compress_width) {
- if (null == in || null == saveFile || compress_width < 1) {// 检查参数有效性
- // LoggerUtil.error(ImageHelper.class, "--invalid parameter, do
- // nothing!");
- return false;
- }
- BufferedImage srcImage = null;
- double multiple = 1.0d;
- try {
- srcImage = ImageIO.read(in);
- int original_width = srcImage.getWidth(); // 原始宽度
- if (original_width > compress_width) {
- multiple = original_width / compress_width; // 计算要达到指定宽度要缩放的倍数
- }
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- // 原图的大小
- int width = (int) (srcImage.getWidth() / multiple);
- int hight = (int) (srcImage.getHeight() / multiple);
- srcImage = resize(srcImage, width, hight);
- // 缩放后的图像的宽和高
- int w = srcImage.getWidth();
- int h = srcImage.getHeight();
- // 如果缩放后的图像和要求的图像宽度一样,就对缩放的图像的高度进行截取
- if (w == width) {
- // 计算X轴坐标
- int x = 0;
- int y = h / 2 - hight / 2;
- try {
- saveSubImage(srcImage, new Rectangle(x, y, width, hight), saveFile);
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- }
- // 否则如果是缩放后的图像的高度和要求的图像高度一样,就对缩放后的图像的宽度进行截取
- else if (h == hight) {
- // 计算X轴坐标
- int x = w / 2 - width / 2;
- int y = 0;
- try {
- saveSubImage(srcImage, new Rectangle(x, y, width, hight), saveFile);
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- }
- return true;
- }
- /**
- * 实现缩放后的截图
- *
- * @param image
- * 缩放后的图像
- * @param subImageBounds
- * 要截取的子图的范围
- * @param subImageFile
- * 要保存的文件
- * @throws IOException
- */
- private static void saveSubImage(BufferedImage image, Rectangle subImageBounds, File subImageFile) throws IOException {
- if (subImageBounds.x < 0 || subImageBounds.y < 0 || subImageBounds.width - subImageBounds.x > image.getWidth() || subImageBounds.height - subImageBounds.y > image.getHeight()) {
- // LoggerUtil.error(ImageHelper.class, "Bad subimage bounds");
- return;
- }
- BufferedImage subImage = image.getSubimage(subImageBounds.x, subImageBounds.y, subImageBounds.width, subImageBounds.height);
- String fileName = subImageFile.getName();
- String formatName = fileName.substring(fileName.lastIndexOf('.') + 1);
- ImageIO.write(subImage, formatName, subImageFile);
- }
- public static void main(String[] args) throws Exception {
- InputStream in = null;
- // 缩放后需要保存的路径
- File saveFile = new File("d:/3_0.jpg");
- try {
- // 原图片的路径
- in = new FileInputStream(new File("d:/3.jpg"));
- if (compress(in, saveFile, 700)) {
- System.out.println("图片压缩完成!");
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- in.close();
- }
- }
- }
Java图片压缩并上传相关推荐
- js图片压缩后上传方法,图片超过1M先进行压缩,然后再上传
js图片压缩后上传方法,图片超过1M先进行压缩,然后再上传 图片上传目录 js图片压缩后上传方法,图片超过1M先进行压缩,然后再上传 html代码 js代码 html代码 <input type ...
- Bmob+Luban(鲁班)压缩图片实现相册选择图片压缩后上传到Bmob后台Glide加载图片显示到本地
源代码已上传CSDN:https://download.csdn.net/download/qq_16519957/11068345 因为本章需要跟前面的知识结合起来看所以就做了一个前面链接方便大家查 ...
- 返回图片_Vue 图片压缩并上传至服务器
日常开发中经常会遇到上传图片的需求,随着手机的蓬勃发展,现在拍出来的照片分辨率越来越高,随之带来的问题就是图片占用空间越来越大,如果我们直接上传图片可能就会浪费很大一笔资源,本文主要讲解基于 Vue ...
- android 快速实现图片压缩与上传
由于最近项目更新功能比较的忙,也没时间去整理自己的知识点和管理自己的博客.在android对手机相册中的图片的压缩和上传到服务器上,这样的功能在每个app开发中都会有这样的需求.所以今天就对andro ...
- Vue 图片压缩并上传至服务器
本文主要讲解基于 Vue + Vant ,实现移动端图片选择,并用 Canvas 压缩图片,最后上传至服务器.还会封装一个工具类,方便直接调用. 一.工具类封装 废话不多说先上代码,封装一个 Comp ...
- Android的图片压缩并上传
Android开发中上传图片很常见,一般为了节省流量会进行压缩的操作,本篇记录一下压缩和上传的方法. 图片压缩的方法 : import java.io.ByteArrayOutputStream; i ...
- Android实现图片压缩并上传到服务器
最近公司又叫开发了一个新项目,这个项目中上传图片用的蛮多的,于是整理一下,记录自己的心得体验 刚入手的时候,对于图片的大小还没有概念,(以前上传图片都是用户头像,对大小没什么要求),心想之间上传就是了 ...
- js图片压缩并上传?
js: var eleFile = document.querySelector('#file'); // 压缩图片需要的一些元素和对象 var reader = new FileReader(); ...
- java图片预览上传_java实现文件上传、下载、图片预览
这篇文章主要介绍了java实现文件上传.下载.图片预览,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 多文件保存到本地: @ResponseBody ...
最新文章
- Beta阶段第二次冲刺
- Global Average Pooling对全连接层的可替代性分析
- 用ajax控件作的高级搜索
- 分行与支行有什么区别
- 牛客OI周赛2-提高组
- Myeclipse下使用Maven搭建spring boot项目(第二篇)
- ubuntu 18.04 使用 nvm 安装 nodejs
- 高等数学张宇18讲 第十讲 多元函数微分学
- android卡片 弹簧滑动,一种通用式弹簧卡扣的制作方法
- 二维vector初始化方法
- jquery蝴蝶飞舞网页动画js特效代码
- ReportNG测试报告的定制修改
- 升级win10系统后出现语言乱码怎么办,如何解决乱码问题?
- 学计算机的是不是都非常木讷,北大学神韦东奕​是正常人吗?内向木讷是缺点​,拿不出手?​...
- 2个Python学习网站制作教程
- 程序员面试时应该知道的福利待遇
- Debian 8桌面安装Nvidia GTX960显卡驱动
- POSIX 是什么?让我们听听 Richard Stallman 的诠释
- 苏州大学计算机科学专业排名,2019苏州大学专业排名
- 学习C语言,困难吗?
热门文章
- 黑苹果 更换引导文件到硬盘
- visualvm 字体太小解决方案
- rtk打点,导入arcgis并进行格式变换
- 在线购物系统1.1分析类图
- pcie总线与cpci总线_一种基于CPCI与CPCIE总线的多功能背板_2010205852433_说明书_专利查询_专利网_钻瓜专利网...
- 洛谷题解 P1713 【麦当劳叔叔的难题】
- Nginx:Nginx添加SSL实现HTTPS访问
- react快速框架dva搭建项目架构
- jstorm 读取mysql_jstorm集成kafka
- mysql制作评论功能_Java+MySQL实现评论功能设计开发