欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

最新評(píng)論