Java读取大于2GB的文件(使用分块)
我正在实现一个文件传输服务器,但在通过网络发送大于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎没有正确从文件中读取数据。在 Java 中从流中读取数据时,标准做法是将数据读入缓冲区。缓冲区的大小可以是您的数据包大小。
请注意,缓冲区数组的大小保持不变。 但是--如果缓冲区无法被填充(比如当它到达文件末尾时),数组的剩余元素将包含来自最后一个数据包的数据,因此您必须忽略这些元素(这是我的代码示例中的
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.
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)嗯,意识到你对变量 i 的处理不正确。
Umm, realize that your handling of the variable i is not correct..