首页 > 其他分享 >pdf转base64字符串及base64字符串反转pdf

pdf转base64字符串及base64字符串反转pdf

时间:2024-01-30 09:35:01浏览次数:24  
标签:Base64 String base64 new 字符串 pdf out

pdf转base64

//转base64
    public  String fileToBase64() {
//        String imgFilePath = "C:\\Users\\zlf\\Desktop\\pdf\\042002200211_87910810.pdf";
        String imgFilePath = "C:\\Users\\zlf\\Desktop\\pdf\\【高德打车-38.65元-1个行程】高德打车电子行程单.pdf";
        String[] res = imgFilePath.split("\\.");
        String pos = res[res.length - 1];
        byte[] data = null;
        // 读取图片字节数组
        try {
            InputStream in = new FileInputStream(imgFilePath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String data1 = Base64.encodeBase64String(data);
        System.out.println(data1);
        return data1;
    }

Base64转pdf

//转pdf
    @Test
    public void sfad(){
        String s = this.fileToBase64();
        byte[] decode = Base64.decodeBase64(s);
        File file = new File("d:/file2.pdf");//pdf保存路径
        try {
            FileOutputStream fop =  new FileOutputStream(file);
            try {
                fop.write(decode);
                fop.flush();
                fop.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

这里的Base64我用的commons-codec:1.15

可以用jdk1.8的base64,如下

public static void main(String[] args) throws UnsupportedEncodingException {
    String string = "12345";
    System.out.println("old string: " + string);
    String base64 = Base64.getEncoder().encodeToString(string.getBytes("UTF-8"));
    System.out.println("base64 decode: " + base64);

    byte[] bytes = Base64.getDecoder().decode(base64);
    System.out.println("base64 encode: " + new String(bytes, "UTF-8"));
}

 

标签:Base64,String,base64,new,字符串,pdf,out
From: https://www.cnblogs.com/ketoli/p/17995791

相关文章

  • uniapp ArrayBuffer转16进度字符串 以及 十六进制转ASCII码
    1.ArrayBuffer转16进度字符串//ArrayBuffer转16进度字符串示例//ab2hex(buffer){//consthexArr=Array.prototype.map.call(//newUint8Array(buffer),//function(bit){//......
  • 《代码大全(第2版)》PDF
    内容简介第2版的《代码大全》是著名IT畅销书作者史蒂夫·迈克康奈尔11年前的经典著作的全新演绎:第2版不是第一版的简单修订增补,而是完全进行了重写;增加了很多与时俱进的内容。这也是一本完整的软件构建手册,涵盖了软件构建过程中的所有细节。它从软件质量和编程思想等方面论述了软件......
  • 《实战Java高并发程序设计(第2版)》PDF
    内容简介在单核CPU时代,单任务在一个时间点只能执行单一程序,随着多核CPU的发展,并行程序开发变得尤为重要。《实战Java高并发程序设计(第2版)》主要介绍基于Java的并行程序设计基础、思路、方法和实战。第一,立足于并发程序基础,详细介绍Java进行并行程序设计的基本方法。第二,进一步详细......
  • 字符串
    $\largeBorder:若字符串同长度的前缀和后缀完全相同,即Prefix[i]=suffix[i],称此前(后)缀为字符串的一个Border$重要性质 $\largep是S的周期\Leftrightarrow|S|-p是S的Border$$\large所以求周期等价于求Border,Border不具备二分性$$\largeS的Border的Border也是......
  • 【专题】保险行业数字化洞察白皮书报告PDF合集分享(附原数据表)
    报告链接:https://tecdat.cn/?p=33203原文出处:拓端数据部落公众号近年来,"养老"、"三胎政策"、"医疗成本"等一系列备受关注的民生话题,使得保险服务备受瞩目,并逐渐渗透到每个人的生活中。自2020年以来,由于多种因素的影响,人们对健康的意识不断提高,这正在重新塑造中国消费者对保险的......
  • Pdfium.Net.Free 一个免费的Pdfium的 .net包装器--加载字体
    项目地址:Pdfium.Net:https://github.com/1000374/Pdfium.NetPdfiumViewer:https://github.com/1000374/PdfiumViewerPdfium.Net加载字体:1.加载ttf字体文件using(vardoc=PdfDocument.CreateNew()){varfontPath=@"c:\Windows\fonts\simhei.ttf";......
  • Pdfium.Net.Free 一个免费的Pdfium的 .net包装器--添加文本
    项目地址:Pdfium.Net:https://github.com/1000374/Pdfium.NetPdfiumViewer:https://github.com/1000374/PdfiumViewerPdfium.Net添加文本有3个重载1.当前重载使用pdf标准字库添加文字(中文会乱码)///<summary>///AddtextobjectusingoneofthestandardPDFfont......
  • Pdfium.Net.Free 一个免费的Pdfium的 .net包装器--创建字符子集
    项目地址:Pdfium.Net:https://github.com/1000374/Pdfium.NetPdfiumViewer:https://github.com/1000374/PdfiumViewerPdfium.Net.Free一个免费的Pdfium的.net包装器--加载字体 接上篇,怎么创建字符子集呢?获取字符集内的字形符号需要引用wpf下PresentationCore.dll,根据比对传入......
  • 查询表、视图字符串字段的长度
    SELECTa.colorder字段序号,a.name字段名,b.name类型,CASEWHENCOLUMNPROPERTY(a.id,a.name,'PRECISION')=-1THEN',长度无限制'ELSE',长度限制'+CAST(COLUMNPROPERTY(a.id,......
  • C#正则提取字符串中的数字
    首先需要引入命名空间System.Text.RegularExpressions,具体实现如下所示://提取纯数字,该方式会将所有数字提取出来并拼接在一起,如:"ABC#123@AS456测试"提取出来就是123456varresult1=Regex.Replace("ABC#123@AS测试",@"[^0-9]+","");//提取多个数字,该方式会分别提取字符串中......