亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

jdk6 中 如何將 Base64 轉為 File 類

jdk6 中 如何將 Base64 轉為 File 類

互換的青春 2018-07-06 14:26:19
網上搜到的是將 Base64轉為圖片 并保存到某一盤符下,要么就是 jdk版本限制;由于下游的數據處理是以 Blob 類型存儲到數據庫中的,在上游只需將 Base64 轉為 File類,但不用輸出到某一盤符就可以了;Stream 的轉換很頭大,希望大神可以指點一下,謝謝
查看完整描述

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


查看完整回答
反對 回復 2018-07-16
?
回首憶惘然

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


查看完整回答
反對 回復 2018-07-16
  • 2 回答
  • 0 關注
  • 591 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號