前端pdf生成的BASE64位編碼后端怎么解密步驟


當前端利用pdf自帶的加密方法加密時

 

 后端直接取出來生成文件打不開,經過測試之后發現,因為base64位編碼在72位字符之后會換行,,所以在獲取到pdf轉的編碼之后需要在進行去除編碼中的空格或換行的操作

 

下面是我接收編碼之后的操作

 

 

public ResponseEntity<Object> pdf( HttpServletRequest req, HttpServletResponse res) throws Exception{

BufferedReader reader = new BufferedReader(new InputStreamReader(req.getInputStream(),"UTF-8"));



String str = "";
String str2 = "";
String encoded="";
while((str = reader.readLine()) != null){//一行一行的讀取body體里面的內容;
int i = str.indexOf(",");
str2=str.substring(i+1, str.length()); //去除編碼前面因為pdf轉而出現的多余的字符
encoded = str2.replaceAll("[\\s*\t\n\r]", "");//去除編碼中的空格和換行
}
FileProperties.ElPath path = properties.getPath();//獲取項目文件中配置的路徑,關於pdf生成之后的路徑
String pathUtl = path.getPathOut();
WordGeneratorUtils.convertBase64ToFile(encoded,pathUtl,"PDF");
return new ResponseEntity<>(encoded, HttpStatus.OK);

}

 

 





 

public static File convertBase64ToFile(String  wholeStr, String pathUtl, String fileName) {

BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
String pathWay = pathUtl+"PDF"+File.separator;
File dir = new File(pathWay);
if (!dir.exists() && dir.isDirectory()) {//判斷文件目錄是否存在
dir.mkdirs();
}

BASE64Decoder decoder = new BASE64Decoder();
byte[] bfile = decoder.decodeBuffer(String.valueOf(wholeStr));

file = new File(pathWay + File.separator + fileName+".pdf");
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
return file;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM