Java TCP不';我不能发送准确的文件

Java TCP不';我不能发送准确的文件,java,file,tcp,Java,File,Tcp,好的,我一直在开发一个轻量级TCP传输应用程序,它几乎完成了,但是,我遇到了一个问题: 每当我尝试发送一个文件时,它确实会被发送,但当它到达时,它总是比发送的文件大几个字节。例如,如果我发送一张111093字节大的图片,当它被接收时,它的大小是111616字节。(大523字节) 我仍然可以打开图像等等,但是如果我发送and.EXE或.JAR,它会在尝试启动时给我错误,因为这些额外的字节 这是我的密码: 发件人: public static void sendFileToServer(Fi

好的,我一直在开发一个轻量级TCP传输应用程序,它几乎完成了,但是,我遇到了一个问题: 每当我尝试发送一个文件时,它确实会被发送,但当它到达时,它总是比发送的文件大几个字节。例如,如果我发送一张111093字节大的图片,当它被接收时,它的大小是111616字节。(大523字节)

我仍然可以打开图像等等,但是如果我发送and.EXE或.JAR,它会在尝试启动时给我错误,因为这些额外的字节

这是我的密码:

发件人:

    public static void sendFileToServer(File file) {
    try {
        byteBuffer = new byte[1024];

        //PREPARING FOR TRANSFER
        output.writeObject("FILE:"+file.getName()+":" + byteBuffer.length);
        output.flush();

        Client.lblData.setText("Sending file...");

        //TRANSFERRING
        BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file));

        byte[] buffer = new byte[1024];

        while ((fis.read(buffer)) > 0) {
            output.write(buffer, 0, buffer.length);
        }
        output.flush();
        fis.close();

        //TRANSFERRING
        output.writeObject("END");
        output.flush();

        Client.lblData.setText("File sent!");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {}
        Client.lblData.setText("Waiting...");

    } catch (IOException e) {
        e.printStackTrace();
    }
}
接受者:

if(file) {
try {
    message = (String) input.readObject();
    if(message.contains("END")) {
        Server.conLabel.setText("Connected to: " + socket.getInetAddress().getHostName() + ":" + socket.getPort());
    fos.flush();
    fos.close();
    file = false;
    }
} catch (IOException | ClassNotFoundException e) {}

byte[] buffer = new byte[65536];
int number;

while ((number = input.read(buffer)) != -1) {
    fos.write(buffer,0,number);
}

} else {
    if (message.startsWith("FILE")) {
    //PREPARING FOR FILE TRANSFER
        file = true;

        Server.conLabel.setText("Recieving file...");

        for(int i = 0; i < Server.files.getSize(); i++) {
            if(Server.files.getElementAt(i).equals(message.split(":")[1])) {
            Server.list.setSelectedIndex(i);

            if(!new File(Server.dlLoc.getText()).exists()) {
            new File(Server.dlLoc.getText()).mkdirs();
            }
            if(!new File(Server.dlLoc.getText() + "/" + message.split(":")[1]).exists()) {
            new File(Server.dlLoc.getText() + "/" + message.split(":")[1]).createNewFile();
            }

            fos = new FileOutputStream(new File(Server.dlLoc.getText() + "/" + message.split(":")[1]));
        }
    }
}
if(文件){
试一试{
message=(字符串)input.readObject();
if(message.contains(“END”)){
Server.conLabel.setText(“连接到:”+socket.getInetAddress().getHostName()+”:“+socket.getPort());
fos.flush();
fos.close();
file=false;
}
}catch(IOException | ClassNotFoundException e){}
字节[]缓冲区=新字节[65536];
整数;
而((数字=输入。读取(缓冲区))!=-1){
fos.写入(缓冲区,0,数字);
}
}否则{
if(message.startsWith(“文件”)){
//准备文件传输
file=true;
Server.conLabel.setText(“接收文件…”);
对于(int i=0;i
}

而且,这是我第一次做这样的事情,所以请告诉我这些代码中其他有用的东西。

111616=1024*109

您正在发送方上写入完整的缓冲区,而没有注意最后一个缓冲区很短这一事实。改为这样做:

    int len;
    while ((len = fis.read(buffer)) > 0) {
        output.write(buffer, 0, len);
    }
111616=1024*109

您正在发送方上写入完整的缓冲区,而没有注意最后一个缓冲区很短这一事实。改为这样做:

    int len;
    while ((len = fis.read(buffer)) > 0) {
        output.write(buffer, 0, len);
    }

还有一个问题,你知道我会怎么看文件传输了多少吗?(对于progressbar)在开始之前(使用
java.io.file
方法)确定总文件大小,然后保持(
len
)值的运行总和,并根据需要调整进度条。另外,还有一个问题,您知道我将如何看到文件传输了多少吗?(对于progressbar)在开始之前(使用
java.io.file
方法)确定总文件大小,然后保持(
len
)值的运行总和,并根据需要调整进度条。