所需要的jar包在:\jdk1.7.0_25\jre\lib\rt.jar里面

  1. package util;
  2.  
  3. import java.awt.Image;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9.  
  10. import javax.imageio.ImageIO;
  11.  
  12. import com.sun.image.codec.jpeg.JPEGCodec;
  13. import com.sun.image.codec.jpeg.JPEGEncodeParam;
  14. import com.sun.image.codec.jpeg.JPEGImageEncoder;
  15.  
  16. public class ImageZipUtil {
  17.  
  18. /**
  19. * 等比例压缩图片文件<br> 先保存原文件,再压缩、上传
  20. * @param oldFile 要进行压缩的文件
  21. * @param newFile 新文件
  22. * @param width 宽度 //设置宽度时(高度传入0,等比例缩放)
  23. * @param height 高度 //设置高度时(宽度传入0,等比例缩放)
  24. * @param quality 质量
  25. * @return 返回压缩后的文件的全路径
  26. */
  27. public static String zipImageFile(File oldFile,File newFile, int width, int height, float quality) {
  28. if (oldFile == null) {
  29. return null;
  30. }
  31. try {
  32. /** 对服务器上的临时文件进行处理 */
  33. Image srcFile = ImageIO.read(oldFile);
  34. int w = srcFile.getWidth(null);
  35. // System.out.println(w);
  36. int h = srcFile.getHeight(null);
  37. // System.out.println(h);
  38. double bili;
  39. if(width>0){
  40. bili=width/(double)w;
  41. height = (int) (h*bili);
  42. }else{
  43. if(height>0){
  44. bili=height/(double)h;
  45. width = (int) (w*bili);
  46. }
  47. }
  48. /** 宽,高设定 */
  49. BufferedImage tag = new BufferedImage(width, height,
  50. BufferedImage.TYPE_INT_RGB);
  51. tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);
  52. //String filePrex = oldFile.getName().substring(0, oldFile.getName().indexOf('.'));
  53. /** 压缩后的文件名 */
  54. //newImage = filePrex + smallIcon+ oldFile.getName().substring(filePrex.length());
  55.  
  56. /** 压缩之后临时存放位置 */
  57. FileOutputStream out = new FileOutputStream(newFile);
  58.  
  59. JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  60. JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
  61. /** 压缩质量 */
  62. jep.setQuality(quality, true);
  63. encoder.encode(tag, jep);
  64. out.close();
  65.  
  66. } catch (FileNotFoundException e) {
  67. e.printStackTrace();
  68. } catch (IOException e) {
  69. e.printStackTrace();
  70. }
  71. return newFile.getAbsolutePath();
  72. }
  73.  
  74. /**
  75. * 按宽度高度压缩图片文件<br> 先保存原文件,再压缩、上传
  76. * @param oldFile 要进行压缩的文件全路径
  77. * @param newFile 新文件
  78. * @param width 宽度
  79. * @param height 高度
  80. * @param quality 质量
  81. * @return 返回压缩后的文件的全路径
  82. */
  83. public static String zipWidthHeightImageFile(File oldFile,File newFile, int width, int height, float quality) {
  84. if (oldFile == null) {
  85. return null;
  86. }
  87. String newImage = null;
  88. try {
  89. /** 对服务器上的临时文件进行处理 */
  90. Image srcFile = ImageIO.read(oldFile);
  91. int w = srcFile.getWidth(null);
  92. // System.out.println(w);
  93. int h = srcFile.getHeight(null);
  94. // System.out.println(h);
  95.  
  96. /** 宽,高设定 */
  97. BufferedImage tag = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
  98. tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);
  99. //String filePrex = oldFile.substring(0, oldFile.indexOf('.'));
  100. /** 压缩后的文件名 */
  101. //newImage = filePrex + smallIcon+ oldFile.substring(filePrex.length());
  102.  
  103. /** 压缩之后临时存放位置 */
  104. FileOutputStream out = new FileOutputStream(newFile);
  105.  
  106. JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  107. JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
  108. /** 压缩质量 */
  109. jep.setQuality(quality, true);
  110. encoder.encode(tag, jep);
  111. out.close();
  112. } catch (FileNotFoundException e) {
  113. e.printStackTrace();
  114. } catch (IOException e) {
  115. e.printStackTrace();
  116. }
  117. return newImage;
  118. }
  119.  
  120. public static void main(String args[]) throws IOException {
  121. System.out.println(ImageZipUtil.zipWidthHeightImageFile(new File("E:/图片/1.jpg"),new File("E:/图片/1-1.jpg"), 120, 128, 3f));
  122. }
  123.  
  124. }

  

Java图片上传压缩处理的更多相关文章

  1. java图片上传(mvc)

    最近有开始学起了java,好久没写文章了,好久没来博客园了.最近看了看博客园上次写的图片上传有很多人看,今天在一些篇关于java图片上传的.后台接收用的是mvc.不墨迹了,直接上图. 先看目录结构.i ...

  2. vue移动端图片上传压缩

    上传压缩方法 import {api} from '../../api/api.js'; import axios from 'axios'; export function imgPreview ( ...

  3. js 利用iframe和location.hash跨域解决的方法,java图片上传回调JS函数跨域

    奶奶的:折腾了我二天,最终攻克了!网上有非常多样例. 但跟我的都不太一样,费话不多说了,上图   上代码: IE ,firefix,chrome 測试通过 js :这个主页面,部分代码, functi ...

  4. java 图片上传

    代码是最有力量的,嘎嘎 @CrossOrigin@ApiOperation(value = "上传图片", notes = "上传图片", httpMethod ...

  5. java图片上传,通过MultipartFile方式,如果后台获取null检查是否缺少步骤

    本方法基于springMvc 1.首先需要在webap下创建images 2.在springmvc.xml上引入 <bean id="multipartResolver" c ...

  6. java图片上传及图片回显1

    目的:选择图片,进行图片回显之后将图片保存到服务器上(PS:没有使用任何插件,样式很丑) 实现方式: js+servlet+jsp的方式来实现 事先准备: 文件上传处理在浏览器中是以流的形式提交到服务 ...

  7. UEditor之实现配置简单的图片上传示例

    UEditor之实现配置简单的图片上传示例 原创 2016年06月11日 18:27:31 开心一笑 下班后,阿华到楼下小超市买毛巾,刚买完出来,就遇到同一办公楼里另一家公司的阿菲,之前与她远远的有过 ...

  8. java多图片上传--前端实现预览--图片压缩 、图片缩放,区域裁剪,水印,旋转,保持比例。

    java多图片上传--前端实现预览 前端代码: https://pan.baidu.com/s/1cqKbmjBSXOhFX4HR1XGkyQ 解压后: java后台: <!--文件上传--&g ...

  9. 关于富文本编辑器—UEditor(java版)的使用,以及如何将UEditor的文件/图片上传路径改成绝对路径

    突然发现好久没写博客了,感觉变懒了,是要让自己养成经常写文章的习惯才行.既可以分享自己的所学,和所想,和大家一起讨论,发现自己的不足的问题. 大家可能经常会用到富文本编辑器,今天我要说的是UEdito ...

随机推荐

  1. android 电容屏(一):电容屏基本原理篇

    平台信息: 内核:linux3.4.39系统:android4.4 平台:S5P4418(cortex a9) 作者:瘋耔(欢迎转载,请注明作者) 欢迎指正错误,共同学习.共同进步!! 关注博主新浪博 ...

  2. 武汉北大青鸟解读2016年10大IT热门岗位

    武汉北大青鸟解读2016年10大IT热门岗位 2016年1月5日 13:37 北大青鸟 这是IT从业者的辉煌时代,IT行业的失业率正处在历史的低点,而且有的岗位——例如网络和安全工程师以及软件开发人员 ...

  3. URAL1501. Sense of Beauty(记忆化)

    链接 dfs+记忆化 对于当前状态虽然满足和差 但如果搜下去没有满足的情况也是不可以的 所以需要记忆化下 #include <iostream> #include<cstdio> ...

  4. 大四实习准备6_android服务

    2015-5-9 1.服务是什么 android四大组件之一,有一些特点: 1)服务的运行不依赖于用户界面,即使程序被切换到后台.或者用户打开了另外一个应用程序,服务仍然能够保持正常运行.(当对应的程 ...

  5. bzoj3157 3516

    太神了,被数学题虐了 orz http://m.blog.csdn.net/blog/skywalkert/43970331 这道题关键是抓住m较小的特点,构造递推解决 ; ..,..] of lon ...

  6. BZOJ_1018_[SHOI2008]_交通堵塞traffic_(线段树)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1018 \(2*n\)的距形,起初没有边相连,之后有三种操作: 1.加边. 2.删边. 3.询问 ...

  7. python auto send email

    /*************************************************************************** * python auto send emai ...

  8. windows2003远程桌面退出后系统自动注销的解决方法

    最近公司有一个奇怪的需求,意思是有一个网页,要时时的打开着.现在只有把这个网页在服务器上打开. 这样才能满足需求.但我在应用中遇见了个问题.我在服务器上打开网页后,关掉远程,过一会网页的运行效果就没有 ...

  9. UpYun上传 401 Unauthorized

    _upt=3b9b444a14059041252014-07-21 08:46:25,218 ERROR (com.UpYun:518) - Upload file error:<h1>4 ...

  10. 怎么学数学[How to Study Math]