java pdf的base64 转 pdf

pdf的base64一般都很长,这里把它放到了一个.txt,用读文件的方式加载进来,然后保存到指定路径,

找到后打开查看内容

import org.junit.Test;
import sun.misc.BASE64Decoder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class PdfTest {

    @Test
    public void depdf() {
        // 存放pdf的base64
        Path path = Paths.get("C:\\Users\\Desktop\\pdf.txt");  
        Stream<String> lines = null;
        try {
            lines = Files.lines(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 读取文件转换为String
        String collect = lines.collect(Collectors.joining(System.lineSeparator())); 
        String encodedBytes = collect;
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] decodedBytes = new byte[0];
        try {
            decodedBytes = decoder.decodeBuffer(encodedBytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        File file = new File("D:/newfile.pdf");
        FileOutputStream fop = null;
        try {
            fop = new FileOutputStream(file);
            fop.write(decodedBytes);
            fop.flush();
            fop.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


版权声明:本文为qq_42320804原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。