zoukankan      html  css  js  c++  java
  • JSON传输图片帮助类

    JSON传输图片帮助类  

    2014-05-27 16:11:22|  分类: Java |  标签:解决方案  java  json  |举报|字号 订阅

     
     
    原理:将图片转换为字节流,再将字节流用base64编码,将编码后的字符串封装到Json串中传输;
     
    *注:sun.misc.BASE64Decoder、sun.misc.BASE64Encoder是JDK自带的类,将MyEclipse的自带JDK换成自己本地JDK即可;
     
    代码:
     
    package com.inlz;
     
    import java.io.FileInputStream;
    import java.io.IOException;
    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;
     
     
    /**
     * Description:用此类将图片转换为字符串,以便将图片封装为JSON进行传输
     * @author 河伯
     * @Date 2014-05-27
     * @version 1.0
     * */
    public class ImgHelper {
        
        /**
         * TODO:将byte数组以Base64方式编码为字符串
         * @param bytes 待编码的byte数组
         * @return 编码后的字符串
         * */
        public static String encode(byte[] bytes){
            return new BASE64Encoder().encode(bytes);
        }
        
        /**
         * TODO:将以Base64方式编码的字符串解码为byte数组
         * @param encodeStr 待解码的字符串
         * @return 解码后的byte数组
         * @throws IOException 
         * */
        public static byte[] decode(String encodeStr) throws IOException{
            byte[] bt = null;  
            BASE64Decoder decoder = new BASE64Decoder();  
            bt = decoder.decodeBuffer(encodeStr);
            return bt;
        }
        
        /**
         * TODO:将两个byte数组连接起来后,返回连接后的Byte数组
         * @param front 拼接后在前面的数组
         * @param after 拼接后在后面的数组
         * @return 拼接后的数组
         * */
        public static byte[] connectBytes(byte[] front, byte[] after){
            byte[] result = new byte[front.length + after.length];
            System.arraycopy(front, 0, result, 0, after.length);
            System.arraycopy(after, 0, result, front.length, after.length);
            return result;
        }
        
        /**
         * TODO:将图片以Base64方式编码为字符串
         * @param imgUrl 图片的绝对路径(例如:D:\jsontest\abc.jpg)
         * @return 编码后的字符串
         * @throws IOException 
         * */
        public static String encodeImage(String imgUrl) throws IOException{
            FileInputStream fis = new FileInputStream(imgUrl);
            byte[] rs = new byte[fis.available()];
            fis.read(rs);
            fis.close();
            return encode(rs);
        }
        
        /**
         * @param args
         */
        public static void main(String[] args) {
            String str;
            try {
                str = encodeImage("D:\MMS_TEST\attachment_jpg.jpg");
                System.out.println(str);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
     
    }
  • 相关阅读:
    使用pod install 出现bad interpreter: No such file or directory
    简单易用且功能丰富的纯Swift下载框架
    Swift主题色顶级解决方案一
    如何基于WKWebView开发一个功能完善的资讯内容页
    关于iPhone X 的适配
    iOS11及Xcode9适配问题汇总
    优豆云
    Mac 网站屏蔽修改
    c语言
    iOS 12 前台通知shouldAlwaysAlertWhileAppIsForeground崩溃问题
  • 原文地址:https://www.cnblogs.com/u0mo5/p/4362307.html
Copyright © 2011-2022 走看看