笔记:使用json传输图片,根据个人经验:我做不到,想了个办法将文件转变成字符串并压缩
来源:互联网 发布:淘宝店铺交易平台 编辑:程序博客网 时间:2024/05/17 02:39
/** * 把字节数组转化为字符串----"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
- 笔记:使用json传输图片,根据个人经验:我做不到,想了个办法将文件转变成字符串并压缩
- 一直使用的图片压缩的代码,做个笔记
- 将对象转变成JSON
- 根据URL下载文件,将图片等比例压缩(可指定宽高),按原图压缩,根据指定分隔符取字符串,将文本信息追加到指定文本
- 如何将word中的图片转变成eps文件插入latex
- js压缩上传的图片,并转化为base64字符串传输
- 将图片转变成手绘图
- Java使用Socket进行字符串和图片文件同时传输
- 将图片文件读入字符串并进行base64加密
- 我有必要给英语学习做个笔记了
- 将字符串压缩入文件
- 在linux环境下将java对象转成json字串并使用AES加密传输数据
- 我做了个决定
- 1个人2个月做到6000W的阅读量,我都做了什么?
- 利用DotNetZip做了个多文件打包下载电子期刊并在本地可以正常使用电子期刊
- 忘记我做不到
- 图片压缩问题解决办法
- 根据图片路径压缩图片并返回压缩后图片的路径
- 进制转换增强版模板
- 打印九九乘法表
- 字节流
- Nginx编译安装
- 经典并查集HDU1232
- 笔记:使用json传输图片,根据个人经验:我做不到,想了个办法将文件转变成字符串并压缩
- RAM和 flash这个东西
- MPI求素数个数
- 俄罗斯乘法
- 让渡价值
- 阿里面试题
- 哥德巴赫猜想CodeForce382Div2 D
- 17年苹果强推ATS,看了好多免费证书。。。
- Android 不停旋转图片