项目总结:电子签名功能-对接第三方-02-技术 -2.如何实现本地上传pdf文件同时转化为一张长图片上传到OSS服务器:

#2.如何实现本地上传pdf文件同时转化为一张长图片上传到OSS服务器:
上传文件:

在这里插入代码片
   //本地上传协议 pdf 和 一张完整的jpg/png到服务器的方法:
    public String insertProtocol(MultipartFile file) throws IOException {

 		/*-------------------------上传协议 start ---------------------*/
        //根据文件名进行查重,判断是否已经上传过该文件
        String fileName = file.getOriginalFilename().replaceAll(" ", "");
        String[] split = fileName.split("\\.");
        String s = split[0];
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("protocolName", s);
        List<Map<String, Object>> list = protocolDao.list(map);
        if (list.size() > 0) {
            return "请不要重复上传!";
        }
		//判断上传的文件是否是pdf格式的:
        if (!file.getOriginalFilename().endsWith(".pdf")) {
            return "选择的文件格式错误,请刷新重试!";
        }

        Protocol protocol=new Protocol();
		//获取上传文件的大小-插表
        if (file != null && file.getSize() != 0) {
            long size = file.getSize();
            protocol.setProtocolSize(size);

            String fileFormat = ".pdf";
            //获取文件名+后缀
            // map.put("protocolName", s);
            String ossPath = s + fileFormat;
            //上传文件:
			//实例化ossFUtils工具类,否则报空指针:
            OssFUtils ossFUtils = OssFUtils.getInstance();
            //获取文件流:
            InputStream inputStreamPDF = file.getInputStream();
            //以流的形式调用阿里云的上传文件方法将文件上传到服务器
            String descPath =ossFUtils.uploadFile("tope365-agent", inputStreamPDF, "agent/protocol/" + ossPath);
			//获取文件在服务器上的下载地址:
            String protocolyUrl ="";
            //可直接下载地址
            if(descPath !=null){
                protocolyUrl = descPath.replace("tope365-agent@", properties.getProperty("tope365_agent_domain"));
            }
            //将下载地址更新入库:
            protocol.setUploadUrl(protocolyUrl);

            /*-------------------------上传协议 end ---------------------*/


转长图上传:

在这里插入代码片
    /*-------------------------pdf转jpg/png start ---------------------*/
            //合同文件对应的预览图片上传到oss 属性设置
            String fileExtensionImg = ".png";
            String newFileNameImg = s + fileExtensionImg;
            String osspathImg = "agent/protocol/" + newFileNameImg;
            InputStream inputStreamJpg = file.getInputStream();
            //图像合并使用参数:
 			// 设置图片的总宽度:
            int width = 0;
            // 保存一张图片中的RGB数据:
            int[] singleImgRGB; 
            int shiftHeight = 0;
            //保存每张图片的像素值
            BufferedImage imageResult = null;
            // 加载解析PDF文件
            //利用PdfBox生成图像
            //注:使用pdf转图片时需要在pom中引入三个jar包,缺一不可:
            PDDocument pdf = PDDocument.load(inputStreamJpg);
            PDFTextStripper text = new PDFTextStripper();
            String text1 = text.getText(pdf);
            logger.info("读取PDF文字:" + text1);
            PDFRenderer renderer = new PDFRenderer(pdf);
            //循环文件的每个页码
            for (int i = 0, len = pdf.getNumberOfPages(); i < len; i++) {
                //dpi参数越大,越清晰
                BufferedImage image = renderer.renderImageWithDPI(i, 200, ImageType.RGB);
                //计算高度和偏移量
                int imageHeight = image.getHeight();
                int imageWidth = image.getWidth();
                if (i == 0) {
                	//使用第一张图片宽度;
                    width = imageWidth;
                    //保存每页图片的像素值
                    imageResult = new BufferedImage(width, imageHeight * len, BufferedImage.TYPE_INT_RGB);
                } else {
               		// 计算偏移高度
                    shiftHeight += imageHeight; 
                }
                singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width);
                // 将解析的图像参数写入流中
                imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width); 
            }
            ByteArrayOutputStream os = new ByteArrayOutputStream();
                ImageIO.write(imageResult, "png", os);
                //获取流数组
                byte[] datas = os.toByteArray();
                //传入流中
                inputStreamJpg = new ByteArrayInputStream(datas);
                //调用阿里云方法进行上传:
                ossFUtils = OssFUtils.getInstance();
                    //上传png
                    ossFUtils.uploadFile("tope365-agent", inputStreamJpg, osspathImg);
            }
        /*------------------------pdf转jpg/png end ---------------------*/
  		 //入库
    }

辅助:
1.引入依赖:注:使用pdf转图片时需要在pom中引入三个jar包,缺一不可:

 		<dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>fontbox</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox-tools</artifactId>
            <version>2.0.1</version>
        </dependency>
        


**辅助:
2.如何将pdf文件转成一张长图片:jpg/png格式均可:
***将PDF转成一页长图:*****
```java

public static BufferedImage pdfChangeImage(InputStream inputStream) {
        //图像合并使用参数:
        // 设置图片的总宽度
        int width = 0; 
        // 保存一张图片中的RGB数据
        int[] singleImgRGB; 
        int shiftHeight = 0;
        //保存每张图片的像素值
        BufferedImage imageResult = null;
 
        try {
            //利用PdfBox生成图像
            PDDocument pdDocument = PDDocument.load(inputStream);
            PDFTextStripper text = new PDFTextStripper();
            String text1 = text.getText(pdDocument);
            logger.info("读取PDF文字:" + text1);
            PDFRenderer renderer = new PDFRenderer(pdDocument);
            //循环文件中的每个页码
            for (int i = 0, len = pdDocument.getNumberOfPages(); i < len; i++) {
                //dpi参数越大,越清晰
                BufferedImage image = renderer.renderImageWithDPI(i, 300, ImageType.RGB);
                //计算高度和偏移量
                int imageHeight = image.getHeight();
                int imageWidth = image.getWidth();
                if (i == 0) {
                	//使用第一张图片宽度;
                    width = imageWidth;
                    //保存每页图片的像素值
                    imageResult = new BufferedImage(width, imageHeight * len, BufferedImage.TYPE_INT_RGB);
                } else {
                // 计算偏移高度
                    shiftHeight += imageHeight; 
                }
                singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width);
                // 将图片参数写入流中
                imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width); 
            }
            pdDocument.close();
        } catch (Exception e) {
            logger.error(" 从PDF转换JPG格式异常!", e);
        }
        return imageResult;
    }
public static void main(String[] args) {
    InputStream inputStream = null;
    BufferedImage bufferedImage = null;
    String url = "";
    try {
        //pdf转img
        bufferedImage = PDFUtils.pdfChangeImage(new URL(url));
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, "png", os);
        inputStream = new ByteArrayInputStream(os.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追梦者晓东

我是最不起眼的程序员阿东。感谢

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值