자바 그림 경로

18914 단어 도구 클래스
package com.dagen.imgs.util;

import com.dagen.imgs.util.api.Base64Util;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class Base64Url {
    /**
     *  base64          
     * @param imgStr: base64     
     * @param path:       -     
     * @return
     */
    public static boolean getImage(String imgStr, String path){
        if (imgStr == null){
            return false;
        }
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            //   
            byte[] b = decoder.decodeBuffer(imgStr);
            //     
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(path);
            out.write(b);
            out.flush();
            out.close();
            return true;
        }catch (Exception e){
            return false;
        }
    }
    /**
     * @Description:          base64     
     * @return
     *       ,       base64              :"data:image/jpeg;base64," ,          。
     */
    public static String getbase64Url(String imgFile) {
        InputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imgFile);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //   
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }

    public static void main(String[] args) {

        //boolean b = getImage(strImg, "C:/Users/admin/Desktop/  /    /2.jpg");
        //System.out.println("==========================================================");
       // System.out.println(b);
    }
    /**
     *      url     base64   
     * @param imgUrl      url
     * @return        base64    
     */
    public static String image2Base64(String imgUrl) {

        URL url = null;

        InputStream is = null;

        ByteArrayOutputStream outStream = null;

        HttpURLConnection httpUrl = null;

        try{

            url = new URL(imgUrl);

            httpUrl = (HttpURLConnection) url.openConnection();

            httpUrl.connect();

            httpUrl.getInputStream();

            is = httpUrl.getInputStream();



            outStream = new ByteArrayOutputStream();

            //    Buffer   

            byte[] buffer = new byte[1024];

            //          ,   -1,        

            int len = 0;

            //        buffer        

            while( (len=is.read(buffer)) != -1 ){

                //     buffer     ,              ,len       

                outStream.write(buffer, 0, len);

            }

            //      Base64  

            return Base64Util.encode(outStream.toByteArray());

        }catch (Exception e) {

            e.printStackTrace();

        }finally{

            if(is != null)

            {

                try {

                    is.close();

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }

            if(outStream != null)

            {

                try {

                    outStream.close();

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }

            if(httpUrl != null)

            {

                httpUrl.disconnect();

            }

        }

        return imgUrl;

    }
}





얻 은 데이터 앞 에 더하기 data:image/jpg;base64,

좋은 웹페이지 즐겨찾기