JAVA WEB project folder upload and download solution

Only the backend code is written here. The basic idea is that the frontend will fragment the file, and then each time you access the upload interface, you will pass parameters to the backend: the current file and the total number of fragments

Let's paste the code directly below. Most of the difficult ones are commented:

Upload file entity class:

It can be seen that there are already many functions we need in the entity class, as well as practical attributes. Such as the information transmitted in MD5 seconds.

 

The first is the file data receiving logic, which is responsible for receiving the file block data uploaded by the control and then writing it to the server file. The control has provided the index, size, MD5 and length information of the block. We can handle it flexibly according to our needs, and we can also save the data of the file block to the distributed storage system.

 

File initialization section

 

The first step: get RandomAccessFile, random access file class object

The second step: call the getChannel () method of RandomAccessFile to open the file channel FileChannel. This logic can be optimized. If there is a need for distributed storage in the future, it can be changed to distributed storage to reduce the pressure on a single server.

Step 3: Get the current number of blocks and calculate the last offset of the file

Step 4: Get the byte array of the current file block, used to get the file byte length

Step 5: Use the map () method of the FileChannel class File Channel to create a direct byte buffer MappedByteBuffer

Step 6: Put the block byte array into the buffer at the current position mappedByteBuffer.put (byte [] b);

Step 7: Free the buffer

Step 8: Check whether the file is completely uploaded

Folder scanning

 

Storage path generation class

Well, this is all over. If you have any questions or criticisms, comments and private messages are welcome. We will grow together and learn together.

Finally, put an effect picture

The back-end code logic is mostly the same, and currently supports MySQL, Oracle, SQL. Before using, you need to configure the database. You can refer to this article I wrote: http://blog.ncmem.com/wordpress/2019/08/07/java upload and download of large files /  
welcome to join the group to discuss "374992201

Guess you like

Origin www.cnblogs.com/songsu/p/12760327.html