笔记:使用json传输图片,根据个人经验:我做不到,想了个办法将文件转变成字符串并压缩

/**
  * 把字节数组转化为字符串----"ISO-8859-1"
  * @param data
  * @return
  */
 public   static  String byteToString(byte[] data){
      String dataString=null;
        try{
             //将字节数组转为字符串,编码格式为ISO-8859-1
             dataString=new String(data,"ISO-8859-1");
            }catch(Exception e){
                e.printStackTrace();
            }
         return dataString;
     }
/**
 * 压缩字符串----"ISO-8859-1"
  * @param data
  * @return
  */
 public  static  String compress(String data){
         String finalData=null;
         try{
                 //打开字节输出流
               ByteArrayOutputStream bout=new ByteArrayOutputStream();
               //打开压缩用的输出流,压缩后的结果放在bout中
                GZIPOutputStream gout=new GZIPOutputStream(bout);
                //写入待压缩的字节数组
                 gout.write(data.getBytes("ISO-8859-1"));
                 //完成压缩写入
                 gout.finish();
                 //关闭输出流
                 gout.close();
                finalData=bout.toString("ISO-8859-1");
             }catch(Exception e){
                 e.printStackTrace();
             }
         return finalData;
     }
/**
 2 * 将图片转换为字节数组
 3 * @return
 4 */
 public static byte[] loadImage(File file){
         //用于返回的字节数组
        byte[] data=null;
         //打开文件输入流
         FileInputStream fin=null;
         //打开字节输出流
         ByteArrayOutputStream bout=null;
        try{
                 //文件输入流获取对应文件
                fin=new FileInputStream(file);
                 //输出流定义缓冲区大小
                bout=new ByteArrayOutputStream((int)file.length());
                 //定义字节数组,用于读取文件流
                 byte[] buffer=new byte[1024];
                 //用于表示读取的位置
                 int len=-1;
                //开始读取文件
                 while((len=fin.read(buffer))!=-1){
                         //从buffer的第0位置开始,读取至第len位置,结果写入bout
                        bout.write(buffer,0,len);
                     }
                //将输出流转为字节数组
                 data=bout.toByteArray();
                 //关闭输入输出流
                 fin.close();
                 bout.close();
             }catch(Exception e){
                 e.printStackTrace();
             }
         return data;
     }

/**
 * 解压字符串
 * @param str
 * @return
 */
public  static  String decompressionString(String str){
    ByteArrayOutputStream out=null;
    ByteArrayInputStream in =null;
    GZIPInputStream gunzip =null;
    try {
        out = new ByteArrayOutputStream();
        in = new ByteArrayInputStream(str
                .getBytes("ISO-8859-1"));
        gunzip = new GZIPInputStream(in);
        byte[] buffer = new byte[256];
        int n;
        while ((n = gunzip.read(buffer))!=-1) {
            out.write(buffer, 0, n);
        }
        return out.toString("ISO-8859-1");
    }catch (IOException e){
        e.printStackTrace();
        return  null;
    }finally {
        close(out);
        close(in);
        close(gunzip);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值