java压缩源代码_压缩图片大小(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 list = new ArrayList();
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源码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是Robtos.txt 协议?
- 下一篇: TeeChart.NET 4.2021.