2 回答
TA貢獻1853條經驗 獲得超6個贊
public void base64ToFile(String base64, String pdfName) {
FileOutputStream fileOutputStream = null;
try {
//創建文件目錄
String filePath="D:\\image";
File file=new File(filePath);
if (!file.exists() && !file.isDirectory()) {
file.mkdirs();
}
byte[] s = Base64.decodeBase64(base64.getBytes());
fileOutputStream = new FileOutputStream(file+File.separator+pdfName);
fileOutputStream.write(s);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}TA貢獻1847條經驗 獲得超11個贊
BASE64解碼成File文件
public static void base64ToFile(String base64, String fileName) {
File file = null;
//創建文件目錄
String filePath="D:\image";
File dir=new File(filePath);
if (!dir.exists() && !dir.isDirectory()) {
dir.mkdirs();
}
BufferedOutputStream bos = null;
java.io.FileOutputStream fos = null;
try {
byte[] bytes = Base64.getDecoder().decode(base64);
file=new File(filePath+"\"+fileName);
fos = new java.io.FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 2 回答
- 0 關注
- 628 瀏覽
添加回答
舉報
