当前位置: 代码迷 >> Android >> Android客户端接收图片信息的有关问题
  详细解决方案

Android客户端接收图片信息的有关问题

热度:123   发布时间:2016-05-01 21:26:25.0
Android客户端接收图片信息的问题!
Java code
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个字节,除了需要的图片名称还有余下无用字节,这是为什么?
  相关解决方案