java - 当我使用套接字通过本地网络传输文件时,如何防止文件损坏?

标签 java file sockets tcp file-handling

我正在开展一个学校项目,我想制作一个个人存储服务器。目前,我想要实现的是将文件从客户端计算机传输到服务器。但是,当使用图像对此进行测试时,文件会在损坏之前部分发送。 请记住,我是一名相当新的程序员,我的技术知识可能有些有限。

我通过 DataOutputStream 使用字节数组来传输文件。我想使用此方法,因为它适用于任何文件类型。我尝试将缓冲区大小设置为文件的确切大小或更大,但都不起作用。

服务器:

public void run() {
            try {
                System.out.println("ip: " + clientSocket.getInetAddress().getHostAddress());

                out = new DataOutputStream(clientSocket.getOutputStream());
                in = new DataInputStream(clientSocket.getInputStream());

                in.read(buffer, 0, buffer.length);

                fileOut = new FileOutputStream("X:\\My Documents\\My 
                Pictures\\gradient.jpg");

                fileOut.write(buffer, 0, buffer.length);
                in.close();
                out.close();
                clientSocket.close();

         } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }

客户:

 public void startConnection(String ip, int port) {
        try {
            clientSocket = new Socket(ip, port);
            out = new DataOutputStream(clientSocket.getOutputStream());
            in = new DataInputStream(clientSocket.getInputStream());

            x = false;
            Path filePath = Paths.get("C:\\Users\\georg\\Documents\\gradient.jpg");
            buffer = Files.readAllBytes(filePath);
            Thread.sleep(3000);
            //Files.write(filePath, buffer);
            //out.write(buffer,0,buffer.length);
            x = true;
            sendMessage(buffer);


        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        } catch (InterruptedException ex) {
            Logger.getLogger(PCS_Client.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public byte[] sendMessage(byte[] buffer) {
        if (x==true){
        try {
            out.write(buffer,0,buffer.length);

        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }

    }
        return null;
    }

以下是我尝试发送的文件与我收到的文件的比较: /image/UuaNh.jpg

奇怪的是,发送单色图像会在服务器上生成单色图像。我相信这里的问题可能在于代码执行的时间,但是我不确定也不知道如何修复它。

最佳答案

问题出在您的服务器代码中,在这一行:

in.read(buffer, 0, buffer.length);

您希望一次读取所有数据,但如果您阅读文档,您会发现:

public final int read(byte[] b, int off, int len) throws IOException

Reads up to len bytes of data from the contained input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read, possibly zero. The number of bytes actually read is returned as an integer.

重要的部分是读取最多 len 个字节的数据。

您必须使用 read 的返回值并重复调用它 read ,直到没有更多内容可供读取。

关于java - 当我使用套接字通过本地网络传输文件时,如何防止文件损坏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58376546/

相关文章:

java.class 文件的目录结构与源文件相同吗?

python - 在Python中写入文件的同一行

Java服务器-客户端socket通信

c - C 中套接字的频繁读/写操作

Java jackson : serialize a class with two field instead of all class

java - Android 构造函数和 onCreate() 有什么区别?

java - 使用 JSF 和托管 Bean 将列表框的内容绑定(bind)到另一个列表框中的选定项

c - 使用C直接读/写磁盘

c - 在二进制文件中搜索模式

php - 越过服务器,如何读取直到当前传输结束?