使用java scoket获取接收文件中的额外字节

使用java scoket获取接收文件中的额外字节,java,multithreading,Java,Multithreading,我已经用JavaSocket实现了一个文件传输程序。在这个程序中,从客户端发送一个文件,然后将其下载到服务器中。程序几乎正常工作,但问题是接收字节的长度始终大于从客户端发送的字节长度。例如,我从客户端发送了678888589字节,但当我在服务器上检查收到的文件的长度时,得到了678925260字节。由于这个原因,我在服务器端得到了不同的校验和 这是我的密码: 客户端类: public class Client { final static int ServerPort = 1234

我已经用JavaSocket实现了一个文件传输程序。在这个程序中,从客户端发送一个文件,然后将其下载到服务器中。程序几乎正常工作,但问题是接收字节的长度始终大于从客户端发送的字节长度。例如,我从客户端发送了678888589字节,但当我在服务器上检查收到的文件的长度时,得到了678925260字节。由于这个原因,我在服务器端得到了不同的校验和

这是我的密码: 客户端类:

public class Client  
{ 
    final static int ServerPort = 1234; 
    public static final int BUFFER_SIZE = 1024 * 50;
    private static byte[] buffer;

    public static void main(String args[]) throws UnknownHostException, IOException  
    { 
        Scanner scn = new Scanner(System.in);
        buffer = new byte[BUFFER_SIZE];

    for(int i=0;i<8;i++) {
        Socket s1 = new Socket(ip, ServerPort); 
        DataOutputStream dos1 = new DataOutputStream(s1.getOutputStream());
        SendMessage message = new SendMessage(s1, "test.mp4",dos1);
        Thread t = new Thread(message); 
        System.out.println("Adding this client to active client list"); 
        t.start();
    }
  }
}

class SendMessage implements Runnable{
    String file_name;
    Socket s;
    public final int BUFFER_SIZE = 1024 * 50;
    private byte[] buffer;
    DataOutputStream dos;

public SendMessage(Socket sc,String file_name,DataOutputStream dos) {
    this.file_name = file_name;
    this.s=sc;
    buffer = new byte[BUFFER_SIZE];
    this.dos = dos;
}

@Override
public void run() { 
    File file = new File(file_name);
    try {
        sendFile(file, dos);
        dos.close();
        while(true) {

        }
    } catch (IOException e1) {
        e1.printStackTrace();
    }

} 


public void sendFile(File file, DataOutputStream dos) throws IOException {
    byte[] buffer = new byte[BUFFER_SIZE+1];
    if(dos!=null&&file.exists()&&file.isFile())
    {
        FileInputStream input = new FileInputStream(file);
        dos.writeLong(file.length());

      System.out.println(file.getAbsolutePath());
        int read = 0;
        int totalLength = 0;
        while ((read = input.read(buffer)) != -1) {
            dos.write(buffer);
            totalLength +=read;
            System.out.println("length "+read);
        }
        input.close();
        System.out.println("File successfully sent! "+totalLength);
    }
}

}
公共类客户端
{ 
最终静态int-ServerPort=1234;
公共静态最终整数缓冲区大小=1024*50;
专用静态字节[]缓冲区;
公共静态void main(字符串args[])抛出UnknownHostException、IOException
{ 
扫描仪scn=新扫描仪(System.in);
缓冲区=新字节[缓冲区大小];
对于(int i=0;i 0){
out.write(缓冲区,0,len);
总长度+=长度;

//如果(len您在客户端代码上犯了一个小错误。您正在写入完整的缓冲区,而不是从文件中读取的内容

        while ((read = input.read(buffer)) != -1) {
            dos.write(buffer,0,read);
            totalLength += read;
            System.out.println("length " + read);
        }

成功了!谢谢。谢谢。能告诉我最后一个部分吗?除非套接字关闭,否则无法中断文件接收循环。但如果套接字关闭,我无法向客户端发送ack。这更是一个设计问题。连接后,客户端可以发送发送前发送的数据量。一旦服务器重新启动确定您可以停止循环并发送确认的数据量。
while ((len = in.read(buffer,0,BUFFER_SIZE)) > 0) {
    out.write(buffer, 0, len);
    System.out.println("length "+len);
    if(len<=0)break;
}
        while ((read = input.read(buffer)) != -1) {
            dos.write(buffer,0,read);
            totalLength += read;
            System.out.println("length " + read);
        }