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();
}
}
posted @ 2021-12-23 19:10  p_小白  阅读(3875)  评论(0编辑  收藏  举报

你再瞅我 还瞅!关注啊