压缩图片大小(Java源码)
/**
*
* 直接指定压缩后的宽高:
* @param oldFile
* 要进行压缩的文件
* @param width
* 压缩后的宽度
* @param height
* 压缩后的高度
* @return 返回压缩后的文件的全路径
*/ public static File zipImageFile(File oldFile, int width, int height) { if (oldFile == null) {
return null;
}
File newImage = null;
try {
/** 对服务器上的临时文件进行处理 */
Image srcFile = ImageIO.read(oldFile);
/** 宽,高设定 */
BufferedImage tag = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null); /** 压缩后的文件名 可以再自定义 */
newImage = oldFile; /** 压缩之后临时存放位置 */
FileOutputStream out = new FileOutputStream(newImage);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag); /** 压缩质量 */ jep.setQuality(90, true);
encoder.encode(tag, jep);
out.close(); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return newImage; }
/**
* 方法描述 上传图片
*/
public void uploadImage() {
long beginTime = System.currentTimeMillis();
HttpServletResponse response = ServletActionContext.getResponse();
try { response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("utf-8");
ResultMsg msg = new ResultMsg();
FileInputStream fis = null;
OutputStream outputStream = null;
try {
String tempPath = "/upload/wechatshake/"+ getUserInfoSession().getAccount()+ "/";
// 生成上传文件
String path = ServletActionContext.getServletContext().getRealPath(tempPath)+"/"; File dir = new File(path);
if (!dir.exists()) {// 判断目录是否存在,否则创建
dir.mkdirs();
}
File file = ImageUtil.zipImageFile(getImage(),140,140); //压缩图片 if (file != null) {
Random random = new Random();
String saveUploadFileName = "upload"
+ System.currentTimeMillis()+ random.nextInt(1000)
+ imageFileName.substring(imageFileName.lastIndexOf("."),imageFileName.length()); fis = new FileInputStream(file);
outputStream = new FileOutputStream(new File(path,saveUploadFileName));
//byte[] buffer = new byte[1024];
int len = 0;
while ((len=fis.read())!=-1) {
outputStream.write(len);
}
//将图片上传到微信端
UserInfoWechat userInfoWechat = iUserInfoWechatServic.getByUser(getUserInfoSession().getId());
String token = iWeixinService.getAccessToken(userInfoWechat);
Material material = new Material();
material.setType("icon");
material.setMedia(tempPath+saveUploadFileName);
material = iWxActivityService.uploadMaterial(material, token);
logger.info("上传图片微信接口返回数据:"+material);
if(null != material){
List<String> list = new ArrayList<String>();
list.add(material.getData().getPic_url());
msg.setDataList(list);
msg.setCode("0");
}else {
msg.setCode("1");
msg.setDesc("发布失败");
}
msg.setDesc(SUCCESS);
} } catch (Exception e) {
error("上传文件失败", e);
msg.setCode("1");
msg.setDesc(ERROR);
} finally {
try {
if(fis!=null){fis.close();}
if(outputStream!=null){outputStream.close();}
} catch (IOException e) {
e.printStackTrace();
}
writeResult(msg);
}
} catch (Exception e) {
error("上传文件失败", e);
} finally {
printTime(beginTime, getClass(), "uploadImage");
}
}
压缩图片大小(Java源码)的更多相关文章
- Java源码解读(一)——HashMap
HashMap作为常用的一种数据结构,阅读源码去了解其底层的实现是十分有必要的.在这里也分享自己阅读源码遇到的困难以及自己的思考. HashMap的源码介绍已经有许许多多的博客,这里只记录了一些我看源 ...
- 【java集合框架源码剖析系列】java源码剖析之java集合中的折半插入排序算法
注:关于排序算法,博主写过[数据结构排序算法系列]数据结构八大排序算法,基本上把所有的排序算法都详细的讲解过,而之所以单独将java集合中的排序算法拿出来讲解,是因为在阿里巴巴内推面试的时候面试官问过 ...
- JVM之---Java源码编译机制
Sun JDK中采用javac将Java源码编译为class文件,这个过程包含三个步骤: 1.分析和输入到符号表(Parse and Enter) Parse过程所做的工作有词法和语法分 ...
- 分享知识-快乐自己:FastDFS 上传 java 源码
FastDFS 上传 java 源码:点我下载源码 首先导入 POM 文件:解决 maven 不能下载 fastdfs-client-java JAR <dependency> <g ...
- Java源码系列1——ArrayList
本文简单介绍了 ArrayList,并对扩容,添加,删除操作的源代码做分析.能力有限,欢迎指正. ArrayList是什么? ArrayList 就是数组列表,主要用来装载数据.底层实现是数组 Obj ...
- Java源码系列2——HashMap
HashMap 的源码很多也很复杂,本文只是摘取简单常用的部分代码进行分析.能力有限,欢迎指正. HASH 值的计算 前置知识--位运算 按位异或操作符^:1^1=0, 0^0=0, 1^0=0, 值 ...
- Tika结合Tesseract-OCR 实现光学汉字识别(简体、宋体的识别率百分之百)—附Java源码、测试数据和训练集下载地址
OCR(Optical character recognition) —— 光学字符识别,是图像处理的一个重要分支,中文的识别具有一定挑战性,特别是手写体和草书的识别,是重要和热门的科学研究方向.可 ...
- Java 源码刨析 - HashMap 底层实现原理是什么?JDK8 做了哪些优化?
[基本结构] 在 JDK 1.7 中 HashMap 是以数组加链表的形式组成的: JDK 1.8 之后新增了红黑树的组成结构,当链表大于 8 并且容量大于 64 时,链表结构会转换成红黑树结构,它的 ...
- 如何阅读Java源码 阅读java的真实体会
刚才在论坛不经意间,看到有关源码阅读的帖子.回想自己前几年,阅读源码那种兴奋和成就感(1),不禁又有一种激动. 源码阅读,我觉得最核心有三点:技术基础+强烈的求知欲+耐心. 说到技术基础,我打个比 ...
随机推荐
- 【LeetCode】拓扑排序
[207] Course Schedule 排课问题,n门课排课,有的课程必须在另外一些课程之前上,问能不能排出来顺序. 题解:裸的拓扑排序.参考代码见算法竞赛入门指南这本书. class Solut ...
- 【Movist Pro】macOS上的绝佳媒体播放器
Movist Pro是适用于Mac的高性能电影播放器,如果比较流程和界面,则Movist与QuickTime非常相似.因此,采用播放器几乎不会有任何问题.使用Quicktime或FFmpeg解码电影并 ...
- Java中的List集合
List集合继承自collection接口,他自己也是个接口,没有具体的结构,与Set集合不同,List集合允许重复的元素. List集合特有方法:(Collection中没有这些) 这些在Arral ...
- 骚操作:c++如何用goto便捷地写人工栈?
在如今所有NOI系列赛事已经开全栈的时势下,人工栈已经离我们很远很远. 所以这博客就是我弄着玩的. 首先我们要清楚的是c++的goto写法: loop:; - goto loop; 在运行到goto时 ...
- 【Java架构:基础技术】一篇文章搞掂:Eclipse
Eclipse中使用SVN 1.打开资源库视图 https://www.cnblogs.com/liangguangqiong/p/7965770.html 一.编辑器方面 格式化取消自动换行:打开E ...
- vc/atlmfc/include/afx.h(24) : fatal error C1189: #error : Building MFC application with /MD[d] (CRT
环境:win7,64位,vs2012 1> c:/program files/microsoft visual studio 8/vc/atlmfc/include/afx.h(24) : fa ...
- rbd_rados命令拷屏
mimic或者luminous rbd_rados sudo mount -t ceph 192.168.7.151:6789:/ /mnt -o name=admin,secret=AQBaPZNc ...
- 6. Python运算符之算术、比较、赋值运算符
什么是运算符?举个简单的例子 4 +1 = 5 . 例子中,4 和 1 被称为操作数,"+" 和"="称为运算符. 工作中用到的运算符主要有以下几种:算术运算符 ...
- 拾遗:Git 常用操作回顾
温故而知新,可以为师矣. Git 布局 工作区---->暂存区---->本地仓库---->远程仓库 Create Repository git init PATH git add P ...
- Netty 源码学习——客户端流程分析
Netty 源码学习--客户端流程分析 友情提醒: 需要观看者具备一些 NIO 的知识,否则看起来有的地方可能会不明白. 使用版本依赖 <dependency> <groupId&g ...