java中pdf写成base64文件流,Java操作pdf文件与Base64编码相互转换与文件流操作
1.第一步,引入bc包的安装依赖。
在pom.xml中引入。
org.bouncycastle
bcprov-jdk15on
1.60
引入后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
*/
@Override
public 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 File
object. A new
* FileDescriptor
object is created to represent this
* file connection.
*
* First, if there is a security manager, its checkWrite
* method is called with the path represented by the file
* argument as its argument.
*
* 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 FileNotFoundException
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
* checkWrite
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文件流,Java操作pdf文件与Base64编码相互转换与文件流操作相关推荐
- java 中如何打包成 jar 包
这里写目录标题 1. java 中如何打包成 jar 包 1.1. 手动打包可直接执行的 jar 包 1.2. 使用 intellij idea 工具打包可直接执行的 jar 包 1. java 中如 ...
- java中char类型转换成int类型的两种方法
java中char类型转换成int类型的两种方法 方法一: 第一种利用Integer包装类的方法Integer.parseInt Copychar ch = '9'; if (Character.is ...
- 使用pypdf2把原始pdf转换成kindle看着舒适的pdf
文章目录 裁剪pdf 使用脚本拆分页面并转成kindle可见的大小 压缩pdf (可选) 拆分pdf 由于买了个kindle,所以想要最大效率地利用它.而在kindle上看pdf是很难受的,因为kin ...
- java中随机数怎么定义类_浅析Java中的随机数类
Java中的随机数是否可以重复?Java中产生的随机数能否可以用来产生数据库主键?带着这个问题,我们做了一系列测试. 1.测试一: 使用不带参数的Random()构造函数 * @author Carl ...
- java弱引用怎么手动释放,十分钟理解Java中的弱引用,十分钟java引用
十分钟理解Java中的弱引用,十分钟java引用 本篇文章尝试从What.Why.How这三个角度来探索Java中的弱引用,帮助大家理解Java中弱引用的定义.基本使用场景和使用方法.由于个人水平有限 ...
- Java中 读-写 文件 BufferedReader BufferedWriter
本篇文章,为大家带来Java中进行文件读写的一种方式. 我的文件目录: /Users/gisboy/Desktop/a.txt Java中,用java.io.BufferedReader 进行文件内容 ...
- java中IO写文件工具类
以下是一些依据经常使用java类进行组装的对文件进行操作的类,平时,我更喜欢使用Jodd.io中提供的一些对文件的操作类,里面的方法写的简单易懂. 当中jodd中提供的JavaUtil类中提供的方法足 ...
- java将数据库数据转换成word文档并且生成pdf文件
目的:将数据转换成word图片的方式展现给用户 工具:openoffice +java代码 +数据库,其中openoffice有windows版本的和Linux版本的. 其中openoffice的下载 ...
- java将PDF转成字符串_【java】将PDF转成字符串
背景 我们在日常编程中,可能会遇到,无法直接传送PDF文件的情况,这就需要将PDF转成字符串.待接收方收到字符串再将字符串转成PDF文件. 代码 import java.io.BufferedInpu ...
- java中函数是什么_[一] java8 函数式编程入门 什么是函数式编程 函数接口概念 流和收集器基本概念...
本文是针对于java8引入函数式编程概念以及stream流相关的一些简单介绍 什么是函数式编程? java程序员第一反应可能会理解成类的成员方法一类的东西 此处并不是这个含义,更接近是数学上的函数 看 ...
最新文章
- oracle bi publisher 安装,Oracle BI Publisher 企业版安装后的配置(BI Publisher Enterpr
- Git学习笔记03--git reset
- 计算机控制系统第三次作业,计算机组成与系统结构第三次作业
- JBoss BRMS复杂事件处理(CEP)性能基准
- 基于MATLAB的Sobel边缘检测算法实现
- 去哪儿-13-city-vuex
- 卸掉IE8,降级使用
- 减少php,如何减少PHP代码?
- C++primer 12.2.1节练习
- Java基础:Collections
- 为什么阿里巴巴禁止在foreach里进行元素的remove/add操作
- python类的本质和跨文件全局变量(秒杀单例模式)
- 禁止电脑任何软件弹出窗口
- R语言:Newton法、似然函数
- xp任务栏不显示任务 vbs脚本
- Java笨狗的C/C++
- 度数换算_度数的换算
- 内容算法解读:提高内容摘要与原文的一致性(Faithfulness)
- android o 结构光流程,结构光光条中心的提取算法.pdf
- 发送linux键盘消息,在C#程序中模拟发送键盘按键消息
热门文章
- 用于Arduino的S4A图形化编程图文详解
- java html pdf 中文_java html 转pdf 中文可显示
- Driver——同相运算放大器
- 累计投放贷款1000亿,马云是如何做银行的?
- 小米平板4软件提取包_小米三大法宝:软件、硬件和服务;以及它的4条成功经验|小米手机|智能手机|手机...
- linux网络连接问题(ping不通)
- python二进制数据存入数据库_python+ mysql存储二进制流的方式
- 题解:luogu P2634 [国家集训队]聪聪可可
- 量化交易之vnpy篇 - 几种同步发单模式(中金所股指锁仓模式、最小单边轧差操作模式、双边同步模式,净头寸模式)
- 震惊全国的汶川大地震