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

标签 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();
}

当客户端接收文件的一部分作为 fileSize 的输入时,错误是 NumberFormatException,在第一个循环之后发生。

最佳答案

确保先刷新 PrintWriter,然后再将原始字节直接写入 PrintWriterOutputStream附于.否则,您可能会将任何缓冲区数据乱序写入底层套接字。

但更重要的是,如果您在接收端使用缓冲读取,请确保您使用接收文件名和文件大小的同一缓冲区读取文件字节。您还应该使用较小的固定 block 传输文件,不要为整个文件大小分配单个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();
}

关于java - 如何通过 byte[] 流读取多个文件的 Socket InputStream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43459604/

相关文章:

java - Java 中的 volatile int 是线程安全的吗?

java - 尝试使用 JNLP 部署时出现 ClassNotFoundException?

PHP - chmod 将无法正常工作

Android:如何将目录中的所有文件列出到数组中?

Android 应用程序图标 文件管理器

python - C 和 Python 之间的 Unix 套接字

Java蓝牙套接字重新启动后不接受连接

java - 不支持的 major.minor 版本 52.0

java - 将 char 返回到 main 方法

java - 如何在java中监听我的系统中已经运行的端口的数据