base64转化为图片

一、创建相应的工具类

public class Base64ToPdfUtil {    /**     * @param base64sString base64     * @throws IOException     */    public static void base64StringToPDF(String base64sString) throws IOException {        BASE64Decoder decoder = new BASE64Decoder();        BufferedInputStream bin = null;        FileOutputStream fout = null;        BufferedOutputStream bout = null;        try {            // 将base64编码的字符串解码成字节数组            byte[] bytes = decoder.decodeBuffer(base64sString);            // 创建一个将bytes作为其缓冲区的ByteArrayInputStream对象            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);            // 创建从底层输入流中读取数据的缓冲输入流对象            bin = new BufferedInputStream(bais);            // 指定输出的文件            File file = new File("D:\test.pdf");            // 创建到指定文件的输出流            fout = new FileOutputStream(file);            // 为文件输出流对接缓冲输出流对象            bout = new BufferedOutputStream(fout);            byte[] buffers = new byte[1024];            int len = bin.read(buffers);            while (len != -1) {                bout.write(buffers, 0, len);                len = bin.read(buffers);            }            // 刷新此输出流并强制写出所有缓冲的输出字节,必须这行代码,否则有可能有问题            bout.flush();        } catch (IOException e) {            e.printStackTrace();        } finally {            if(!StringUtils.isEmpty(bin)){                bin.close();            }            if(!StringUtils.isEmpty(fout)){                fout.close();            }            if(!StringUtils.isEmpty(bout)){                bout.close();            }        }    }    /**     *     * @param file 文件     * @return     * @throws IOException     */    public static String PDFToBase64(File file) throws IOException {        BASE64Encoder encoder = new BASE64Encoder();        FileInputStream fin =null;        BufferedInputStream bin =null;        ByteArrayOutputStream baos = null;        BufferedOutputStream bout =null;        try {            fin = new FileInputStream(file);            bin = new BufferedInputStream(fin);            baos = new ByteArrayOutputStream();            bout = new BufferedOutputStream(baos);            byte[] buffer = new byte[1024];            int len = bin.read(buffer);            while(len != -1){                bout.write(buffer, 0, len);                len = bin.read(buffer);            }            //刷新此输出流并强制写出全部缓冲的输出字节            bout.flush();            byte[] bytes = baos.toByteArray();            return encoder.encodeBuffer(bytes).trim();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }finally{            if(!StringUtils.isEmpty(bout)){                bout.close();            }            if(!StringUtils.isEmpty(baos)){                baos.close();            }            if(!StringUtils.isEmpty(bin)){                bin.close();            }            if(!StringUtils.isEmpty(fin)){                fin.close();            }        }        return null;    }    /**     * base64字符串转化成图片     *     * @param imgData 图片编码     *     * @param imgFilePath 存放到本地路径     *     * @return     * @throws IOException     */    public static boolean GenerateImage(String imgData, String imgFilePath) throws IOException {        // 对字节数组字符串进行Base64解码并生成图片        // 图像数据为空        if (imgData == null)            return false;        BASE64Decoder decoder = new BASE64Decoder();        OutputStream out = null;        try {            out = new FileOutputStream(imgFilePath);            // Base64解码            byte[] b = decoder.decodeBuffer(imgData);            for (int i = 0; i < b.length; ++i) {                // 调整异常数据                if (b[i] < 0) {                    b[i] += 256;                }            }            out.write(b);        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally {            if(!StringUtils.isEmpty(out)){                out.flush();                out.close();            }            return true;        }    }}


发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章