JAVA压缩图片 服务器端压缩图片 原创
Posted Tony
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA压缩图片 服务器端压缩图片 原创相关的知识,希望对你有一定的参考价值。
@RequestMapping(value = "/uploadImage", method = RequestMethod.POST) @ResponseBody public String uploadImage(HttpServletRequest request) throws IOException { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; Iterator<String> iterator = multipartRequest.getFileNames(); String fileName = ""; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); String dir = "upload/" + sdf.format(new Date()) + "/"; String realPath = request.getSession().getServletContext().getRealPath("/"); String suffix = ""; while (iterator.hasNext()) { MultipartFile multipartFile = multipartRequest.getFile(iterator.next()); if (multipartFile != null) { String fn = multipartFile.getOriginalFilename(); suffix = fn.substring(fn.lastIndexOf(".")); fileName = Utils.getRandomStringByLength(6) + suffix; String path = realPath + dir + fileName; path = path.replace("\\", "/"); File f = new File(path); if (!f.mkdirs()) { f.mkdir(); } multipartFile.transferTo(f); } } PictureUtil.Tosmallerpic(realPath + dir, new File((realPath + dir + fileName).replace("\\", "/")), suffix, fileName, (float) 0.1); return dir +fileName; }
package com.property.client.util; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGEncodeParam; import com.sun.image.codec.jpeg.JPEGImageEncoder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.logging.Level; /** */ public class PictureUtil { private final static Logger logger = LoggerFactory.getLogger(PictureUtil.class); /** * @param originalFilePath 原图片所在的文件夹路径 * @param originalFileObject 原图片路径 * @param originalFileExtensionName 原图片扩展名 * @param originalFileName 原图片名 * @param percent 压缩百分比 */ public static void Tosmallerpic(String originalFilePath, File originalFileObject, String originalFileExtensionName, String originalFileName, float percent) { Image src; try { src = javax.imageio.ImageIO.read(originalFileObject); //构造Image对象 String img_midname = originalFilePath + originalFileName.substring(0, originalFileName.indexOf(".")) + originalFileExtensionName + "_small"; BufferedImage tag = new BufferedImage(src.getWidth(null), src.getHeight(null), BufferedImage.TYPE_INT_RGB); //tag.getGraphics().drawImage(src,0,0,new_w,new_h,null); //绘制缩小后的图 tag.getGraphics().drawImage(src.getScaledInstance(src.getWidth(null), src.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null); FileOutputStream newImage = new FileOutputStream(img_midname); //输出到文件流 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newImage); JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag); /* 压缩质量 */ jep.setQuality(percent, true); encoder.encode(tag, jep); //encoder.encode(tag); //近JPEG编码 newImage.close(); } catch (IOException ex) { logger.error("图片压缩失败", ex); } } }
以上是关于JAVA压缩图片 服务器端压缩图片 原创的主要内容,如果未能解决你的问题,请参考以下文章
移动端图片上传解决方案localResizeIMG先压缩后ajax无刷新上传