base64轉pdf


public static void base64StringToPdf(String base64Content, String filePath) {
BASE64Decoder decoder = new BASE64Decoder();
byte[] bytes;
try {
bytes = decoder.decodeBuffer(base64Content);// base64編碼內容轉換為字節數組
} catch (IOException e) {
throw new BusinessRuntimeException("base64轉換pdf失敗:base64轉換為字節數組失敗");
}
File file = new File(filePath);
File path = file.getParentFile();
if (!path.exists()) {
boolean b = path.mkdirs();
if (!b) {
throw new BusinessRuntimeException("base64轉換pdf失敗:創建文件失敗");
}
}
try (
ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);
BufferedInputStream bis = new BufferedInputStream(byteInputStream);
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream 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();
}
}


免責聲明!

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



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