压缩图片大小(Java源码)
2015-11-14 09:49
477 查看
/** * * 直接指定压缩后的宽高: * @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 web的学习好从基础做起
- java的四种引用
- struts2中action向action之间传数据和action向jsp传数据理解
- eclipse集成maven3后,创建java项目详细图解
- Java函数参数传递方式详解
- 长链接生成短链接Java源码(调用百度接口)
- Java字节转换类实现
- JAVA关于继承、多态和接口
- Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
- java调用存储过程方法
- Struts2 防止重复提交
- struts2核心工作流程与原理
- java的真相
- 自己做的demo---宣告可以在java世界开始自由了
- 第2章番外 Java的命名规范
- Java类获取spring 容器的bean
- Java NIO教程(十一)Pipe
- Java NIO教程(十)DatagramChannel
- SpringMVC与Struts2的对比
- java初始化数组操作