使用Java套接字发送字节数组

| 我正在编程一个允许从服务器下载pdf文件的应用程序。在服务器端,由于pdfview库,我得到了pdf文件的字节缓冲区。我用字节缓冲区填充字节数组,然后用DataOutputStream发送字节数组。 大多数时候,我会在客户端获得良好的数据,但是有时我会得到一个填充有随机数的数组,因此无法重建pdf文件。我通常会遇到以下错误:\“ java.io.IOException:这可能不是PDF文件\” 因此,当我比较接收的数据和发送的数据时,它是完全不同的。 我注意到服务器部分的数据总是正确的 任何帮助表示赞赏
//Server side
this.in = new ObjectInputStream(this.socket.getInputStream());
this.out = new DataOutputStream(this.socket.getOutputStream()); 
this.outObject = new ObjectOutputStream(this.socket.getOutputStream());

this.res = this.in.readObject().toString();//I read the client order(GET home page, next page...)

//I get the bytebuffer from the pdf file------
this.file = new File (name+this.numFile+\".pdf\");
RandomAccessFile raf;
raf = new RandomAccessFile(this.file, \"r\");
FileChannel channel = raf.getChannel();
this.buf = channel.map(FileChannel.MapMode.READ_ONLY,0, channel.size());
//--------------------------------------------

int size = this.buf.capacity();
this.out.writeInt(size);//I send the size of my bytebuffer to the server

int size_array = 1000;
this.pack = new byte[size_array];
this.pack = clean_array();//I replace my variable by an array filled with zeros

for(long i=0;i<(size/size_array);i++){
buf.get(this.pack);
    this.out.write(this.pack);
    this.pack = clean_array();//I replace my variable by an array filled with zeros
}

//I have not sent the whole bytebuffer, the last byte array could have a different size
//I work out this size, I create the new bytearray and I send it---------------------
int byteLeft = size%size_array;
if(byteLeft>0){
    this.pack = new byte[byteLeft];
buf.get(this.pack);
this.out.write(this.pack);
this.pack = clean_array();//I replace my variable by an array filled with zeros
}

 //-------------------------------------------------

//Client side
int size_array = 1000;

pack =new byte[size_array];
pack = clean_array();

for(int i=0;i<((size/size_array));i++){     
    in.read(pack);
    buf.put(pack);
        pack = clean_array();
}

if(size%size_array>0){
//for the last loop, the number of bytes sent by the server is not equal to 1000
//So I create a byte array with the good size
   pack = new byte[size%size_array];
   in.read(pack);
   buf.put(pack);
   pack = clean_array();
  }
已邀请:
this.in = new ObjectInputStream(this.socket.getInputStream());
this.out = new DataOutputStream(this.socket.getOutputStream());
this.outObject = new ObjectOutputStream(this.socket.getOutputStream());
您在这里不需要DataOutputStream,并且必须在ObjectInputStream之前创建ObjectOutputStream,否则会出现死锁。
this.res = this.in.readObject().toString();//I read the client order(GET home page, next page...)
zz如果下一个对象是String,则此行代码将起作用,但应使用(String)强制转换,而不是toString()。如果下一个对象不是字符串,则说明它已将其破坏为其他内容。
this.pack = new byte[size_array];
this.pack = clean_array();//I replace my variable by an array filled with zeros
无意义。 (a)它已经充满了零,并且(b)如果您坚持第二次分配,那么第一次分配的意义是什么? 代码的其余部分是将文件发送到套接字的漫长且可能错误的方法。这是简单的方法:
FileInputStream fin = new FileInputStream(file);
int count;
byte[] buffer = new byte[8192];
while ((count = fin.read(buffer)) > 0)
  out.write(buffer, 0, count);  // here \'out\' is the socket output stream or whatever you want to wrap around it.

要回复问题请先登录注册