1.第一步,引入bc包的安装依赖。

在pom.xml中引入。

<!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on -->
<dependency><groupId>org.bouncycastle</groupId><artifactId>bcprov-jdk15on</artifactId><version>1.60</version>
</dependency>

引入后reimport一下项目。

2.pdf文件转换成Base64.

/*** @Description:* @param:* @return:* @author: TateBrown* @Date: 2018/7/25*/@ApiOperation("根据docid返回pdf文件Base64编码")@PostMapping("/FindPDFBase")public R FindPDFBase(@PathVariable("signdocid") Integer signdocid){FileInputStream fin=null;BufferedInputStream bin=null;ByteArrayOutputStream baos=null;BufferedOutputStream bout=null;try{SignDocEntity signDoc = signDocService.selectById(signdocid);String filepath=signDoc.getUrl();File file=new File(filepath);if(!file.exists()){return R.error("文件不存在");}fin=new FileInputStream(file);bin=new BufferedInputStream(fin);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();String res=Base64.toBase64String(bytes);return R.ok("获取成功").put("Base64",res);}catch(Exception e){e.printStackTrace();return R.error();}finally {try{fin.close();bin.close();bout.close();}catch (IOException e){e.printStackTrace();}}}

总的思路就是,先弄几个文件流的引用,将文件转换成二进制流读取存到数组中,然后利用bc包中的Base64的函数toBase64String将二进制数组转换成Base64的操作。

3.Base64转换成pdf文件。

Base64一般比pdf文件要大一些,但是对于浏览器来说,Base64可以直接解析,做一个在线预览功能。而存储在服务器端一般使用.pdf文件来存储,一般不会用Base64存。所以Base64转换成pdf也是非常关键的。下面是源码。

 /*** @Description:Base64转换成pdf* @param:* @return:* @author: TateBrown* @Date: 2018/7/23*/@Overridepublic void BasetoPdffile(String pdfBase64Str,String filepath){BufferedInputStream bis = null;FileOutputStream fos = null;BufferedOutputStream bos = null;try{byte[] bytes=Base64.decode(pdfBase64Str);ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(bytes);bis=new BufferedInputStream(byteArrayInputStream);File file=new File(filepath);File path=file.getParentFile();if(!path.exists()){path.mkdirs();}fos=new FileOutputStream(file);bos=new BufferedOutputStream(fos);byte[] buffer=new byte[1024];int length=bis.read(buffer);while(length!=-1){bos.write(buffer,0,length);length=bis.read(buffer);}bos.flush();}catch(Exception e){e.printStackTrace();}finally {try{bis.close();bos.close();fos.close();}catch (IOException e){e.printStackTrace();}}}

总的思路就是和上面的反过来,先将Base64利用bc包中的函数解析成二进制流。然后将二进制流写入文件即可。这里要注意的是FileOutputStream这个构造函数。我们可以看一下源码

 /*** Creates a file output stream to write to the file represented by* the specified <code>File</code> object. A new* <code>FileDescriptor</code> object is created to represent this* file connection.* <p>* First, if there is a security manager, its <code>checkWrite</code>* method is called with the path represented by the <code>file</code>* argument as its argument.* <p>* If the file exists but is a directory rather than a regular file, does* not exist but cannot be created, or cannot be opened for any other* reason then a <code>FileNotFoundException</code> is thrown.** @param      file               the file to be opened for writing.* @exception  FileNotFoundException  if the file exists but is a directory*                   rather than a regular file, does not exist but cannot*                   be created, or cannot be opened for any other reason* @exception  SecurityException  if a security manager exists and its*               <code>checkWrite</code> method denies write access*               to the file.* @see        java.io.File#getPath()* @see        java.lang.SecurityException* @see        java.lang.SecurityManager#checkWrite(java.lang.String)*/public FileOutputStream(File file) throws FileNotFoundException {this(file, false);}

在这上面我们可以看到它另有一个默认的参数,为false,根据注解这个参数的意思其实是判断你对该文件的操作是继续往下面添加,还是重新擦掉重来。默认是擦掉重来。

另外pdf可能会很大,很大利用这种方式就是会发生问题,因为二进制流数组不够大,这时候可以使用分块上传,自行实现这里就不赘述了。

Java操作pdf文件与Base64编码相互转换与文件流操作相关推荐

  1. java中pdf写成base64文件流,Java操作pdf文件与Base64编码相互转换与文件流操作

    1.第一步,引入bc包的安装依赖. 在pom.xml中引入. org.bouncycastle bcprov-jdk15on 1.60 引入后reimport一下项目. 2.pdf文件转换成Base6 ...

  2. html音频base64编码,录音文件与Base64编码相互转换的方法

    前言 最近有几个朋友一直在问语音文件怎么转base64字符串进行发送上传,base64字符串又如何转成文件,论坛中已经有多篇问题的帖子有介绍,这里只是稍微整理,方便大家可以更加方便的使用,首先看效果: ...

  3. 录音文件与Base64编码相互转换的方法

    http://ask.dcloud.net.cn/article/841?item_id=10780 前言 最近有几个朋友一直在问语音文件怎么转base64字符串进行发送上传,base64字符串又如何 ...

  4. PDF文件转换为Base64编码

    在线base64转pdf:格式转换 包名: import java.io.BufferedInputStream; import java.io.BufferedOutputStream; impor ...

  5. 使用PDF.js实现前端和手机端网页预览PDF文件(可定制,支持本地文件、Base64编码和远程URL跨域方式)

    1.插件下载地址:https://mozilla.github.io/pdf.js/ 下载后解压pdfjs-1.10.88-dist.zip文件后得到: 2.把pdfjs-1.10.88-dist放到 ...

  6. 图片从base64编码转换为jpg文件

    1.使用网站 注意在base64编码前加上:data:image/png;base64, http://tool.chinaz.com/tools/imgtobase​​​​​​​ 2.转换的代码 p ...

  7. android转base64内存溢出,base64编码处理大文件

    在作项目的时候遇到须要将文件转为base64编码,并存储在文件中.程序员 在将文件转为base64编码是会将文件读入内存,进行base64编码,输出到文件中.代码入下:编码 1spa 2code 3内 ...

  8. Html5 FileReader 对文件进行Base64编码

    以Base64进行编码的数据Url开始越来越广泛的被应用起来,原来做Base64转换要自己写一个小程序来转,其实Html5的FileReader的readAsDataURL方法读取出的数据就已经是Ba ...

  9. 要将OFD文件的base64编码转换为可下载的OFD文件

    要将OFD文件的base64编码转换为可下载的OFD文件,可以通过以下步骤: 将后台返回的OFD文件的base64编码解码成二进制数据. 创建一个Blob对象,并将二进制数据放入其中. const b ...

最新文章

  1. [C#基础]Func和Action学习
  2. Effective stl---笔记
  3. dd linux 尾部添加0_Linux文件目录命令整理Linux学习
  4. linuc和python常用命令是一样的么_Linux常用命令大全(非常全!!!)(2)|简明python教程|python入门|python教程...
  5. scrapy爬虫系列之三--爬取图片保存到本地
  6. mysql python连接时、指定的ip地址被转接了_pythonmysql.connector模块使用了错误的IP地址...
  7. dj鲜生-38-项目上线简介-从本地小项目到云服务的调试
  8. Hadoop系列之九:Hadoop集群伪分布式模式的实现详解
  9. 利用ffmpeg一步一步编程实现摄像头采集编码推流直播系统
  10. 整理下关于Visual Foxpro的技术
  11. 0ffice2003安装2007兼容包不能使用的解法
  12. 可與言而不與之言,失人。不可與言而與之言,失言。知者不失人,亦不失言。
  13. 应用礼学赋能新员工职业素养提升
  14. 托福高频真词List10 // 附托福TPO阅读真题
  15. matlab R2013a 生成exe 脱离matlab开发环境运行
  16. 阿里P7爆款《K8s+Jenkins》技术笔记,读了后确实有实质性的帮助
  17. GVM升级后启动异常处理
  18. 【详解】位运算符:位逻辑运算符,位移运算符(<<左移,>>右移)
  19. 新科技革命的主要特点
  20. vnc view使用教程

热门文章

  1. string::find()函数和string::npos静态成员常量的使用
  2. 分类任务中精确率(Precision)、召回率(Recall)以及准确率(Accuracy)解释说明
  3. 输入阻抗的仿真方法(TINA软件)
  4. 客户旅程_我来到纽约,并在Outreachy旅程中使用freeCodeCamp庆祝
  5. oracle11g解锁命令,Oracle11G的用户解锁、卸载以及基础操作
  6. js 数组合并的方式
  7. 学会共情让你的设计更走心
  8. bad magic number in 'application': b'\x03\xf3\r\n': ImportError
  9. HJ1 字符串最后一个单词的长度
  10. 用Java实现QQ登录