如何通过byte []流读取多个文件的Socket InputStream?

时间:2017-04-17 21:06:03

标签: java file sockets stream transfer

我在下面发布的代码适用于通过套接字进行单个文件传输。但它不适用于通过套接字进行多个文件传输。当通过套接字尝试多个文件传输时,代码崩溃。

我通过循环遍历服务器发送代码x次数来发送多个文件,并运行接收代码x次。当尝试发送多个文件时,第一个文件将成功发送,第二个文件名和大小将被成功读取,但我的代码中的错误发生在此之后。

在我的接收客户端中,我尝试使用此处发布的建议:Java multiple file transfer over socket但没有成功。

错误发生在客户端。

我问的问题是:为什么这段代码不能用于多个文件,我该如何解决?

服务器发送

try{
    byte[] bytes = new byte[(int)file.length()];
    FileInputStream fis = new FileInputStream(file);
    OutputStream os = socket.getOutputStream();

    out.println(file.getName()); // Send Filename
    out.println(file.length()); // Send filesize

    int count;
    while ((count = fis.read(bytes)) > 0) {
      os.write(bytes, 0, count);
    }
    os.flush();
    fis.close();
  }catch(IOException e){
    e.printStackTrace();
  }
}

客户接收

try{
  String file = in.readLine(); // Read filename
  int fileSize = Integer.parseInt(in.readLine()); // Read Filesize 
  //ERROR HAPPENING ON LINE ABOVE IN LOOPS AFTER THE FIRST
  byte [] buf  = new byte [fileSize];
  FileOutputStream fos = new FileOutputStream(file);
  InputStream is = socket.getInputStream();

  int count = 0;
  while (fileSize > 0 && (count = is.read(buf, 0, (int)Math.min(buf.length, fileSize))) != -1){
    fos.write(buf, 0, count);
    fileSize -= count;
  }

  fos.close();
}catch(IOException e){
  e.printStackTrace();
}

错误是NumberFormatException,在客户端接收文件的一部分以进行fileSize输入时的第一个循环之后。

1 个答案:

答案 0 :(得分:1)

在将原始字节直接写入flush所附加的PrintWriter之前,请务必OutputStream PrintWriter。否则,您可以将任何缓冲区数据无序地写入底层套接字。

但更重要的是,请确保如果在接收端使用缓冲读取,则使用接收文件名和文件大小的相同缓冲区读取文件字节。您还应该使用较小的固定块传输File,不要为整个文件大小分配单个byte[]数组,这会浪费大文件的内存,并且可能会失败。

服务器:

try{
    byte[] bytes = new byte[1024];

    FileInputStream fis = new FileInputStream(file);
    OutputStream os = socket.getOutputStream();
    BufferedOutputStream bos = new BufferedOutputStream(os);

    PrinterWriter pw = new PrintWriter(bos);
    pw.println(file.getName()); // Send Filename
    pw.println(file.length()); // Send filesize
    pw.flush();

    int count;
    while ((count = fis.read(bytes)) > 0) {
      bos.write(bytes, 0, count);
    }
    bos.flush();
    fis.close();
  }catch(IOException e){
    e.printStackTrace();
  }
}

客户端:

try{
  byte [] buf  = new byte [1024];

  FileOutputStream fos = new FileOutputStream(file);
  InputStream is = socket.getInputStream();
  BufferedInputStream bis = new BufferedInputStream(is);

  InputStreamReader isr = new InputStreamReader(bis);
  String file = isr.readLine(); // Read filename
  long fileSize = Long.parseLong(isr.readLine()); // Read Filesize 

  int count = 0;
  while ((fileSize > 0) && (count = bis.read(buf, 0, (int)Math.min(buf.length, fileSize))) > 0){
    fos.write(buf, 0, count);
    fileSize -= count;
  }

  fos.close();
}catch(IOException e){
  e.printStackTrace();
}

话虽如此,您也可以考虑使用DataOutputStream.writeLong()DataInputStream.readLong()以原始二进制格式发送/接收文件大小,而不是文本字符串:

服务器:

try{
    byte[] bytes = new byte[1024];

    FileInputStream fis = new FileInputStream(file);
    OutputStream os = socket.getOutputStream();
    BufferedOutputStream bos = new BufferedOutputStream(os);

    PrinterWriter pw = new PrintWriter(bos);
    pw.println(file.getName()); // Send Filename
    pw.flush();

    DataOutputStream dos = new DataOutputStream(bos);
    dos.writeLong(file.length()); // Send filesize
    dos.flush();

    int count;
    while ((count = fis.read(bytes)) > 0) {
      bos.write(bytes, 0, count);
    }
    bos.flush();
    fis.close();
  }catch(IOException e){
    e.printStackTrace();
  }
}

客户端:

try{
  byte [] buf  = new byte [1024];

  FileOutputStream fos = new FileOutputStream(file);
  InputStream is = socket.getInputStream();
  BufferedInputStream bis = new BufferedInputStream(is);

  InputStreamReader isr = new InputStreamReader(bis);
  String file = isr.readLine(); // Read filename

  DataInputStream dis = new DataInputStream(bos);
  long fileSize = dis.readLong(); // Read Filesize 

  int count = 0;
  while ((fileSize > 0) && (count = bis.read(buf, 0, (int)Math.min(buf.length, fileSize))) > 0){
    fos.write(buf, 0, count);
    fileSize -= count;
  }

  fos.close();
}catch(IOException e){
  e.printStackTrace();
}