pdf与base64相互转换的工具类

本文介绍了一个用于在Java中进行PDF文件与Base64字符串之间转换的工具类。通过这个工具,可以方便地将PDF文档编码为Base64字符串,也可以将Base64编码的数据解码并保存为PDF文件,适用于在Web应用中进行数据传输和存储。
摘要由CSDN通过智能技术生成
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * 用于PDF与Base64之间的转换
 * Created by Administrator on 2018/12/5 0005.
 */
public class PDFAndBase64ConvertUtil {

    /**
     * 将base64字符串转换为PDF在显示到页面中
     * @param base64String
     * @param httpServletResponse
     */
    public static void base64StringToPDFToPage(String base64String, HttpServletResponse httpServletResponse){
        BASE64Decoder decoder = new BASE64Decoder();
        ByteArrayOutputStream baos = null;
        ServletOutputStream sos = null;
        try {
            byte[] bytes = decoder.decodeBuffer(base64String);
            baos = new ByteArrayOutputStream();
            baos.write(bytes); //把byte写进输出流里
            if (baos != null) {
                httpServletResponse.setContentType("application/pdf");
 
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个Java工具类,提供了将文件、图片、PDF、URL转换Base64编码的方法,以及将Base64编码转换为文件的方法: ```java import java.io.*; import java.net.URL; import java.util.Base64; public class PdfUtil { /** * 将文件转换Base64编码 * @param file 文件对象 * @return Base64编码字符串 */ public static String fileToBase64(File file) { String base64 = null; InputStream in = null; try { in = new FileInputStream(file); byte[] bytes = new byte[in.available()]; in.read(bytes); base64 = Base64.getEncoder().encodeToString(bytes); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } return base64; } /** * 将图片转换Base64编码 * @param imgUrl 图片地址 * @return Base64编码字符串 */ public static String imageToBase64(String imgUrl) { String base64 = null; InputStream in = null; try { URL url = new URL(imgUrl); in = url.openStream(); byte[] bytes = new byte[in.available()]; in.read(bytes); base64 = Base64.getEncoder().encodeToString(bytes); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } return base64; } /** * 将PDF文件转换Base64编码 * @param file PDF文件对象 * @return Base64编码字符串 */ public static String pdfToBase64(File file) { String base64 = null; InputStream in = null; try { in = new FileInputStream(file); byte[] bytes = new byte[in.available()]; in.read(bytes); base64 = Base64.getEncoder().encodeToString(bytes); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } return base64; } /** * 将Base64编码保存为文件 * @param base64 Base64编码字符串 * @param filePath 文件保存路径 * @return 文件对象 */ public static File base64ToFile(String base64, String filePath) { File file = null; OutputStream out = null; try { byte[] bytes = Base64.getDecoder().decode(base64); file = new File(filePath); out = new FileOutputStream(file); out.write(bytes); } catch (IOException e) { e.printStackTrace(); } finally { if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } return file; } } ``` 使用方法如下: ```java // 将文件转换Base64编码 File file = new File("example.pdf"); String base64 = PdfUtil.fileToBase64(file); // 将图片转换Base64编码 String imgUrl = "https://example.com/image.png"; String base64 = PdfUtil.imageToBase64(imgUrl); // 将PDF文件转换Base64编码 File pdfFile = new File("example.pdf"); String base64 = PdfUtil.pdfToBase64(pdfFile); // 将Base64编码保存为文件 File file = PdfUtil.base64ToFile(base64, "example.pdf"); ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值