java將圖片輸出base64位碼顯示


注意需要過濾:\r \n數據

jkd1.7的

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
  /**
     * 網絡圖片轉換Base64的方法
     *
     * @param netImagePath     
     */
    public static String NetImageToBase64(String netImagePath) {
        final ByteArrayOutputStream data = new ByteArrayOutputStream();
        String strNetImageToBase64 = null;
        try {
            // 創建URL
            URL url = new URL(netImagePath);
            final byte[] by = new byte[1024];
            // 創建鏈接
            final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            if(conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                return strNetImageToBase64;
                //連接失敗/鏈接失效/圖片不存在
            }

            Thread thread =new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        InputStream is = conn.getInputStream();
                        // 將內容讀取內存中
                        int len = -1;
                        while ((len = is.read(by)) != -1) {
                            data.write(by, 0, len);
                        }
                        // 關閉流
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
            thread.start();
            try {
                thread.join();
            } catch (InterruptedException e) {
                 e.printStackTrace();
            }
            // 對字節數組Base64編碼
            BASE64Encoder encoder = new BASE64Encoder();
            strNetImageToBase64 = encoder.encode(data.toByteArray());
            // 刪除 \r\n
            strNetImageToBase64 = strNetImageToBase64.replaceAll("\n", "").replaceAll("\r", "");
            strNetImageToBase64 = "data:image/jpeg;base64," + strNetImageToBase64;



        } catch (IOException e) {
            e.printStackTrace();
        }
        return  strNetImageToBase64;
    }

  

 

 

1.8java.util.Base64包

/**
     * 轉換為base64格式
     *
     * @param image
     * @return
     */
    public static String encode2Base64Jpg(BufferedImage image) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();// io流
        try {
            ImageIO.write(image, "jpg", baos);
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] bytes = baos.toByteArray();// 轉換成字節
        Base64.Encoder encoder = Base64.getEncoder();
        String jpgBase64 = encoder.encodeToString(bytes).trim();// 轉換成base64串
        jpgBase64 = jpgBase64.replaceAll("\n", "").replaceAll("\r", "");// 刪除 \r\n

        return "data:image/jpeg;base64," + jpgBase64;
    }

  

 

 

其他本地圖片處理

/**
     * 圖片轉化成base64字符串
     * @param imgPath
     * @return
     */
    public static String GetImageStr(String imgPath) {// 將圖片文件轉化為字節數組字符串,並對其進行Base64編碼處理
        String imgFile = imgPath;// 待處理的圖片
        InputStream in = null;
        byte[] data = null;
        String encode = null; // 返回Base64編碼過的字節數組字符串
        // 對字節數組Base64編碼
        BASE64Encoder encoder = new BASE64Encoder();
        try {
            // 讀取圖片字節數組
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            encode = encoder.encode(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return encode;
    }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM