Maven

<!--ofd文件处理工具  这里和我其他包中的 slf4j冲突  排除一下-->
        <dependency>
            <groupId>org.ofdrw</groupId>
            <artifactId>ofdrw-full</artifactId>
            <version>1.8.6</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-slf4j-impl</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

代码

//这里返回的直接是一个jpg文件流
 public void getCertificateImg(String certificateRecordInfoId, HttpServletResponse resp)  {
  
  //这里请求第三方直接返回的是个ofd流
HttpResponse content = HttpRequest.post(certificateUrl + CommonCertificateUrl.METADATA_ABSTRACT_CODE).form(paramMap).execute();

        resp.setContentType("application/jpg; charset=UTF-8");
        resp.setHeader("Content-Disposition", "inline;filename=" + MD5Util.getMD5Str(licenceCertificateImgs.get(0).getAbstractCode()) + ".jpg");
        ByteArrayOutputStream bs = null;
        ImageOutputStream imOut = null;
        try {
            //ofd转换为 jpg
            ImageMaker imageMaker = new ImageMaker(new OFDReader(content.bodyStream()), 15);
            imageMaker.config.setDrawBoundary(false);
            BufferedImage image = null;
            //读取到 BufferedImage 中
            for (int i = 0; i < imageMaker.pageSize(); i++) {
                image = imageMaker.makePage(i);
            }
            //将 BufferedImage 转换为流
            bs = new ByteArrayOutputStream();
            imOut = ImageIO.createImageOutputStream(bs);
            ImageIO.write(image, "jpg", imOut);
            resp.getOutputStream().write(bs.toByteArray());
        } finally {
            if (ObjectUtil.isNotNull(bs)){
                bs.flush();
                bs.close();
            }
            if (ObjectUtil.isNotNull(imOut)){
                imOut.flush();
                imOut.close();
            }
        }
}
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐