1. public void uploadFile(@RequestParam(value = "file", required = false)MultipartFile[] file,HttpServletRequest request, HttpServletResponse response) throws IOException {
  2. //附件上传
  3. for (MultipartFile multipartFile : file) {
  4. //获取文件名
  5. String fileName = multipartFile.getOriginalFilename();
  6. if(null!=fileName && !"".equals(fileName)){
  7. try {
  8. String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();//文件扩展名
  9. String newfilename = StringUtil.getUUID() + "." + fileExt; //生成uuid文件名
  10. String savePath = "upload/images/" + newfilename;
  11. File folder=new File(PropUtils.get("web.root")+"upload/images");
  12. if(!folder.exists()){
  13. folder.mkdirs();//如果文件夹不存在 创建文件夹
  14. }
  15. //上传文件
  16. //                  multipartFile.transferTo(new File(PropUtils.get("web.root") + savePath));
  17. //压缩文件
  18. ImageHelper.compress(multipartFile.getInputStream(), new File(PropUtils.get("web.root") + savePath), 700);
  19. //数据库保存图片路径信息
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. return;
  23. }
  24. }
  25. }
  26. }
[java] view plaincopy
  1. package com.xypt.utils;
  2. import java.awt.Graphics2D;
  3. import java.awt.Rectangle;
  4. import java.awt.RenderingHints;
  5. import java.awt.geom.AffineTransform;
  6. import java.awt.image.BufferedImage;
  7. import java.awt.image.ColorModel;
  8. import java.awt.image.WritableRaster;
  9. import java.io.File;
  10. import java.io.FileInputStream;
  11. import java.io.FileNotFoundException;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import javax.imageio.ImageIO;
  15. /**
  16. * 图片工具类
  17. * @author
  18. * @description 图片压缩 截取
  19. *
  20. */
  21. public class ImageHelper {
  22. /**
  23. * 实现图像的等比缩放
  24. *
  25. * @param source
  26. * @param targetW
  27. * @param targetH
  28. * @return
  29. */
  30. private static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
  31. // targetW,targetH分别表示目标长和宽
  32. int type = source.getType();
  33. BufferedImage target = null;
  34. double sx = (double) targetW / source.getWidth();
  35. double sy = (double) targetH / source.getHeight();
  36. // 这里想实现在targetW,targetH范围内实现等比缩放。如果不需要等比缩放
  37. // 则将下面的if else语句注释即可
  38. if (sx < sy) {
  39. sx = sy;
  40. targetW = (int) (sx * source.getWidth());
  41. } else {
  42. sy = sx;
  43. targetH = (int) (sy * source.getHeight());
  44. }
  45. if (type == BufferedImage.TYPE_CUSTOM) { // handmade
  46. ColorModel cm = source.getColorModel();
  47. WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
  48. boolean alphaPremultiplied = cm.isAlphaPremultiplied();
  49. target = new BufferedImage(cm, raster, alphaPremultiplied, null);
  50. } else
  51. target = new BufferedImage(targetW, targetH, type);
  52. Graphics2D g = target.createGraphics();
  53. // smoother than exlax:
  54. g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
  55. g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
  56. g.dispose();
  57. return target;
  58. }
  59. /**
  60. * 实现图像的等比缩放和缩放后的截取, 处理成功返回true, 否则返回false
  61. *
  62. * @param inFilePath
  63. *            要截取文件的路径
  64. * @param outFilePath
  65. *            截取后输出的路径
  66. * @param width
  67. *            要截取宽度
  68. * @param hight
  69. *            要截取的高度
  70. * @throws Exception
  71. */
  72. public static boolean compress(String inFilePath, String outFilePath, int width, int hight) {
  73. boolean ret = false;
  74. File file = new File(inFilePath);
  75. File saveFile = new File(outFilePath);
  76. InputStream in = null;
  77. try {
  78. in = new FileInputStream(file);
  79. ret = compress(in, saveFile, width, hight);
  80. } catch (FileNotFoundException e) {
  81. e.printStackTrace();
  82. ret = false;
  83. } finally {
  84. if (null != in) {
  85. try {
  86. in.close();
  87. } catch (IOException e) {
  88. e.printStackTrace();
  89. }
  90. }
  91. }
  92. return ret;
  93. }
  94. /**
  95. * 实现图像的等比缩放和缩放后的截取, 处理成功返回true, 否则返回false
  96. *
  97. * @param in
  98. *            要截取文件流
  99. * @param outFilePath
  100. *            截取后输出的路径
  101. * @param width
  102. *            要截取宽度
  103. * @param hight
  104. *            要截取的高度
  105. * @throws Exception
  106. */
  107. public static boolean compress(InputStream in, File saveFile, int width, int hight) {
  108. // boolean ret = false;
  109. BufferedImage srcImage = null;
  110. try {
  111. srcImage = ImageIO.read(in);
  112. } catch (IOException e) {
  113. e.printStackTrace();
  114. return false;
  115. }
  116. if (width > 0 || hight > 0) {
  117. // 原图的大小
  118. int sw = srcImage.getWidth();
  119. int sh = srcImage.getHeight();
  120. // 如果原图像的大小小于要缩放的图像大小,直接将要缩放的图像复制过去
  121. if (sw > width && sh > hight) {
  122. srcImage = resize(srcImage, width, hight);
  123. } else {
  124. String fileName = saveFile.getName();
  125. String formatName = fileName.substring(fileName.lastIndexOf('.') + 1);
  126. try {
  127. ImageIO.write(srcImage, formatName, saveFile);
  128. } catch (IOException e) {
  129. e.printStackTrace();
  130. return false;
  131. }
  132. return true;
  133. }
  134. }
  135. // 缩放后的图像的宽和高
  136. int w = srcImage.getWidth();
  137. int h = srcImage.getHeight();
  138. // 如果缩放后的图像和要求的图像宽度一样,就对缩放的图像的高度进行截取
  139. if (w == width) {
  140. // 计算X轴坐标
  141. int x = 0;
  142. int y = h / 2 - hight / 2;
  143. try {
  144. saveSubImage(srcImage, new Rectangle(x, y, width, hight), saveFile);
  145. } catch (IOException e) {
  146. e.printStackTrace();
  147. return false;
  148. }
  149. }
  150. // 否则如果是缩放后的图像的高度和要求的图像高度一样,就对缩放后的图像的宽度进行截取
  151. else if (h == hight) {
  152. // 计算X轴坐标
  153. int x = w / 2 - width / 2;
  154. int y = 0;
  155. try {
  156. saveSubImage(srcImage, new Rectangle(x, y, width, hight), saveFile);
  157. } catch (IOException e) {
  158. e.printStackTrace();
  159. return false;
  160. }
  161. }
  162. return true;
  163. }
  164. /**
  165. * 实现图像的等比缩放和缩放后的截取, 处理成功返回true, 否则返回false
  166. *
  167. * @param in
  168. *            图片输入流
  169. * @param saveFile
  170. *            压缩后的图片输出流
  171. * @param proportion
  172. *            压缩后宽度
  173. * @throws Exception
  174. */
  175. public static boolean compress(InputStream in, File saveFile, float compress_width) {
  176. if (null == in || null == saveFile || compress_width < 1) {// 检查参数有效性
  177. // LoggerUtil.error(ImageHelper.class, "--invalid parameter, do
  178. // nothing!");
  179. return false;
  180. }
  181. BufferedImage srcImage = null;
  182. double multiple = 1.0d;
  183. try {
  184. srcImage = ImageIO.read(in);
  185. int original_width = srcImage.getWidth(); // 原始宽度
  186. if (original_width > compress_width) {
  187. multiple = original_width / compress_width; // 计算要达到指定宽度要缩放的倍数
  188. }
  189. } catch (IOException e1) {
  190. // TODO Auto-generated catch block
  191. e1.printStackTrace();
  192. }
  193. // 原图的大小
  194. int width = (int) (srcImage.getWidth() / multiple);
  195. int hight = (int) (srcImage.getHeight() / multiple);
  196. srcImage = resize(srcImage, width, hight);
  197. // 缩放后的图像的宽和高
  198. int w = srcImage.getWidth();
  199. int h = srcImage.getHeight();
  200. // 如果缩放后的图像和要求的图像宽度一样,就对缩放的图像的高度进行截取
  201. if (w == width) {
  202. // 计算X轴坐标
  203. int x = 0;
  204. int y = h / 2 - hight / 2;
  205. try {
  206. saveSubImage(srcImage, new Rectangle(x, y, width, hight), saveFile);
  207. } catch (IOException e) {
  208. e.printStackTrace();
  209. return false;
  210. }
  211. }
  212. // 否则如果是缩放后的图像的高度和要求的图像高度一样,就对缩放后的图像的宽度进行截取
  213. else if (h == hight) {
  214. // 计算X轴坐标
  215. int x = w / 2 - width / 2;
  216. int y = 0;
  217. try {
  218. saveSubImage(srcImage, new Rectangle(x, y, width, hight), saveFile);
  219. } catch (IOException e) {
  220. e.printStackTrace();
  221. return false;
  222. }
  223. }
  224. return true;
  225. }
  226. /**
  227. * 实现缩放后的截图
  228. *
  229. * @param image
  230. *            缩放后的图像
  231. * @param subImageBounds
  232. *            要截取的子图的范围
  233. * @param subImageFile
  234. *            要保存的文件
  235. * @throws IOException
  236. */
  237. private static void saveSubImage(BufferedImage image, Rectangle subImageBounds, File subImageFile) throws IOException {
  238. if (subImageBounds.x < 0 || subImageBounds.y < 0 || subImageBounds.width - subImageBounds.x > image.getWidth() || subImageBounds.height - subImageBounds.y > image.getHeight()) {
  239. // LoggerUtil.error(ImageHelper.class, "Bad subimage bounds");
  240. return;
  241. }
  242. BufferedImage subImage = image.getSubimage(subImageBounds.x, subImageBounds.y, subImageBounds.width, subImageBounds.height);
  243. String fileName = subImageFile.getName();
  244. String formatName = fileName.substring(fileName.lastIndexOf('.') + 1);
  245. ImageIO.write(subImage, formatName, subImageFile);
  246. }
  247. public static void main(String[] args) throws Exception {
  248. InputStream in = null;
  249. // 缩放后需要保存的路径
  250. File saveFile = new File("d:/3_0.jpg");
  251. try {
  252. // 原图片的路径
  253. in = new FileInputStream(new File("d:/3.jpg"));
  254. if (compress(in, saveFile, 700)) {
  255. System.out.println("图片压缩完成!");
  256. }
  257. } catch (Exception e) {
  258. e.printStackTrace();
  259. } finally {
  260. in.close();
  261. }
  262. }
  263. }

Java图片压缩并上传相关推荐

  1. js图片压缩后上传方法,图片超过1M先进行压缩,然后再上传

    js图片压缩后上传方法,图片超过1M先进行压缩,然后再上传 图片上传目录 js图片压缩后上传方法,图片超过1M先进行压缩,然后再上传 html代码 js代码 html代码 <input type ...

  2. Bmob+Luban(鲁班)压缩图片实现相册选择图片压缩后上传到Bmob后台Glide加载图片显示到本地

    源代码已上传CSDN:https://download.csdn.net/download/qq_16519957/11068345 因为本章需要跟前面的知识结合起来看所以就做了一个前面链接方便大家查 ...

  3. 返回图片_Vue 图片压缩并上传至服务器

    日常开发中经常会遇到上传图片的需求,随着手机的蓬勃发展,现在拍出来的照片分辨率越来越高,随之带来的问题就是图片占用空间越来越大,如果我们直接上传图片可能就会浪费很大一笔资源,本文主要讲解基于 Vue ...

  4. android 快速实现图片压缩与上传

    由于最近项目更新功能比较的忙,也没时间去整理自己的知识点和管理自己的博客.在android对手机相册中的图片的压缩和上传到服务器上,这样的功能在每个app开发中都会有这样的需求.所以今天就对andro ...

  5. Vue 图片压缩并上传至服务器

    本文主要讲解基于 Vue + Vant ,实现移动端图片选择,并用 Canvas 压缩图片,最后上传至服务器.还会封装一个工具类,方便直接调用. 一.工具类封装 废话不多说先上代码,封装一个 Comp ...

  6. Android的图片压缩并上传

    Android开发中上传图片很常见,一般为了节省流量会进行压缩的操作,本篇记录一下压缩和上传的方法. 图片压缩的方法 : import java.io.ByteArrayOutputStream; i ...

  7. Android实现图片压缩并上传到服务器

    最近公司又叫开发了一个新项目,这个项目中上传图片用的蛮多的,于是整理一下,记录自己的心得体验 刚入手的时候,对于图片的大小还没有概念,(以前上传图片都是用户头像,对大小没什么要求),心想之间上传就是了 ...

  8. js图片压缩并上传?

    js: var eleFile = document.querySelector('#file'); // 压缩图片需要的一些元素和对象 var reader = new FileReader(); ...

  9. java图片预览上传_java实现文件上传、下载、图片预览

    这篇文章主要介绍了java实现文件上传.下载.图片预览,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 多文件保存到本地: @ResponseBody ...

最新文章

  1. Beta阶段第二次冲刺
  2. Global Average Pooling对全连接层的可替代性分析
  3. 用ajax控件作的高级搜索
  4. 分行与支行有什么区别
  5. 牛客OI周赛2-提高组
  6. Myeclipse下使用Maven搭建spring boot项目(第二篇)
  7. ubuntu 18.04 使用 nvm 安装 nodejs
  8. 高等数学张宇18讲 第十讲 多元函数微分学
  9. android卡片 弹簧滑动,一种通用式弹簧卡扣的制作方法
  10. 二维vector初始化方法
  11. jquery蝴蝶飞舞网页动画js特效代码
  12. ReportNG测试报告的定制修改
  13. 升级win10系统后出现语言乱码怎么办,如何解决乱码问题?
  14. 学计算机的是不是都非常木讷,北大学神韦东奕​是正常人吗?内向木讷是缺点​,拿不出手?​...
  15. 2个Python学习网站制作教程
  16. 程序员面试时应该知道的福利待遇
  17. Debian 8桌面安装Nvidia GTX960显卡驱动
  18. POSIX 是什么?让我们听听 Richard Stallman 的诠释
  19. 苏州大学计算机科学专业排名,2019苏州大学专业排名
  20. 学习C语言,困难吗?

热门文章

  1. 黑苹果 更换引导文件到硬盘
  2. visualvm 字体太小解决方案
  3. rtk打点,导入arcgis并进行格式变换
  4. 在线购物系统1.1分析类图
  5. pcie总线与cpci总线_一种基于CPCI与CPCIE总线的多功能背板_2010205852433_说明书_专利查询_专利网_钻瓜专利网...
  6. 洛谷题解 P1713 【麦当劳叔叔的难题】
  7. Nginx:Nginx添加SSL实现HTTPS访问
  8. react快速框架dva搭建项目架构
  9. jstorm 读取mysql_jstorm集成kafka
  10. mysql制作评论功能_Java+MySQL实现评论功能设计开发