• Thumbnailator 压缩图片


    Github地址 https://github.com/coobird/thumbnailator

    不废话,直接上代码

    /**
         * 智能识车接口
         * 拍照识车 一图一车
         * ByteArrayResource
         *
         * @param deviceId
         * @return
         */
        @RequestMapping(value = "demo", method = RequestMethod.POST)
        @org.springframework.web.bind.annotation.ResponseBody
        public String demo(@RequestParam(value = "file") MultipartFile file, String deviceId, HttpServletRequest request) {
    
            String uri = "上传uri";
            String appKey = "appkey";
            String secret = "secret";
            String sign = "sign";
    
            try {
                RestTemplate restTemplate = restTemplateBuilder.setConnectTimeout(Duration.ofSeconds(5))
                        .setReadTimeout(Duration.ofSeconds(10)).build();
                List<String> cookies = CookieHelper.getCookieList(request);
                //构造请求头
                HttpHeaders headers = new HttpHeaders();
                headers.put("Cookie", cookies);
                headers.add("appKey", appKey);
                headers.add("sign", sign);
                headers.add("deviceId", deviceId);
                headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    
                MultiValueMap<String, Object> map = new LinkedMultiValueMap();
    
                String fileName = file.getOriginalFilename();
                // 获取文件的后缀名,比如图片的jpeg,png
                String suffixName = fileName.substring(fileName.lastIndexOf("."));
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                //Thumbnails.fromInputStreams(Arrays.asList(file.getInputStream())).scale(0.5).toFile("E:\images\a.jpg");
                //Quality png格式不起作用,先转换为jpg 格式,再应用
                Thumbnails.fromInputStreams(Arrays.asList(file.getInputStream())).scale(0.5).outputQuality(0.8).toOutputStream(os);
                ByteArrayResource arrayResource = new ByteArrayResource(os.toByteArray()) {
                    @Override
                    public String getFilename() throws IllegalStateException {
                        return fileName;
                    }
                };
    
                Resource resource = arrayResource;
    
                map.add("cid", 300);
                map.add("file", resource);
                HttpEntity requestEntity = new HttpEntity(map, headers);
                ResponseEntity<String> responseEntity = restTemplate.postForEntity(uri, requestEntity, String.class);
    
                HttpHeaders headersRes = responseEntity.getHeaders();
                return responseEntity.getBody();
            } catch (IOException ex) {
                ex.printStackTrace();
                System.out.println(ex);
            } catch (Exception ex) {
                ex.printStackTrace();
                System.out.println(ex);
            }
            return "";
        }
  • 相关阅读:
    不怕上不了 Android developers
    不花钱的可靠性设计
    linux2.6 内核特性配置
    Linux动态库的编译与使用 转载
    多线程使用互斥锁的C范例
    TSLIB 分析
    Notepad++中文版下载 以及HEX显示
    C程序实现在lcd 上全屏写 blue 色 及获取fb信息
    pthread_cond_wait的spurious wakeup问题
    查看linux内存条数的命令与清理内存使用
  • 原文地址:https://www.cnblogs.com/xiyoujiyjy/p/15217407.html
Copyright © 2020-2023  润新知