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

时间:2011-04-18 21:56:55

标签: java sockets bytearray

我正在编写一个允许从服务器下载pdf文件的应用程序。在我的服务器端,由于pdfview库,我获得了pdf文件的bytebuffer。我用字节缓冲区填充字节数组,然后用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();
  }

1 个答案:

答案 0 :(得分:1)

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...)

Bzzt。如果下一个对象是String,那么这行代码就可以工作,但它应该使用(String)强制转换,而不是toString()。如果下一个对象不是一个String,那么你刚刚将它损坏为其他东西。

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.