java將pdf轉成base64字符串及將base64字符串反轉pdf

package cn.wonders.utils;java

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;web

import org.springframework.web.util.UriUtils;spring

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();apache

//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;
}

app


/**
* @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;
}


}url

相關文章
相關標籤/搜索