Java圖片讀取ImageIO.read()報(bào)錯(cuò)問題及解決
更新時(shí)間:2024年10月15日 15:47:18 作者:刀下陽光
在使用imageio庫讀取圖片時(shí),如果路徑中包含中文,可能會(huì)導(dǎo)致讀取失敗,解決方法是將路徑中的中文字符進(jìn)行轉(zhuǎn)義處理,可以使用ImageUtil.java工具類進(jìn)行路徑轉(zhuǎn)義,從而避免錯(cuò)誤,這是一個(gè)常見問題,希望本文的解決方案能幫助到遇到相同問題的開發(fā)者
使用imageio直接讀取圖片報(bào)錯(cuò)問題重現(xiàn)


原因分析
路徑中包含中文解決方案:
將路徑中的中文進(jìn)行轉(zhuǎn)義
URLEncoder.encode(fileName,"UTF-8")
ImageUtil.java工具類:
package com.test.common.utils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
public class ImageUtil {
/**
* 描述:將圖片地址進(jìn)行base64編碼
*/
public static String encodeImgageToBase64(URL imageUrl) {// 將圖片文件轉(zhuǎn)化為字節(jié)數(shù)組字符串,并對(duì)其進(jìn)行Base64編碼處理
ByteArrayOutputStream outputStream = null;
try {
BufferedImage bufferedImage = ImageIO.read(imageUrl);
outputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "jpg", outputStream);
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 對(duì)字節(jié)數(shù)組Base64編碼
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(outputStream.toByteArray());// 返回Base64編碼過的字節(jié)數(shù)組字符串
}
/**
* 描述:將圖片文件進(jìn)行base64編碼
*/
public static String encodeImgageToBase64(File imageFile) {// 將圖片文件轉(zhuǎn)化為字節(jié)數(shù)組字符串,并對(duì)其進(jìn)行Base64編碼處理
ByteArrayOutputStream outputStream = null;
try {
BufferedImage bufferedImage = ImageIO.read(imageFile);
outputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "jpg", outputStream);
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 對(duì)字節(jié)數(shù)組Base64編碼
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(outputStream.toByteArray());// 返回Base64編碼過的字節(jié)數(shù)組字符串
}
/**
* 描述:將base64圖片解碼并保存
*/
public static File decodeBase64ToImage(String base64, String path, String imgName) {
BASE64Decoder decoder = new BASE64Decoder();
File file=null;
try {
file=new File(path + imgName);
FileOutputStream write = new FileOutputStream(file);
String replase=base64.replace("data:image/jpeg;base64,","");
byte[] decoderBytes = decoder.decodeBuffer(replase);
write.write(decoderBytes);
write.close();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
IDEA中配置Python環(huán)境并運(yùn)行方式
本文介紹了在Mac和Windows平臺(tái)上安裝Python環(huán)境的方法,并詳細(xì)講解了如何在IntelliJ IDEA中安裝Python插件、創(chuàng)建Python工程和運(yùn)行Python文件,同時(shí),還提到了一些常用的Python框架,如Django、Google App Engine和SQL支持2025-03-03
Spring Cache + Caffeine的整合與使用示例詳解
對(duì)于一些項(xiàng)目里需要對(duì)數(shù)據(jù)庫里的某些數(shù)據(jù)一直重復(fù)請(qǐng)求的,且這些數(shù)據(jù)基本是固定的,在這種情況下,可以借助簡(jiǎn)單使用本地緩存來緩存這些數(shù)據(jù),本文介紹一下Spring Cache和Caffeine的使用,感興趣的朋友一起看看吧2023-12-12
基于Spring MVC的文件上傳和下載實(shí)現(xiàn)方法
在Web應(yīng)用程序中,文件上傳和下載是常見的功能,Spring MVC框架提供了方便的方式來實(shí)現(xiàn)這些功能,本文將介紹如何使用Spring MVC實(shí)現(xiàn)文件上傳和下載,需要的朋友可以參考下2023-05-05
Spring?Boot?3.x?集成?Feign的詳細(xì)過程
本文闡述了如何在SpringBoot3.x中集成Feign,以實(shí)現(xiàn)微服務(wù)之間的調(diào)用,主要步驟包括:搭建chain-common服務(wù),創(chuàng)建chain-starter/chain-feign-starter服務(wù),集成Feign到chain-system和chain-iot-channel服務(wù),配置Feign,感興趣的朋友一起看看吧2024-09-09
Java設(shè)計(jì)模式之觀察者模式(Observer模式)介紹
這篇文章主要介紹了Java設(shè)計(jì)模式之觀察者模式(Observer模式)介紹,Java深入到一定程度,就不可避免的碰到設(shè)計(jì)模式(design pattern)這一概念,了解設(shè)計(jì)模式,將使自己對(duì)java中的接口或抽象類應(yīng)用有更深的理解,需要的朋友可以參考下2015-03-03
詳解使用Java代碼讀取并比較本地兩個(gè)txt文件區(qū)別
這篇文章主要為大家介紹了使用Java代碼讀取并比較本地兩個(gè)txt文件區(qū)別詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

