zoukankan      html  css  js  c++  java
  • 流处理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();
  • 相关阅读:
    关于最近
    Cryptography Application Block
    关于修改SQL SERVER 登陆模式
    我与软件工程
    JAVA之路(一)
    C#基础:类的继承与多态
    简述java语言的特点
    简述java虚拟机的工作原理
    阿里云盾特点及防DDoS攻击服务介绍
    智能DNS
  • 原文地址:https://www.cnblogs.com/yifanSJ/p/10622273.html
Copyright © 2011-2022 走看看