Java读取大于2GB的文件(使用分块)

发布于 2024-12-17 20:28:15 字数 578 浏览 0 评论 0原文

我正在实现一个文件传输服务器,但在通过网络发送大于 2 GB 的文件时遇到了问题。当我获得我想要使用的File并尝试将其内容读入byte[]时,问题就开始了。我有一个 for 循环:

for(long i = 0; i < fileToSend.length(); i += PACKET_SIZE){
    fileBytes = getBytesFromFile(fileToSend, i);  


其中 getBytesFromFile()fileToSend 读取 PACKET_SIZE 字节数,然后将其发送到客户端for 循环。 getBytesFromFile() 使用 i 作为偏移量;但是,FileInputStream.read() 中的偏移变量必须是 int。我确信有更好的方法将该文件读入数组,只是我还没有找到。

我宁愿不使用 NIO,尽管我将来会改用它。满足我的疯狂:-)

I'm implementing a file transfer server, and I've run into an issue with sending a file larger than 2 GB over the network. The issue starts when I get the File I want to work with and try to read its contents into a byte[]. I have a for loop :

for(long i = 0; i < fileToSend.length(); i += PACKET_SIZE){
    fileBytes = getBytesFromFile(fileToSend, i);  

where getBytesFromFile() reads a PACKET_SIZE amount of bytes from fileToSend which is then sent to the client in the for loop. getBytesFromFile() uses i as an offset; however, the offset variable in FileInputStream.read() has to be an int. I'm sure there is a better way to read this file into the array, I just haven't found it yet.

I would prefer to not use NIO yet, although I will switch to using that in the future. Indulge my madness :-)

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

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

发布评论

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

评论(2

策马西风 2024-12-24 20:28:15

您似乎没有正确从文件中读取数据。在 Java 中从流中读取数据时,标准做法是将数据读入缓冲区。缓冲区的大小可以是您的数据包大小。

File fileToSend = //...
InputStream in = new FileInputStream(fileToSend);
OutputStream out = //...
byte buffer[] = new byte[PACKET_SIZE];
int read;
while ((read = in.read(buffer)) != -1){
  out.write(buffer, 0, read);
}
in.close();
out.close();

请注意,缓冲区数组的大小保持不变。 但是--如果缓冲区无法被填充(比如当它到达文件末尾时),数组的剩余元素将包含来自最后一个数据包的数据,因此您必须忽略这些元素(这是我的代码示例中的 out.write() 行的作用)

It doesn't look like you're reading data from the file properly. When reading data from a stream in Java, it's standard practice to read data into a buffer. The size of the buffer can be your packet size.

File fileToSend = //...
InputStream in = new FileInputStream(fileToSend);
OutputStream out = //...
byte buffer[] = new byte[PACKET_SIZE];
int read;
while ((read = in.read(buffer)) != -1){
  out.write(buffer, 0, read);
}
in.close();
out.close();

Note that, the size of the buffer array remains constant. But-- if the buffer cannot be filled (like when it reaches the end of the file), the remaining elements of the array will contain data from the last packet, so you must ignore these elements (this is what the out.write() line in my code sample does)

路弥 2024-12-24 20:28:15

嗯,意识到你对变量 i 的处理不正确。

Iteration 0: i=0
Iteration 1: i=PACKET_SIZE
...
...
Iteration n: i=PACKET_SIZE*n

Umm, realize that your handling of the variable i is not correct..

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