java讀取圖片并轉(zhuǎn)化為二進(jìn)制字符串的實(shí)現(xiàn)方法
本例子的目的在于測(cè)試往oracle數(shù)據(jù)庫(kù)中插入blob字段
public static String getImgStr(String imgFile){
//將圖片文件轉(zhuǎn)化為字節(jié)數(shù)組字符串,并對(duì)其進(jìn)行Base64編碼處理
InputStream in = null;
byte[] data = null;
//讀取圖片字節(jié)數(shù)組
try
{
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return new String(Base64.encodeBase64(data));
}
--
利用以上的思路寫的一個(gè)測(cè)試
public class ReadImageTest {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream(new File("C:\\Users\\luzhifei\\Pictures\\hc_logo.png"));
String picStr="";
byte[] read = null;
int len = 0;
read= new byte[fis.available()];
fis.read(read);
String baseStr= Base64.getEncoder().encodeToString(read);
//System.out.println( baseStr);
byte[] op= Base64.getDecoder().decode(baseStr);
// System.out.println(new String(op));
FileOutputStream fos = new FileOutputStream(new File("d:\\temp\\1.jpg"));
fos.write(op,0,op.length );
fos.flush();
fos.close();
}
}
但是available()有一定的限制。
為了穩(wěn)妥,嚴(yán)重建議采取以下方式:
public static void imageToBase64Str() throws IOException{
FileInputStream fis = new FileInputStream(new File("C:\\Users\\luzhifei\\Pictures\\hc_logo.png"));
byte[] read = new byte[1024];
int len = 0;
List<byte[]> blist=new ArrayList<byte[]>();
int ttllen=0;
while((len = fis.read(read))!= -1){
byte[] dst=new byte[len];
System.arraycopy(read, 0, dst, 0, len);
ttllen+=len;
blist.add(dst);
}
fis.close();
byte[] dstByte=new byte[ttllen];
int pos=0;
for (int i=0;i<blist.size();i++){
if (i==0){
pos=0;
}
else{
pos+=blist.get(i-1).length;
}
System.arraycopy(blist.get(i), 0, dstByte, pos, blist.get(i).length);
}
String baseStr= Base64.getEncoder().encodeToString(dstByte);
byte[] op= Base64.getDecoder().decode(baseStr);
FileOutputStream fos = new FileOutputStream(new File("d:\\temp\\2.jpg"));
fos.write(op,0,op.length );
fos.flush();
fos.close();
}
總結(jié)
以上所述是小編給大家介紹的java讀取圖片并轉(zhuǎn)化為二進(jìn)制字符串,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
Javaweb請(qǐng)求轉(zhuǎn)發(fā)及重定向?qū)崿F(xiàn)詳解
這篇文章主要介紹了Javaweb請(qǐng)求轉(zhuǎn)發(fā)及重定向?qū)崿F(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
SpringBoot+?Sharding?Sphere?輕松實(shí)現(xiàn)數(shù)據(jù)庫(kù)字段加解密功能
在Spring?Boot生態(tài)中,有一個(gè)非常厲害的開源框架:Apache?ShardingSphere,它是一款分布式?SQL?事務(wù)和查詢引擎,可通過數(shù)據(jù)分片、彈性伸縮、加密等能力對(duì)任意數(shù)據(jù)庫(kù)進(jìn)行增強(qiáng),今天通過這篇文章,我們一起來了解一下如何在?Spring?Boot?中快速實(shí)現(xiàn)數(shù)據(jù)的加解密功能2024-07-07
java同步器AQS架構(gòu)AbstractQueuedSynchronizer原理解析
這篇文章主要為大家介紹了java同步器AQS架構(gòu)AbstractQueuedSynchronizer的底層原理及源碼解析,有需要的朋友可以借鑒參考下,希望能有所幫助,祝大家多多進(jìn)步早日升職加薪2022-03-03
Springboot @Configuration @bean注解作用解析
這篇文章主要介紹了springboot @Configuration @bean注解作用解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
spring boot 如何指定profile啟動(dòng)
這篇文章主要介紹了spring boot 如何指定profile啟動(dòng)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07

