byte数组转成String再转成byte数组

wjsq0705 2012-09-04 05:16:10
是这样的,一个图片用流读出来成为byte数组,然后需要通过webService传输,需要转换成String传输。获得后,再转为byte数组用输出流转为图片。

我是这样做的
都到数组byte[] bt;(用流把读出来都得byte)
String [] str=new String[4];
str[0]="1";
str[1]="2";
str[2]=bt.toString();
str[3]="4";
result=call.invoke(str); (这个是WebService的方法,传一个Object数组。我传的是String数组。)


接收到后
byte[] bt=str.getBytes();
再把byte数组用输出流生成图片。可是生成的图片都是空白。
...全文
27203 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wms821 2012-09-05
  • 打赏
  • 举报
回复
String [] str=new String[4];
str[0]="1";
str[1]="2";
str[2]=new sun.misc.BASE64Encoder().encodeBuffer(bt);
str[3]="4";
result=call.invoke(str); (这个是WebService的方法,传一个Object数组。我传的是String数组。)

接收到后,这里要取str的第2个数组元素,因为上面你传的第2个元素才是图片数据;
byte[] bt=new sun.misc.BASE64Decoder().decodeBuffer(str[2]);
wms821 2012-09-05
  • 打赏
  • 举报
回复
接收到的内容与原来的内容不同,
只能说明未从webservice中解析到正确的数据;
你得检查webservice的传输和接收相关的代码,
这个跟编码解码就没关系了;
如果怀疑BASE64对图片的编码解码,可以测试下面的代码;


import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import junit.framework.TestCase;

public class Debug extends TestCase
{
public void test() throws Exception
{
//1.在发送端用BASE64Encoder将二进制数据编码成字符串后再发送;
String srcfile = "G:/workspace/hzjx_wsdl/WebContent/images/wj.jpg";
byte[] bt_src = readFile(srcfile);
String temp = new sun.misc.BASE64Encoder().encodeBuffer(bt_src);

// .....<webservice传输 temp 过程>

//2.在接收端用BASE64Decoder对接收到的字符串解码成二进制数据;再输出生成图片;
byte[] bt_tag = new sun.misc.BASE64Decoder().decodeBuffer(temp);
String tagfile = "G:/abcde.jpg";
saveToFile(bt_tag, tagfile);

System.out.println("请检查目标文件<"+tagfile+">是否与源文件<"+srcfile+">的内容肯定是相同的!");
System.out.println("内容相同证明Base64编码与解码处理过程肯定是正确的!");
}

public static byte[] readFile(String filePath)
{
byte[] arr_result = null;
InputStream in = null;
ByteArrayOutputStream fout = new ByteArrayOutputStream(8192);
try
{
in = new FileInputStream(filePath);
BufferedInputStream fin = new BufferedInputStream(in, 8192);

byte[] data = new byte[8192];
int count = 0;

while (count >= 0)
{
count = fin.read(data);
if (count > 0)
{
fout.write(data, 0, count);
}
}

arr_result = fout.toByteArray();
} catch (Exception e)
{
e.printStackTrace();
} finally
{
if (in != null)try{in.close();} catch (Exception ex){}
if (fout != null)try{fout.close();} catch (Exception ex){}
}

return arr_result;
}

public static void saveToFile(byte[] bt, String filePath)
{
OutputStream os = null;
try
{
File savefile = new File(filePath);
os = new FileOutputStream(savefile);
os.write(bt);
} catch (IOException e)
{
e.printStackTrace();
} finally
{
if (os != null){try{os.close();} catch (Exception ex){}}
}
}

}
MiceRice 2012-09-05
  • 打赏
  • 举报
回复
结贴了?那么问题找到了么?

byte[] 转BASE64,容量一定会变大的;使用时必须再用BASE64算法逆转回byte[],容量就会恢复正常。
wjsq0705 2012-09-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
二进制数据要转成字符串来传输是需要经过编码(例如BASE64)处理才可以的;

1.在发送端用BASE64Encoder将二进制数据编码成字符串后再发送;
byte[] bt = <bt是读取到的图片的二制数据>;
String temp = new sun.misc.BASE64Encoder().encodeBuffer(bt);

2.在接收端用BASE64Decoder对接收到……
[/Quote]

先谢谢你们,我使用了BASE64编码是传输了过去,程序运行不报错,可问题是,原来图片5K过去后变成6K了。
而且不能显示。

我贴下我的代码。
public String [][] getReport(String uname,String pwd,String filePath,String fileName){
String [] str=new String[4];
str[0]=uname;
str[1]=pwd;
filePath="G:/workspace/hzjx_wsdl/WebContent/images/wj.jpg";
File file=new File(filePath);
InputStream is=null;
if(file.exists()){
System.out.println("OK!");
try {
is=new FileInputStream(file);
} catch (FileNotFoundException e) {

e.printStackTrace();
}
}else{
System.out.println("NO!");
}
byte[] bt =new byte[4096];

try {
is.read(bt,0,4096);
} catch (IOException e) {
e.printStackTrace();
}
String st=new String(bt);
String temp = new sun.misc.BASE64Encoder().encodeBuffer(bt);
str[2]=temp;//这个就是我需要传输的主要内容,图片。
fileName="123123213S";
str[3]=fileName;
return (String[][]) this.setSoap("getReport",str);
}


以上是准备调用webservice准备的内容。
public static void fileDownload(byte[] bt ,String fileName) {
String nowTime=Long.toString(System.currentTimeMillis());
File savefile=new File("G:/JAVA/"+nowTime+".jpg");
System.out.println(savefile.toString());
try {
OutputStream os=new FileOutputStream (savefile);
os.write(bt);
} catch (IOException e) {
e.printStackTrace();
}

}


这边是我解析的内容。大神帮我看看哪边出的错。
wms821 2012-09-04
  • 打赏
  • 举报
回复 2
二进制数据要转成字符串来传输是需要经过编码(例如BASE64)处理才可以的;

1.在发送端用BASE64Encoder将二进制数据编码成字符串后再发送;
byte[] bt = <bt是读取到的图片的二制数据>;
String temp = new sun.misc.BASE64Encoder().encodeBuffer(bt);

2.在接收端用BASE64Decoder对接收到的字符串解码成二进制数据;再输出生成图片;
byte[] bt = new sun.misc.BASE64Decoder().decodeBuffer(temp);

BASE64编码参见: http://baike.baidu.com/view/1485202.htm
iGoodLoser 2012-09-04
  • 打赏
  • 举报
回复
你这是什么逻辑啊楼主没看明白!
为什么要转来转去的?
还有,str不是字符串数组吗怎么会有getBytes()方法呢?
直接将图片读取到byte数组中,然后通过webservice传输获得后生成图片不行吗?跟字符串类型没多大关系吧?
webservice可以传递byte数组吧,不用转换成字符串或者字符串数组吧!
  • 打赏
  • 举报
回复
String s2 =new String(bytes);

byte[] bytes2 = s2.getBytes();
这样不行吗
MiceRice 2012-09-04
  • 打赏
  • 举报
回复
byte[]转成String的时候,请用BASE64来转码,否则会因为字符转换问题(不是所有的byte组合都能映射为char),导致内容丢失。

反之也用BASE64来重新得到byte[]。

可以用Apache的Commons包,里面提供了BASE64工具类。

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧