基于Java實(shí)現(xiàn)文件和base64字符串轉(zhuǎn)換
這篇文章主要介紹了基于Java實(shí)現(xiàn)文件和base64字符串轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
項(xiàng)目中遇到需要將圖片轉(zhuǎn)成base64編碼的字符串的需求,但是,考慮到擴(kuò)展性,寫了一個(gè)可以轉(zhuǎn)換任務(wù)類型文件的方法。需要引入的包:
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.13</version>
</dependency>
源碼如下:
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.*;
public class Base64FileUtil {
private static String targetFilePath = "E:\\base2Img\\target\\test.txt";
public static void main(String[] args) throws Exception {
String fileStr = getFileStr("E:\\base2Img\\big test.txt");
System.out.println("fileStr ===" + fileStr);
System.out.println(generateFile(fileStr, targetFilePath));
System.out.println("end");
}
/**
* 文件轉(zhuǎn)化成base64字符串
* 將文件轉(zhuǎn)化為字節(jié)數(shù)組字符串,并對(duì)其進(jìn)行Base64編碼處理
*/
public static String getFileStr(String filePath) {
InputStream in = null;
byte[] data = null;
// 讀取文件字節(jié)數(shù)組
try {
in = new FileInputStream(filePath);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 對(duì)字節(jié)數(shù)組Base64編碼
BASE64Encoder encoder = new BASE64Encoder();
// 返回 Base64 編碼過的字節(jié)數(shù)組字符串
return encoder.encode(data);
}
/**
* base64字符串轉(zhuǎn)化成文件,可以是JPEG、PNG、TXT和AVI等等
*
* @param base64FileStr
* @param filePath
* @return
* @throws Exception
*/
public static boolean generateFile(String base64FileStr, String filePath) throws Exception {
// 數(shù)據(jù)為空
if (base64FileStr == null) {
System.out.println(" 不行,oops! ");
return false;
}
BASE64Decoder decoder = new BASE64Decoder();
// Base64解碼,對(duì)字節(jié)數(shù)組字符串進(jìn)行Base64解碼并生成文件
byte[] byt = decoder.decodeBuffer(base64FileStr);
for (int i = 0, len = byt.length; i < len; ++i) {
// 調(diào)整異常數(shù)據(jù)
if (byt[i] < 0) {
byt[i] += 256;
}
}
OutputStream out = null;
InputStream input = new ByteArrayInputStream(byt);
try {
// 生成指定格式的文件
out = new FileOutputStream(filePath);
byte[] buff = new byte[1024];
int len = 0;
while ((len = input.read(buff)) != -1) {
out.write(buff, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
out.flush();
out.close();
}
return true;
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java如何優(yōu)雅關(guān)閉異步中的ExecutorService
在并發(fā)編程領(lǐng)域,Java的ExecutorService是線程池管理的關(guān)鍵接口,這篇文章主要為大家介紹了如何優(yōu)雅關(guān)閉異步中的ExecutorService,需要的可以了解下2025-02-02
基于Java Gradle復(fù)制項(xiàng)目模塊過程圖解
這篇文章主要介紹了基于Java Gradle復(fù)制項(xiàng)目模塊過程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
Java HttpClient-Restful工具各種請(qǐng)求高度封裝提煉及總結(jié)
這篇文章主要介紹了Java HttpClient-Restful工具各種請(qǐng)求高度封裝提煉及總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
SpringBoot + Mybatis增刪改查實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于SpringBoot + Mybatis增刪改查的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Spring Cloud 配置中心多環(huán)境配置bootstrap.yml的實(shí)現(xiàn)方法
spring cloud用上了配置中心,就一個(gè)boostrap.yml,本文就來介紹一下Spring Cloud 配置中心多環(huán)境配置bootstrap.yml的實(shí)現(xiàn)方法,感興趣的可以了解一下2024-03-03
Java開發(fā)學(xué)習(xí)之Bean的作用域和生命周期詳解
這篇文章主要介紹了淺談Spring中Bean的作用域,生命周期和注解,從創(chuàng)建到消亡的完整過程,例如人從出生到死亡的整個(gè)過程就是一個(gè)生命周期。本文將通過示例為大家詳細(xì)講講,感興趣的可以學(xué)習(xí)一下2022-06-06
SpringBoot整合POI導(dǎo)出通用Excel的方法示例
這篇文章主要介紹了SpringBoot整合POI導(dǎo)出通用Excel的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08

