图片压缩

   @ResponseBody
    @RequestMapping(value = "uploadImage")
    public Map<String, Object> uploadImage(@RequestParam(value = "upfile", required = false) MultipartFile upfile, HttpServletRequest request, HttpServletResponse response) {
        /*将图片装换为base64位编码*/
        JSONObject json = new JSONObject();
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> resultMap = new HashMap<>();
        String originalName = upfile.getOriginalFilename();
        String prefix = originalName.substring(originalName.lastIndexOf(".") + 1);
        if (!prefix.equalsIgnoreCase("jpg") && !prefix.equalsIgnoreCase("jpeg") && !prefix.equalsIgnoreCase("png") && !prefix.equalsIgnoreCase("gif")) {
            map.put("code", -1);
            map.put("msg", "文件上传失败");
        }
        byte[] data = null;
        String base64String = null;
        CustUser user = this.currentCustUser();
        if (user != null) {
            String userId = user.getUserId();
            try {
                /*缩略图*/
               /* BufferedImage sourceImg = javax.imageio.ImageIO.read(upfile.getInputStream());
                ImageUtil.scale(sourceImg, 0.5f);*/
                base64String = compressFile(upfile.getInputStream());
                System.out.println(base64String.length() + "+++++++++++++++++++++++++++++++");
             /*   if (available>20480){
                    available = compressFile(fileInput);
                }

                data = new byte[available];
                System.out.println("data:{}"+fileInput.read(data)+"---------------------------------------");
                int count = 0;
                String base64String = null;

                while ((count = fileInput.read(data)) > 0) {
                    //图片文件Base64编码
                    *//*BASE64Encoder encoder = new BASE64Encoder();
                    base64String = encoder.encode(data);*//*

                    Base64 base64 = new Base64();
                    base64String = base64.encodeBase64String(data);

                    *//*107080+++++++++++++++++++++++++++++++*//*
                 *//*232500+++++++++++++++++++++++++++++++*//*
                    S
                }*/
                resultMap.put("code", 200);
                resultMap.put("url", "data:image/jpeg;base64," + base64String);
                resultMap.put("title", originalName);
            } catch (IOException e) {

                log.error("uploadIdentityImg", e);
                map.put("code", -1);
                map.put("msg", "文件上传失败");
            } /*finally {
                if (fileInput != null) {
                    try {
                        fileInput.close();
                    } catch (IOException e) {
                        log.error("uploadIdentityImg", e);
                    }
                }
            }*/
        } else {
            map.put("code", -2);
            map.put("msg", "您未登录,请登录");
        }
        return resultMap;
    }

    /**
     * @author 李清伟
     * @time 2020/4/15-14:42
     * @description 压缩图片方法
     */
    public static String compressFile(InputStream input) {

        String base64String = null;
        try {
            BufferedImage bufImg = ImageIO.read(input);// 把图片读入到内存中
            bufImg = Thumbnails.of(bufImg).width(300).keepAspectRatio(true).outputQuality(0.3f).asBufferedImage();//压缩:宽度100px,长度自适应;质量压缩到0.1
            ByteArrayOutputStream bos = new ByteArrayOutputStream();// 存储图片文件byte数组
            ImageIO.write(bufImg, "jpg", bos); // 图片写入到 ImageOutputStream
            input = new ByteArrayInputStream(bos.toByteArray());
            byte[] data = new byte[input.available()];
            int count = 0;
            while ((count = input.read(data)) > 0) {
                //图片文件Base64编码
                    /*BASE64Encoder encoder = new BASE64Encoder();
                    base64String = encoder.encode(data);*/

                Base64 base64 = new Base64();
                base64String = base64.encodeBase64String(data);

                /*107080+++++++++++++++++++++++++++++++*/
                /*232500+++++++++++++++++++++++++++++++*/
                System.out.println(base64String.length() + "+++++++++++++++++++++++++++++++");
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    logger.error(e.getMessage(), e);
                }
            }
        }
        return base64String;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值