zoukankan      html  css  js  c++  java
  • 图片与字符串(base64编码)的转化

    package com.demo;
    import java.util.*;
    import java.io.*;
    
    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;
    
    public class ImageTra {
        
        /*
         * 有给定base64编码的字符串,抓化为图片
         * 先经过解码生成字节数据(byte数组存储)
         * 然后IO六输出
         * */
        public static boolean generateImg(String String, String path) throws IOException{
            if (String == null){
                return false;
            }else{
                OutputStream out = new FileOutputStream(path);
                BASE64Decoder decoder = new BASE64Decoder();
                
                byte[] data = decoder.decodeBuffer(String);
                out.write(data);
                out.flush();
                out.close();
                return true;
            }
        }
        
        /*
         * 给定一张图片,经base64编码转化为字符串,
         * */
        public static String ImgToString(String path) throws IOException{
            InputStream inputStream = new FileInputStream(path);
            //根据图片的字节大小定义byte数组
            byte[] data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
            // base64加密
            BASE64Encoder encoder = new BASE64Encoder();
            return encoder.encode(data);
        }
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            try {
                //当前路径
    //            System.out.println(System.getProperty("user.dir"));
                String string = ImgToString("test.png");
                generateImg(string,"same.png");
                
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    
    }
  • 相关阅读:
    Element-ui 的 slot 关系理解
    关于Delegate委托和Event事件的学习
    JavaScript 中 prototype 与 __proto__
    正向代理与反向代理的个人理解
    MVC和三层架构
    关于SqlDataAdapter的思考
    关于C#连接Oracle数据库
    关于VS配置环境
    富文本的实现
    博客
  • 原文地址:https://www.cnblogs.com/qcblog/p/7565478.html
Copyright © 2011-2022 走看看