Java 数据输入流长度

发布于 2024-11-06 07:04:52 字数 1728 浏览 0 评论 0原文

我正在为学校作业创建一个文件服务器应用程序。我目前拥有的是一个简单的 Client 类,它通过 TCP 发送图像,还有一个 Server 类,它接收图像并将其写入文件。

这是我的客户端代码

import java.io.*;
import java.net.*;

class Client {
    public static void main(String args[]) throws Exception {
        long start = System.currentTimeMillis();
        Socket clientSocket = new Socket("127.0.0.1", 6789);
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());

        File file = new File("hot.jpg");
        FileInputStream fin = new FileInputStream(file);
        byte sendData[] = new byte[(int)file.length()];
        fin.read(sendData);

        outToServer.write(sendData, 0, sendData.length);
        clientSocket.close();

        long end = System.currentTimeMillis();
        System.out.println("Took " + (end - start) + "ms");
    }
}

,这是我的服务器代码。

import java.io.*;
import java.net.*;

class Server {
    public static void main(String args[]) throws Exception {
        ServerSocket serverSocket = new ServerSocket(6789);
        Socket connectionSocket = serverSocket.accept();
        DataInputStream dis = new DataInputStream(connectionSocket.getInputStream());

        byte[] receivedData = new byte[61500]; // <- THIS NUMBER

        for(int i = 0; i < receivedData.length; i++)
            receivedData[i] = dis.readByte();

        connectionSocket.close();
        serverSocket.close();

        FileOutputStream fos = new FileOutputStream("received.jpg");
        fos.write(receivedData);
        fos.close();
    }
}

我的问题是如何获取正在发送的文件的大小。如果您检查 Server 代码,您会发现我现在已经对数字进行了硬编码,即 61500。如何动态检索这个号码?

或者,我这样做的方式错误吗?替代解决方案是什么?

I am creating a file server application for school assignment. What I currently have is a simple Client class that sends an image through TCP and a Server class that receives it and writes it to the file.

this is my client code

import java.io.*;
import java.net.*;

class Client {
    public static void main(String args[]) throws Exception {
        long start = System.currentTimeMillis();
        Socket clientSocket = new Socket("127.0.0.1", 6789);
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());

        File file = new File("hot.jpg");
        FileInputStream fin = new FileInputStream(file);
        byte sendData[] = new byte[(int)file.length()];
        fin.read(sendData);

        outToServer.write(sendData, 0, sendData.length);
        clientSocket.close();

        long end = System.currentTimeMillis();
        System.out.println("Took " + (end - start) + "ms");
    }
}

and this is my server code.

import java.io.*;
import java.net.*;

class Server {
    public static void main(String args[]) throws Exception {
        ServerSocket serverSocket = new ServerSocket(6789);
        Socket connectionSocket = serverSocket.accept();
        DataInputStream dis = new DataInputStream(connectionSocket.getInputStream());

        byte[] receivedData = new byte[61500]; // <- THIS NUMBER

        for(int i = 0; i < receivedData.length; i++)
            receivedData[i] = dis.readByte();

        connectionSocket.close();
        serverSocket.close();

        FileOutputStream fos = new FileOutputStream("received.jpg");
        fos.write(receivedData);
        fos.close();
    }
}

My question is how to get the size of the file that is being sent. If you check the Server code you'll see that I've hardcoded the number i.e. 61500 at the moment. How can I retrieve this number dynamically?

Or, am I doing this the wrong way? What an alternative solution would be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何以笙箫默 2024-11-13 07:04:52

在发送文件之前添加一个“长度字段”。 (请注意,由于您将文件读取到内存,因此文件的最大大小可以为 ~2GB。)


在发送文件之前写入文件的长度:

  outToServer.writeInt(sendData.length);

接收时首先读取长度并使用它作为长度:

  int dataLength = dis.readInt()
  byte[] receivedData = new byte[dataLength];

更好的方法是首先将文件读入内存,而是直接从FileInputStream传输它 - 然后你可以传输更大的文件!

Add one "length field" before sending the file. (Note that since you read the file to memory the maximum size of the file can be ~2GB.)


Before sending the file write the length of the file:

  outToServer.writeInt(sendData.length);

And when receiving read the length first and use it as a length:

  int dataLength = dis.readInt()
  byte[] receivedData = new byte[dataLength];

A better way would be not to read the file into memory first but to transfer it directly from the FileInputStream - then you could transfer bigger files!

深陷 2024-11-13 07:04:52

如果您知道长度,则使用 readFully() 比一次读取一个字节要高效得多。

在这种情况下,您不需要知道长度,您可以编写循环来读取/写入尽可能多的数据。

InputStream is = connectionSocket.getInputStream();

byte[] bytes = new byte[8192];
int len;
while((len = is.read(bytes)) > 0)
    fos.write(bytes, 0, len);

您可以通过在读取数据时复制数据来避免将整个文件读取到内存中。

FileInputStream fis = new FileInputStream(filename);
OutputStream os = socket.getOutputStream();

byte[] bytes = new byte[8192];
int len;
while((len = fis.read(bytes)) > 0)
    os.write(bytes, 0, len);

您可以使用 Apache IOUtils.copy()如果您愿意,可以执行从一个流到另一个流的复制。

此方法的优点是文件可以是任意大小(大于 2 GB)。使用数组限制为 2 GB(并使用更多内存)

If you know the length, using readFully() is much more efficient than reading a byte at a time.

In this case, you don't need to know the length, you can write the loop to read/write as much data as you get.

InputStream is = connectionSocket.getInputStream();

byte[] bytes = new byte[8192];
int len;
while((len = is.read(bytes)) > 0)
    fos.write(bytes, 0, len);

You can avoid reading the whole file into memory by copying the data as you read it.

FileInputStream fis = new FileInputStream(filename);
OutputStream os = socket.getOutputStream();

byte[] bytes = new byte[8192];
int len;
while((len = fis.read(bytes)) > 0)
    os.write(bytes, 0, len);

You can use Apache IOUtils.copy() to perform the copy from one stream to another if you wish.

This approach has the advantage that the file can be any size (greater than 2 GB). Using an array is limited to 2 GB (and uses more memory)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文