java将pdf转成base64字符串及将base64字符串反转pdf package cn.wonders.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.HttpURLConnection;
import java.URL;
import org.springframework.web.util.UriUtils;
import s.internal.impl.dv.util.Base64;
import sun.misc.BASE64Decoder;
public class GetFile {
static BASE64Decoder decoder = new sun.misc.BASE64Decoder();
/**
* @Title:buFile
* @Description:根据防伪码获取⽂件,将pdf转成base64字符串
* @param: @param typeData
* @param: @param codeData
* @param: @return
* @return:StringBuffer
* @throws
*/
public static String buFile(String typeData,String codeData) {
InputStream is = null;
ByteArrayOutputStream os = null;
String resultCode = "00";
String dUrlData="";
// StringBuffer res_xml = new StringBuffer();
字符串转数组编码方式
/
/pdf源路径
String urlStr = " ";
byte[] buff = new byte[1024];
int len = 0;
try {
URL url = new dePath(urlStr, "UTF-8"));
// URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "plain/text;charset="+ "UTF-8");
conn.setRequestProperty("charset", "UTF-8");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setReadTimeout(30000);
is = InputStream();
os = new ByteArrayOutputStream();
while ((len = is.read(buff)) != -1) {
os.write(buff, 0, len);
}
//刷新此输出流并强制写出所有缓冲的输出字节,必须这⾏代码,否则有可能有问题
os.flush();
dUrlData= ByteArray());
// ⼆进制数据流转BASE64格式字符串
} catch (IOException e) {
resultCode = "01";
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
resultCode = "02";
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
resultCode = "03";
}
}
}
System.out.println(dUrlData);
return dUrlData;
}
/**
* @Title:base64StringToPDF
* @Description:
*1.使⽤BASE64Decoder对编码的字符串解码成字节数组
*2.使⽤底层输⼊流ByteArrayInputStream对象从字节数组中获取数据;
*3.建⽴从底层输⼊流中读取数据的BufferedInputStream缓冲输出流对象;
*4.使⽤BufferedOutputStream和FileOutputSteam输出数据到指定的⽂件中
* @param: @param base64sString
* @param: @param filePath
* @return:void
* @throws
*/
public static StringBuffer BasetoPdffile(String typeData, String codeData,String filepath){ BufferedInputStream bis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
String pdfBase64Str = null;
String resultCode = "00";
StringBuffer res_xml = new StringBuffer();
try{
//将pdf转为base64编码的字符串
pdfBase64Str = buFile( typeData,codeData);
//将base64编码的字符串解码成字节数组
byte[] bytes=Base64.decode(pdfBase64Str);
//apache公司的API
//byte[] bytes = Base64.decodeBase64(pdfBase64Str);
//创建⼀个将bytes作为其缓冲区的ByteArrayInputStream对象ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(bytes);
//创建从底层输⼊流中读取数据的缓冲输⼊流对象
bis=new BufferedInputStream(byteArrayInputStream);
//指定输出的⽂件
File file=new File(filepath);
File ParentFile();
if(!ists()){
path.mkdirs();
}
//创建到指定⽂件的输出流
fos=new FileOutputStream(file);
//为⽂件输出流对接缓冲输出流对象
bos=new BufferedOutputStream(fos);
byte[] buffer=new byte[1024];
int ad(buffer);
while(length!=-1){
bos.write(buffer,0,length);
ad(buffer);
}
//刷新此输出流并强制写出所有缓冲的输出字节,必须这⾏代码,否则有可能有问题bos.flush();
}catch(Exception e){
resultCode="01";
e.printStackTrace();
}finally {
try{
bis.close();
bos.close();
fos.close();
}catch (IOException e){
resultCode="02";
e.printStackTrace();
}
}
res_xml.append("{");
res_xml.append("\""+Contant.XML_RESULTCODE+"\":\""+resultCode+"\",");
res_xml.append("\""+Contant.XML_RESULTDATA+"\":\""+filepath+"\"");
res_xml.append("}");
System.out.println("filepath="+filepath);
return res_xml;
}
/**
* @Title:inStFile
* @Description:pdf按⾏读取
* @param: @param typeData
* @param: @param codeData
* @param: @return
* @return:StringBuffer
* @throws
*/
public static StringBuffer inStFile(String typeData,String codeData) { InputStream is = null;
String resultCode = "00";
StringBuffer res_xml = new StringBuffer();
StringBuffer sb = new StringBuffer();
//源路径
String urlStr = "";
try {
URL url = new URL(urlStr);
// URL url = new dePath(urlStr, "UTF-8")); HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "plain/text;charset="+ "UTF-8"); conn.setRequestProperty("charset", "UTF-8");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setReadTimeout(30000);
InputStreamReader brr = new InputStream(),"UTF-8"); BufferedReader br = new BufferedReader(brr);
String message = null;
while ((message = br.readLine()) != null) {
sb.append(message);
}
} catch (IOException e) {
resultCode = "01";
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
resultCode = "02";
}
}
}
res_xml.append("{");
res_xml.append("\""+Contant.XML_RESULTCODE+"\":\""+resultCode+"\",");
res_xml.append("\""+Contant.XML_RESULTDATA+"\":\""+sb+"\"");
res_xml.append("}");
System.out.println(res_xml);
return res_xml;
}
}
原创博客,引⽤请注明出处    wwwblogs/guangxiang/p/10334130.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。