• 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);
            }
        }
    }
  • 相关阅读:
    程序员过年必备 -- Auto.js微信自动抢红包
    VSCode, Django, and Anaconda开发环境集成配置[Windows]
    pandas to_excel 修改excel样式以及格式
    hvac系统开源项目情况匠能智控
    安卓作业(有空再整理)
    在jetson nano中配opencv环境(python通用)
    获取深度图像
    英伟达开发板安装python-opencv下的问题
    在vs2015中对kinectV2环境搭建,环境配置
    PagerSlidingTabStrip 导入 Android Studio 的教程
  • 原文地址:https://www.cnblogs.com/tonyzeng/p/5630785.html
Copyright © 2020-2023  润新知