Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java套接字发送文件,丢失数据_Java_Sockets_Tcp_File Transfer - Fatal编程技术网

使用Java套接字发送文件,丢失数据

使用Java套接字发送文件,丢失数据,java,sockets,tcp,file-transfer,Java,Sockets,Tcp,File Transfer,我试图用Java中的套接字将文件从客户端发送到服务器。当我在同一台机器上测试时,它可以正常工作,但是当我在不同的机器上测试时,我会丢失大量数据,导致文件损坏。如果我尝试发送一个非常小的文件(),您似乎在混合分块数据和面向行的操作。我建议您在服务器上使用,在客户端上使用 private static void sendFile(String path) { if (path == null) { throw new NullPointerException("Path is

我试图用Java中的套接字将文件从客户端发送到服务器。当我在同一台机器上测试时,它可以正常工作,但是当我在不同的机器上测试时,我会丢失大量数据,导致文件损坏。如果我尝试发送一个非常小的文件(),您似乎在混合分块数据和面向行的操作。我建议您在服务器上使用,在客户端上使用

private static void sendFile(String path) {
    if (path == null) {
        throw new NullPointerException("Path is null");
    }

    File file = new File(path);
    Socket socket = null;
    try {
        System.out.println("Connecting to server...");
        socket = new Socket(HOSTNAME, PORT);
        System.out.println("Connected to server at "
                + socket.getInetAddress());

        try (DataOutputStream dos = new DataOutputStream(
                new BufferedOutputStream(socket.getOutputStream()));) {
            dos.writeUTF(file.getName());
            dos.writeLong(file.length());

            System.out.println("Sending " + file.getName() + " ("
                    + file.length() + " bytes) to server...");
            writeFile(file, dos);
            System.out.println("Finished sending " + file.getName()
                    + " to server");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
然后在服务器上

socket = server.accept();

DataInputStream dis = new DataInputStream(socket.getInputStream());
String name = dis.readUTF();
File file = new File(_downloadDir, name);
long fileSize = dis.readLong();
System.out.println("Saving " + file + " from user... ("
        + fileSize + " bytes)");

在您的代码中,您混合了所有不应该混合的内容:(a)输入/输出流,(b)打印流,(c)流读取器和(d)缓冲读取器

您应该使用唯一的对象(流)来写入您的所有员工,并使用唯一的对象(流)来读取您的所有员工

我可以推荐你用这两种方法中的任何一种

OutputStream out = new BufferedOutputStream(socket.getOutputStream()); // for writing
InputStream in = new BufferedInputStream(socket.getInputStream()); // for reading


第一种更灵活,第二种更易于使用。

哇,你说得对,这就解决了它。我没有意识到混合不同类型的流可能会有这么大的问题,尽管现在我想这是有道理的。谢谢,我不会再犯这样的错误了。
socket = server.accept();

DataInputStream dis = new DataInputStream(socket.getInputStream());
String name = dis.readUTF();
File file = new File(_downloadDir, name);
long fileSize = dis.readLong();
System.out.println("Saving " + file + " from user... ("
        + fileSize + " bytes)");
OutputStream out = new BufferedOutputStream(socket.getOutputStream()); // for writing
InputStream in = new BufferedInputStream(socket.getInputStream()); // for reading
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
DataInputStream in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));