1. 程式人生 > >java將pdf轉成base64字串及將base64字串反轉pdf

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.net.HttpURLConnection;
import java.net.URL;

import org.springframework.web.util.UriUtils;

import com.sun.org.apache.xerces.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 URL(UriUtils.encodePath(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);
conn.connect();
is = conn.getInputStream();
os = new ByteArrayOutputStream();
while ((len = is.read(buff)) != -1) {
os.write(buff, 0, len);
}

//重新整理此輸出流並強制寫出所有緩衝的輸出位元組,必須這行程式碼,否則有可能有問題
os.flush();
os.toByteArray();
dUrlData= Base64.encode(os.toByteArray());
// 二進位制資料流轉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 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){
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 URL(UriUtils.encodePath(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);
conn.connect();

InputStreamReader brr = new InputStreamReader(conn.getInputStream(),"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;
}


}