package com.jkcredit.invoice.util; import org.apache.commons.codec.binary.Base64; import java.io.*; public class Base64Util { public static void main(String [] args){ //将PDF格式文件转成base64编码 String base64String = getPDFBinary(new File("C:\\Users\\msy\\Desktop\\服务及承诺函.pdf")); //将base64的编码转成PDF格式文件 base64StringToPDF(base64String); } public static String fileToBase64(String path) { int byteread = 0; String total = null; byte[] totalbyte = new byte[0]; InputStream inStream = null; try { inStream = getStream(path); byte[] buffer = new byte[1024]; while ((byteread = inStream.read(buffer)) != -1) { //拼接流,这样写是保证文件不会被篡改 totalbyte = byteMerger(totalbyte, buffer, byteread); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (null != inStream) { inStream.close(); } } catch (IOException e) { e.printStackTrace(); } } return Base64.encodeBase64String(totalbyte); } //合并数组 public static byte[] byteMerger(byte[] totalArr, byte[] mergerArr, int mergerLen) { if (null == totalArr || null == mergerArr){ return totalArr; } if (mergerLen == 0 || mergerLen > mergerArr.length) { throw new RuntimeException("mergerLen : " + mergerArr + " ,mergerArrLen : " + mergerArr.length); } int len = totalArr.length; byte[] reArr = new byte[len + mergerLen]; System.arraycopy(totalArr, 0, reArr, 0, len); System.arraycopy(mergerArr, 0, reArr, len, mergerLen); return reArr; } //根据 url 获取输入流 public static InputStream getStream(String path) throws IOException { File file = new File(path); InputStream in = new FileInputStream(file); return in; } /** * 精确计算base64字符串文件大小(单位:B) * @param base64String * @return */ public static double base64FileSize(String base64String) { /**检测是否含有base64,文件头)*/ if (base64String.lastIndexOf(",") > 0) { base64String = base64String.substring(base64String.lastIndexOf(",")+1); } /** 获取base64字符串长度(不含data:audio/wav;base64,文件头) */ int size0 = base64String.length(); /** 获取字符串的尾巴的最后10个字符,用于判断尾巴是否有等号,正常生成的base64文件'等号'不会超过4个 */ String tail = base64String.substring(size0 - 10); /** 找到等号,把等号也去掉,(等号其实是空的意思,不能算在文件大小里面) */ int equalIndex = tail.indexOf("="); if (equalIndex > 0) { size0 = size0 - (10 - equalIndex); } /** 计算后得到的文件流大小,单位为字节 */ return size0 - ((double) size0 / 8) * 2; } /** * 将PDF转换成base64编码 * 1.使用BufferedInputStream和FileInputStream从File指定的文件中读取内容; * 2.然后建立写入到ByteArrayOutputStream底层输出流对象的缓冲输出流BufferedOutputStream * 3.底层输出流转换成字节数组,然后由BASE64Encoder的对象对流进行编码 * */ public static String getPDFBinary(File file) { FileInputStream fin =null; BufferedInputStream bin =null; ByteArrayOutputStream baos = null; BufferedOutputStream bout =null; try { //建立读取文件的文件输出流 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(); return new sun.misc.BASE64Encoder().encodeBuffer(bytes).trim(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { fin.close(); bin.close(); //关闭 ByteArrayOutputStream 无效。此类中的方法在关闭此流后仍可被调用,而不会产生任何 IOException //baos.close(); 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){ BufferedInputStream bin = null; FileOutputStream fout = null; BufferedOutputStream bout = null; try { //将base64编码的字符串解码成字节数组 byte[] bytes = new sun.misc.BASE64Decoder().decodeBuffer(base64sString); //apache公司的API //byte[] bytes = Base64.decodeBase64(base64sString); //创建一个将bytes作为其缓冲区的ByteArrayInputStream对象 ByteArrayInputStream bais = new ByteArrayInputStream(bytes); //创建从底层输入流中读取数据的缓冲输入流对象 bin = new BufferedInputStream(bais); //指定输出的文件http://m.nvzi91.cn/nxby/29355.html File file = new File("C:\\Users\\msy\\Desktop\\马圣毅.pdf"); //创建到指定文件的输出流 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 { bin.close(); fout.close(); bout.close(); } catch (IOException e) { e.printStackTrace(); } } } }