zhaowenxuan
7 天以前 0892d843cd394f383ff50f480f7f22f2ec3829bc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.ltkj.web.config.pdfutils;
 
/**
 * @Author: 西安路泰科技有限公司/lige
 * @Date: 2022/12/9 18:24
 */
 
import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
 
import java.io.*;
 
/***
 * 为WEBSERVICE 准备 PDF的二进制流的字符串
 * 文件在 XXX转成二进制流的字符串,及将二进制流反转成PDF\PDFBinary.jar
 * xxx转成二进制流的字符串,及将二进制流反转成PDF\PDFBinary.jar
 */
public class PDFBinaryUtil {
    /*BASE64Encoder和BASE64Decoder这两个方法是sun公司的内部方法,并没有在java api中公开过,所以使用这些方法是不安全的,
     * 将来随时可能会从中去除,所以相应的应该使用替代的对象及方法,建议使用apache公司的API---可引用 import org.apache.commons.codec.binary.Base64;进行替换*/
    static BASE64Encoder encoder = new BASE64Encoder();
    static BASE64Decoder decoder = new BASE64Decoder();
 
    public static void main(String[] args) {
        //将PDF格式文件转成base64编码
        String base64String = getPDFBinary("d:\\Users\\w\\Desktop\\pdf\\22212087152体检报告.pdf");
        System.out.println(base64String);
        //将base64的编码转成PDF格式文件,,保存到XXX
        base64StringToPDF(base64String, "d:\\Users\\w\\Desktop\\pdf\\22212087152体检报告1111111.pdf");
    }
 
 
    /**
     * 将PDF转换成base64编码
     * 1.使用BufferedInputStream和FileInputStream从File指定的文件中读取内容;
     * 2.然后建立写入到ByteArrayOutputStream底层输出流对象的缓冲输出流BufferedOutputStream
     * 3.底层输出流转换成字节数组,然后由BASE64Encoder的对象对流进行编码
     */
    public static String getPDFBinary(String filePath) {
        FileInputStream fin = null;
        BufferedInputStream bin = null;
        ByteArrayOutputStream baos = null;
        BufferedOutputStream bout = null;
        try {
            File file = new File(filePath);
            //建立读取文件的文件输出流
            fin = new FileInputStream(file);
            //在文件输出流上安装节点流(更大效率读取)
            bin = new BufferedInputStream(fin);
            // 创建一个新的 byte 数组输出流,它具有指定大小的缓冲区容量
            baos = new ByteArrayOutputStream();
            //创建一个新的缓冲输出流,以将数据写入指定的底层输出流
            bout = new BufferedOutputStream(baos);
            byte[] buffer = new byte[1024];
            int len = bin.read(buffer);
            while (len != -1) {
                bout.write(buffer, 0, len);
                len = bin.read(buffer);
            }
            //刷新此输出流并强制写出所有缓冲的输出字节,必须这行代码,否则有可能有问题
            bout.flush();
            byte[] bytes = baos.toByteArray();
            //sun公司的API
            //return encoder.encodeBuffer(bytes).trim();
            //apache公司的API
            return Base64.encodeBase64String(bytes);
 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fin != null)
                    fin.close();
                if (bin != null)
                    bin.close();
                //关闭 ByteArrayOutputStream 无效。此类中的方法在关闭此流后仍可被调用,而不会产生任何 IOException
                if (baos != null)
                    baos.close();
                if (bout != null)
                    bout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
 
    /**
     * 将base64编码转换成PDF,保存到
     *
     * @param base64sString 1.使用BASE64Decoder对编码的字符串解码成字节数组
     *                      2.使用底层输入流ByteArrayInputStream对象从字节数组中获取数据;
     *                      3.建立从底层输入流中读取数据的BufferedInputStream缓冲输出流对象;
     *                      4.使用BufferedOutputStream和FileOutputSteam输出数据到指定的文件中
     */
    public static void base64StringToPDF(String base64sString, String filePath) {
        BufferedInputStream bin = null;
        FileOutputStream fout = null;
        BufferedOutputStream bout = null;
        ByteArrayInputStream bais = null;
        try {
            //将base64编码的字符串解码成字节数组
            byte[] bytes = decoder.decodeBuffer(base64sString);
            //apache公司的API
            //byte[] bytes = Base64.decodeBase64(base64sString);
            //创建一个将bytes作为其缓冲区的ByteArrayInputStream对象
            bais = new ByteArrayInputStream(bytes);
            //创建从底层输入流中读取数据的缓冲输入流对象
            bin = new BufferedInputStream(bais);
            //指定输出的文件
            File file = new File(filePath);
            //创建到指定文件的输出流
            fout = new FileOutputStream(file);
            //为文件输出流对接缓冲输出流对象
            bout = new BufferedOutputStream(fout);
 
            byte[] buffers = new byte[1024];
            int len = bin.read(buffers);
            while (len != -1) {
                bout.write(buffers, 0, len);
                len = bin.read(buffers);
            }
            //刷新此输出流并强制写出所有缓冲的输出字节,必须这行代码,否则有可能有问题
            bout.flush();
 
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
               /* if (bin!=null)
                    bin.close();
                if (fout!=null)
                    fout.close();
                if (bout!=null)
                    bout.close();*/
                if (bout != null)
                    bout.close();
 
                if (fout != null)
                    fout.close();
                if (bin != null)
                    bin.close();
                if (bais != null) {
                    bais.close();
                }
 
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}