最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java中Base64和File之間互轉(zhuǎn)代碼示例

 更新時(shí)間:2023年08月01日 09:50:01   作者:七月七日晴52000  
這篇文章主要給大家介紹了關(guān)于Java中Base64和File之間互轉(zhuǎn)的相關(guān)資料,Base64是網(wǎng)絡(luò)上最常見(jiàn)的用于傳輸8Bit字節(jié)碼的編碼方式之一,Base64就是一種基于64個(gè)可打印字符來(lái)表示二進(jìn)制數(shù)據(jù)的方法,需要的朋友可以參考下

1、Base64 轉(zhuǎn) File

public File base64ToFile(String base64, String filePath) {
        File file = new File(filePath);
        byte[] buffer;
        try {
            BASE64Decoder base64Decoder = new BASE64Decoder();
            buffer = base64Decoder.decodeBuffer(base64);
            FileOutputStream out = new FileOutputStream(filePath);
            out.write(buffer);
            out.close();
        } catch (Exception e) {
            Log.e("TAG", "異常信息:" + e.getMessage());
        }
        return file;
    }

2、File 轉(zhuǎn) Base64

public String fileToBase64(String filePath) {
	File file = new File(filePath);
	FileInputStream inputFile;
	try {
		inputFile = new FileInputStream(file);
		byte[] buffer = new byte[inputFile.available()];
		inputFile.read(buffer);
		inputFile.close();
		BASE64Encoder base64Encoder = new BASE64Encoder();
		Log.i("encodeFileToBase64", "encode = " + base64Encoder.encode(buffer));
		return base64Encoder.encode(buffer);
	} catch (Exception e) {
		Log.e("TAG", "異常信息:" + e.getMessage());
	}
	return "";
}

附:java把base64位編碼轉(zhuǎn)為File文件并存到本地

public File getFileFromBase64(String base) throws Exception {
        String base64Pic = base;
        File file = null;
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if (base64Pic == null) { // 圖像數(shù)據(jù)為空
            resultMap.put("resultCode", 0);
            resultMap.put("msg", "圖片為空");
        } else {
            BASE64Decoder decoder = new BASE64Decoder();
            String baseValue = base64Pic.replaceAll(" ", "+");//前臺(tái)在用Ajax傳base64值的時(shí)候會(huì)把base64中的+換成空格,所以需要替換回來(lái)。
            byte[] b = decoder.decodeBuffer(baseValue.replace("data:image/jpeg;base64,", ""));//去除base64中無(wú)用的部分
            base64Pic = base64Pic.replace("base64,", "");
            SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
            String nowDate = df2.format(new Date());
            String imgFilePath = QMConfig.filePathRoot + "\\" + nowDate + "\\" + System.currentTimeMillis();
            File file1 = new File(imgFilePath);
            if (!file1.exists() && !file1.isDirectory()) {//判斷文件路徑下的文件夾是否存在,不存在則創(chuàng)建
                file1.mkdirs();
            }
            try {
                for (int i = 0; i < b.length; ++i) {
                    if (b[i] < 0) {// 調(diào)整異常數(shù)據(jù)
                        b[i] += 256;
                    }
                }
                file = new File(imgFilePath + "\\" + System.currentTimeMillis());
                // 如果要返回file文件這邊return就可以了,存到臨時(shí)文件中
                OutputStream out = new FileOutputStream(file.getPath());
                out.write(b);
                out.flush();
                out.close();
            } catch (Exception e) {
                resultMap.put("resultCode", 0);
                resultMap.put("msg", "存儲(chǔ)異常");
            }
        }
        return file;
    }

總結(jié) 

到此這篇關(guān)于Java中Base64和File之間互轉(zhuǎn)的文章就介紹到這了,更多相關(guān)Java Base64和File互轉(zhuǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

宜宾市| 镇宁| 财经| 东乡| 西丰县| 浦北县| 上饶县| 望谟县| 庄浪县| 新昌县| 太保市| 和顺县| 滨州市| 金沙县| 筠连县| 蒲城县| 苏州市| 康保县| 苍溪县| 谢通门县| 乳山市| 文安县| 商丘市| 江永县| 舞阳县| 漯河市| 岱山县| 济阳县| 崇礼县| 永定县| 吉林省| 江油市| 深泽县| 绥棱县| 贵定县| 沂水县| 沅江市| 阿巴嘎旗| 汝南县| 泰顺县| 宜宾市|