public void run(){

byte[] temp = new byte[1024];
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
char filename[]= new char[100];
try{
int length; while((length=in.read(temp))!= -1){
buffer.write(temp, 0, length);
System.out.println("gets: 2");
}
int Size = buffer.size();
System.out.println("buffer长度是:"+Size);
byte[] FileInfo = new byte[Size];
for(int c = 0; c < Size; ++c){
FileInfo[c] = 0;

}
FileInfo = buffer.toByteArray();
for(int z = 0; z<104; ++z){
System.out.println("FileInfo["+z+"] = "+FileInfo[z]);
}
byte[] ByteFileName=new byte[100];
for(int i = 0; i < ByteFileName.length; ++i){
ByteFileName[i] = 0;
}
byte[] ByteFileSize = new byte[4];

System.out.println("数组FileInfo长度是:"+FileInfo.length);
System.arraycopy(FileInfo, 0, ByteFileSize, 0, 4);
System.out.println("gets: 3");
System.arraycopy(FileInfo, 4, ByteFileName, 0, filename.length);
for(int b=0; b<filename.length; ++b){
filename[b] = 0;

}
for(int j = 0; j < ByteFileName.length; ++j){ //打印对比,找出出现方格的地方及原因
System.out.println("ByteFileName["+j+"] = "+ByteFileName[j]);
//filename[j] = (char)(ByteFileName[j]);
//System.out.println("filename["+j+"] = "+filename[j]);
}
StringBuffer sBuffer = new StringBuffer();
for(int a = 0; a < filename.length; ++a){
sBuffer.append(filename[a]);
}
//String s = sBuffer.toString().trim();
String FileName = new String(ByteFileName, "GBK");

int Length = FileInfo.length - 104;
byte[] ByteFileData = new byte[Length];
System.arraycopy(FileInfo, 104, ByteFileData, 0, Length);

System.out.println("FileName is:"+FileName);
//Byte to Int
int FileSize = (ByteFileSize[0] & 0xff) | ((ByteFileSize[1] << 8) & 0xff00) // | 表示安位或   
| ((ByteFileSize[2] << 24) >>> 8) | (ByteFileSize[3] << 24);
System.out.println("FileSize is:"+FileSize);
File dir = new File("/data/data/com.FileDemo.Activity/files");
if(!dir.exists()){
dir.mkdirs();
}
System.out.println("文件名长度是:"+FileName.length());
File file = new File("/data/data/com.FileDemo.Activity/files/2.bmp");
if(!file.exists()){
file.createNewFile();
}
OutputStream os = new FileOutputStream(file);
os.write(ByteFileData, 0, ByteFileData.length);
os.close();
handler.post(runnableImg);
in.close();
sk.close();
//this.destroy();
}catch(Exception e){
e.printStackTrace();
}
}服务器是VC++的,客户端是Android的。从服务器发给客户端一个图片信息,包含一个struct和图片的内容数据,struct是int类型图片内容长度和关于图片名称char【100】数组。
这里有2个问题:
1.经测试发现,在FileInfo = buffer.toByteArray()开始,取到的图片名称后面带有乱码,即用于存放图片名称的byte数组有100个字节,除了需要的图片名称还有余下无用字节,这是为什么?
2。我自己创建一个2.bmp的图像文件,把传过来的图片数据往里面写入却不能在模拟器上画出来。这又是什么原因?
求求各位大神的帮助啊,毕业设计的,不能不急啊!

解决方案 »

  1.   

    第一个问题:后边的冗余字段不是网络填补码吗?这个我记得你在另一个帖子里问过了
    第二个问题:网络的传输的分片问题,在你的代码中还是没考虑进去。你接到的buffer不一定一下就能接收完全
      

  2.   

    第一个问题我已经解决了,原因是处在我就收的信息一次性存在buffer里面,文件名称和文件内容数据之间是没有间隙的,即是说那些乱码就是图片的内容。
    新的问题又来了,我收到的文件大小(struct里面的那个文件大小)比我实际收到的图片内容字符长度小。这是什么原因?我那样写不是分片吗?不懂!我觉得分不分片没多大关系吧,毕竟我们传的图片也不大。
      

  3.   

    你那么写当然没考虑了,建议做个变量记录,等图片完全传完,buffer取下整张图片后再显示
      

  4.   

    先dump文件内容块,看图片内容是否完整;再调试显示。。
      

  5.   

    楼主你好,我也做linux发送图片到安卓手机端。手机端也没有显示图片。可以把你代码发一份给我参考么?