Base64 이미지 수신 저장 로컬

1960 단어
package com.cmos.smrz.util;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;

public class ImageSwitcher {

    /**
     *  base64          
     *       base64              "data:image/jpeg;base64,"        
     *
     * @param imgString base64      
     * @param imgPath       -     
     * @return       
     */
    public static boolean generateImage(String imgString, String imgPath) {
        if (imgString == null) {
            //       
            return false;
        }
        BASE64Decoder base64Decoder = new BASE64Decoder();
        try {
            // // Base64      
            byte[] b = base64Decoder.decodeBuffer(imgString);
            //     
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    //       
                    b[i] += 256;
                }
            }
            //       
            OutputStream outputStream = new FileOutputStream(imgPath);
            outputStream.write(b);
            outputStream.flush();
            outputStream.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     *           base64      
     *
     * @param imgFilePath       
     * @return    base64      
     */
    public static String getImageString(String imgFilePath) {
        InputStream inputStream = null;
        byte[] data = null;
        //         
        try {
            inputStream = new FileInputStream(imgFilePath);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //       Base64      
        BASE64Encoder base64Encoder = new BASE64Encoder();
        //    Base64            
        return base64Encoder.encode(data);
    }
}

좋은 웹페이지 즐겨찾기