Java图片上传压缩处理
所需要的jar包在:\jdk1.7.0_25\jre\lib\rt.jar里面
- package util;
- import java.awt.Image;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import com.sun.image.codec.jpeg.JPEGCodec;
- import com.sun.image.codec.jpeg.JPEGEncodeParam;
- import com.sun.image.codec.jpeg.JPEGImageEncoder;
- public class ImageZipUtil {
- /**
- * 等比例压缩图片文件<br> 先保存原文件,再压缩、上传
- * @param oldFile 要进行压缩的文件
- * @param newFile 新文件
- * @param width 宽度 //设置宽度时(高度传入0,等比例缩放)
- * @param height 高度 //设置高度时(宽度传入0,等比例缩放)
- * @param quality 质量
- * @return 返回压缩后的文件的全路径
- */
- public static String zipImageFile(File oldFile,File newFile, int width, int height, float quality) {
- if (oldFile == null) {
- return null;
- }
- try {
- /** 对服务器上的临时文件进行处理 */
- Image srcFile = ImageIO.read(oldFile);
- int w = srcFile.getWidth(null);
- // System.out.println(w);
- int h = srcFile.getHeight(null);
- // System.out.println(h);
- double bili;
- if(width>0){
- bili=width/(double)w;
- height = (int) (h*bili);
- }else{
- if(height>0){
- bili=height/(double)h;
- width = (int) (w*bili);
- }
- }
- /** 宽,高设定 */
- BufferedImage tag = new BufferedImage(width, height,
- BufferedImage.TYPE_INT_RGB);
- tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);
- //String filePrex = oldFile.getName().substring(0, oldFile.getName().indexOf('.'));
- /** 压缩后的文件名 */
- //newImage = filePrex + smallIcon+ oldFile.getName().substring(filePrex.length());
- /** 压缩之后临时存放位置 */
- FileOutputStream out = new FileOutputStream(newFile);
- JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
- JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
- /** 压缩质量 */
- jep.setQuality(quality, true);
- encoder.encode(tag, jep);
- out.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return newFile.getAbsolutePath();
- }
- /**
- * 按宽度高度压缩图片文件<br> 先保存原文件,再压缩、上传
- * @param oldFile 要进行压缩的文件全路径
- * @param newFile 新文件
- * @param width 宽度
- * @param height 高度
- * @param quality 质量
- * @return 返回压缩后的文件的全路径
- */
- public static String zipWidthHeightImageFile(File oldFile,File newFile, int width, int height, float quality) {
- if (oldFile == null) {
- return null;
- }
- String newImage = null;
- try {
- /** 对服务器上的临时文件进行处理 */
- Image srcFile = ImageIO.read(oldFile);
- int w = srcFile.getWidth(null);
- // System.out.println(w);
- int h = srcFile.getHeight(null);
- // System.out.println(h);
- /** 宽,高设定 */
- BufferedImage tag = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
- tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);
- //String filePrex = oldFile.substring(0, oldFile.indexOf('.'));
- /** 压缩后的文件名 */
- //newImage = filePrex + smallIcon+ oldFile.substring(filePrex.length());
- /** 压缩之后临时存放位置 */
- FileOutputStream out = new FileOutputStream(newFile);
- JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
- JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
- /** 压缩质量 */
- jep.setQuality(quality, true);
- encoder.encode(tag, jep);
- out.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return newImage;
- }
- public static void main(String args[]) throws IOException {
- System.out.println(ImageZipUtil.zipWidthHeightImageFile(new File("E:/图片/1.jpg"),new File("E:/图片/1-1.jpg"), 120, 128, 3f));
- }
- }
Java图片上传压缩处理的更多相关文章
- java图片上传(mvc)
最近有开始学起了java,好久没写文章了,好久没来博客园了.最近看了看博客园上次写的图片上传有很多人看,今天在一些篇关于java图片上传的.后台接收用的是mvc.不墨迹了,直接上图. 先看目录结构.i ...
- vue移动端图片上传压缩
上传压缩方法 import {api} from '../../api/api.js'; import axios from 'axios'; export function imgPreview ( ...
- js 利用iframe和location.hash跨域解决的方法,java图片上传回调JS函数跨域
奶奶的:折腾了我二天,最终攻克了!网上有非常多样例. 但跟我的都不太一样,费话不多说了,上图 上代码: IE ,firefix,chrome 測试通过 js :这个主页面,部分代码, functi ...
- java 图片上传
代码是最有力量的,嘎嘎 @CrossOrigin@ApiOperation(value = "上传图片", notes = "上传图片", httpMethod ...
- java图片上传,通过MultipartFile方式,如果后台获取null检查是否缺少步骤
本方法基于springMvc 1.首先需要在webap下创建images 2.在springmvc.xml上引入 <bean id="multipartResolver" c ...
- java图片上传及图片回显1
目的:选择图片,进行图片回显之后将图片保存到服务器上(PS:没有使用任何插件,样式很丑) 实现方式: js+servlet+jsp的方式来实现 事先准备: 文件上传处理在浏览器中是以流的形式提交到服务 ...
- UEditor之实现配置简单的图片上传示例
UEditor之实现配置简单的图片上传示例 原创 2016年06月11日 18:27:31 开心一笑 下班后,阿华到楼下小超市买毛巾,刚买完出来,就遇到同一办公楼里另一家公司的阿菲,之前与她远远的有过 ...
- java多图片上传--前端实现预览--图片压缩 、图片缩放,区域裁剪,水印,旋转,保持比例。
java多图片上传--前端实现预览 前端代码: https://pan.baidu.com/s/1cqKbmjBSXOhFX4HR1XGkyQ 解压后: java后台: <!--文件上传--&g ...
- 关于富文本编辑器—UEditor(java版)的使用,以及如何将UEditor的文件/图片上传路径改成绝对路径
突然发现好久没写博客了,感觉变懒了,是要让自己养成经常写文章的习惯才行.既可以分享自己的所学,和所想,和大家一起讨论,发现自己的不足的问题. 大家可能经常会用到富文本编辑器,今天我要说的是UEdito ...
随机推荐
- android 电容屏(一):电容屏基本原理篇
平台信息: 内核:linux3.4.39系统:android4.4 平台:S5P4418(cortex a9) 作者:瘋耔(欢迎转载,请注明作者) 欢迎指正错误,共同学习.共同进步!! 关注博主新浪博 ...
- 武汉北大青鸟解读2016年10大IT热门岗位
武汉北大青鸟解读2016年10大IT热门岗位 2016年1月5日 13:37 北大青鸟 这是IT从业者的辉煌时代,IT行业的失业率正处在历史的低点,而且有的岗位——例如网络和安全工程师以及软件开发人员 ...
- URAL1501. Sense of Beauty(记忆化)
链接 dfs+记忆化 对于当前状态虽然满足和差 但如果搜下去没有满足的情况也是不可以的 所以需要记忆化下 #include <iostream> #include<cstdio> ...
- 大四实习准备6_android服务
2015-5-9 1.服务是什么 android四大组件之一,有一些特点: 1)服务的运行不依赖于用户界面,即使程序被切换到后台.或者用户打开了另外一个应用程序,服务仍然能够保持正常运行.(当对应的程 ...
- bzoj3157 3516
太神了,被数学题虐了 orz http://m.blog.csdn.net/blog/skywalkert/43970331 这道题关键是抓住m较小的特点,构造递推解决 ; ..,..] of lon ...
- BZOJ_1018_[SHOI2008]_交通堵塞traffic_(线段树)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1018 \(2*n\)的距形,起初没有边相连,之后有三种操作: 1.加边. 2.删边. 3.询问 ...
- python auto send email
/*************************************************************************** * python auto send emai ...
- windows2003远程桌面退出后系统自动注销的解决方法
最近公司有一个奇怪的需求,意思是有一个网页,要时时的打开着.现在只有把这个网页在服务器上打开. 这样才能满足需求.但我在应用中遇见了个问题.我在服务器上打开网页后,关掉远程,过一会网页的运行效果就没有 ...
- UpYun上传 401 Unauthorized
_upt=3b9b444a14059041252014-07-21 08:46:25,218 ERROR (com.UpYun:518) - Upload file error:<h1>4 ...
- 怎么学数学[How to Study Math]