Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
Hello, everyone. Today, I will share with you another article about actual combat. I hope you will like it.
Recently, I received a new demand to upload a video file of about 2G, so I tried it with OSS of the test environment, and it took more than ten minutes to upload. Considering the resource problem of the company, I gave up this plan decisively.
When it comes to uploading large files, the first thing I think of is all kinds of network disks. Now everyone likes to upload their small movies to network disks for preservation. Web disks generally support breakpoint continuation and file second transmission functions, reducing network fluctuations and network bandwidth restrictions on files, greatly improving user experience, let people love.
Speaking of which, let’s take a look at these concepts:
- File fragmentation: divide large files into small files, upload/download the small files, and finally assemble the small files into large files;
- Breakpoint continuation: on the basis of file partition, upload/download each small file by a separate thread. In case of network failure, you can continue to upload/download the unfinished part from the uploaded/downloaded part, and there is no need to start from the beginning.
- File transfer in seconds: The file already exists on the resource server, and the URI of the file is returned when others upload the file.
RandomAccessFile
We usually use FileInputStream, FileOutputStream, FileReader, and FileWriter to read files. Today we will look at RandomAccessFile.
It is an independent class that directly inherits Object. In the underlying implementation, it implements DataInput and DataOutput interfaces. This class supports random reads of files, which are similar to large byte arrays stored in a file system.
Its implementation is based on a file pointer (a cursor or index that points to an implied array), which can be read by the getFilePointer method or set by the seek method.
Input reads bytes starting from the file pointer and makes the file pointer exceed the bytes read. Output operations that write beyond the current end of the implied array result in expanding the array. This class has four modes to choose from:
- R: Opens the file in read-only mode and will be thrown if a write operation is performed
IOException
; - Rw: Open a file in read or write mode. If the file does not exist, create a file.
- RWS: Open a file in read or write mode, requiring that every update of the file content or metadata be synchronized to the underlying storage device.
- RWD: opens a file in read and write mode, requiring that each update of the file content be synchronized to the underlying storage device.
In RW mode, buffer is used by default, and only when the cache is full or the stream is closed with randomAccessfile.close () is actually written to the file.
In the next article we will take a look at the use of RandomAccessFile and how the front end implements large file chunking.
The above is all the content of today, if you have different opinions or better idea, welcome to contact AH Q, add AH Q can join the technical exchange group to participate in the discussion!