• 流处理PDF、Base64


    文件转Base64

    public static String ImageToBase64ByLocal(String imgFile) {
        String pdfBase64 = "";
        try {
            byte[] pdfPathByte = Files.readAllBytes(Paths.get(imgFile));
            pdfBase64 = Base64.encodeBase64String(pdfPathByte);
        } catch (Exception e) {
            logger.info(e.getMessage(), e);
        }
        return pdfBase64;
    }

    Base64转文件

    //方法一
    String pngPath = path + fileName + ".PNG";
    byte[] b = Base64Utils.decodeFromString(base64Str);
    try (OutputStream out = new FileOutputStream(pngPath)) {
        out.write(b);
        out.flush();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    //方法二
    Files.write(Paths.get(filePath), Base64.decodeBase64(base64), StandardOpenOption.CREATE);

    网络PDF地址转Base64

    public static String pathToBase64(String strUrl) {
        String base64 = "";
        try {
            HttpURLConnection conn = null;
            try {
                URL url = new URL(strUrl);
                conn = (HttpURLConnection) url.openConnection();
                final ByteArrayOutputStream output = new ByteArrayOutputStream();
                IOUtils.copy(conn.getInputStream(), output);
                InputStream inputStream = new ByteArrayInputStream(output.toByteArray());
    
                ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
                byte[] buff = new byte[100];
                int rc = 0;
                while ((rc = inputStream.read(buff, 0, 100)) > 0) {
                    swapStream.write(buff, 0, rc);
                }
                byte[] in_b = swapStream.toByteArray();
                base64 = Base64Utils.encodeToString(in_b);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            } finally {
                try {
                    if (conn != null) {
                        conn.disconnect();
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        return base64;
    }

    Base64转字节流

    public static byte[] Base64strToByte(String base64str) {
        if (StringUtils.isEmpty(base64str)) {
            return null;
        }
        Base64.Decoder decoder = Base64.getDecoder();
        return decoder.decode(base64str);
    }

    字节流保存为PDF文件

    public static void base64ByteToPdf(byte[] bytes, String filePath) {
        File file = new File(filePath);
        try (ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes); BufferedInputStream bis = new BufferedInputStream(byteInputStream); FileOutputStream fos = new FileOutputStream(file); BufferedOutputStream bos = new BufferedOutputStream(fos);) {
            byte[] buffer = new byte[1024];
            int length = bis.read(buffer);
            while (length != -1) {
                bos.write(buffer, 0, length);
                length = bis.read(buffer);
            }
            bos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    PNG Base64 转 PDF文件 转 PDF Base64

    public static String PNGToPDF(String path, String fileName, String base64Str) {
        byte[] pngBase64 = Base64Utils.decodeFromString(base64Str);
        String pdfPath = path + fileName + ".pdf";
        com.lowagie.text.Document document = new com.lowagie.text.Document();
        try (FileOutputStream fos = new FileOutputStream(pdfPath)) {
            PdfWriter.getInstance(document, fos);
            // 读取一个图片
            Image image = Image.getInstance(pngBase64);
            float imageHeight = image.getScaledHeight();
            float imageWidth = image.getScaledWidth();
            // 设置文档的大小
            document.setPageSize(new RectangleReadOnly(imageWidth, imageHeight));
            document.open();
            image.setAbsolutePosition(0, 0);
            // 插入一个图片
            document.add(image);
            document.close();
            fos.flush();
    
            //PDF文件转Base64
            byte[] pdfPathByte = Files.readAllBytes(Paths.get(pdfPath));
            String pdfBase64 = Base64.encodeBase64String(pdfPathByte);
    
            return pdfBase64;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    
        return null;
    }

    导出文件

    OutputStream out = response.getOutputStream();
    try(InputStream inputStream = new FileInputStream(totalPdfPath)) {
        int b = 0;
        byte[] buffer = new byte[1024];
        while ((b = inputStream.read(buffer)) != -1) {
            out.write(buffer, 0, b);
        }
    } catch (Exception e) {
        throw e;
    }
    out.flush();
  • 相关阅读:
    Ant Design Pro:Layout 组件——嵌套布局
    React实战之将数据库返回的时间转换为几分钟前、几小时前、几天前的形式。
    React实战之Ant Design—Upload上传_附件上传
    React实战之60s倒计时按钮(发送短信验证按钮)
    map方法到底会不会改变原始数组?
    【React】react开发vscode插件推荐
    【React】React 工程的 VS Code 插件及配置
    【React】react新特性实例详解(memo、lazy、suspense、hooks)
    数组常用slice和splice的区别
    【React】react-beautiful-dnd 实现组件拖拽
  • 原文地址:https://www.cnblogs.com/yifanSJ/p/10622273.html
Copyright © 2020-2023  润新知