Java 数据输入流长度
我正在为学校作业创建一个文件服务器应用程序。我目前拥有的是一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在发送文件之前添加一个“长度字段”。 (请注意,由于您将文件读取到内存,因此文件的最大大小可以为 ~2GB。)
在发送文件之前写入文件的长度:
接收时首先读取长度并使用它作为长度:
更好的方法是不首先将文件读入内存,而是直接从
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:
And when receiving read the length first and use it as a length:
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!如果您知道长度,则使用 readFully() 比一次读取一个字节要高效得多。
在这种情况下,您不需要知道长度,您可以编写循环来读取/写入尽可能多的数据。
您可以通过在读取数据时复制数据来避免将整个文件读取到内存中。
您可以使用 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.
You can avoid reading the whole file into memory by copying the data as you read it.
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)