php web development - Upload and download folder

The core principle:

 

The core of the project is to upload the file block. Front and rear end to high level of cooperation, the two sides agreed need better data in order to complete large file segment, we focus on in the project to solve the following problems.

How fragmentation;

How to synthesize a file;

Interrupted which slice from the beginning.

How points, using powerful js library to ease our work, have been able to block the wheels on large files on the market, although the nature of the programmer has forced me to re-create the wheel. However, because of the relationship as well as working time, I can only give up the. Finally, I chose Baidu's WebUploader to achieve the desired front-end.

How close, before closing, we must first solve a problem, how do we distinguish the file block belongs. At the beginning, I was using a front-end to generate a unique uuid do sign documents, put on each slice request. But later, when do I give up the second pass, using Md5 to maintain block and file relationships.

Merge file server in question, and record blocking, and in this respect the industry has actually given a good solution. Referring Thunder, you will find that each download time, there will be two files, a file body, another file is a temporary file, temporary file stores the state of each block corresponding byte position.

These are the need to make close contact with front and rear ends, the front end of a fixed size need to file fragmentation, and fragmentation to bring the request number and size. After successful transmission request reaches the front end of the background, according to the server only needs to request data slice number and slice each block size (segment size is fixed and the same) is calculated start position, and the read segment data file, writing into the file.

In order to facilitate the development, I will end service business logic follows divided into initialization, block processing, and the like uploaded file.

The server-side business logic module follows

 

Functional Analysis:

Folder generating module

 

After the folder is uploaded by the server scan code is as follows

 

 

分块上传,分块处理逻辑应该是最简单的逻辑了,up6已经将文件进行了分块,并且对每个分块数据进行了标识,这些标识包括文件块的索引,大小,偏移,文件MD5,文件块MD5(需要开启)等信息,服务端在接收这些信息后便可以非常方便的进行处理了。比如将块数据保存到分布式存储系统中

 

分块上传可以说是我们整个项目的基础,像断点续传、暂停这些都是需要用到分块。

分块这块相对来说比较简单。前端是采用了webuploader,分块等基础功能已经封装起来,使用方便。

借助webUpload提供给我们的文件API,前端就显得异常简单。

前台HTML模板

 

分则必合。把大文件分片了,但是分片了就没有原本文件功能,所以我们要把分片合成为原本的文件。我们只需要把分片按原本位置写入到文件中去。因为前面原理那一部我们已经讲到了,我们知道分块大小和分块序号,我就可以知道该分块在文件中的起始位置。所以这里使用RandomAccessFile是明智的,RandomAccessFile能在文件里面前后移动。但是在andomAccessFile的绝大多数功能,已经被JDK1.4的NIO的“内存映射文件(memory-mapped files)”取代了。我在该项目中分别写了使用RandomAccessFile与MappedByteBuffer来合成文件。分别对应的方法是uploadFileRandomAccessFile和uploadFileByMappedByteBuffer。两个方法代码如下。

秒传功能

 

服务端逻辑

秒传功能,相信大家都体现过了,网盘上传的时候,发现上传的文件秒传了。其实原理稍微有研究过的同学应该知道,其实就是检验文件MD5,记录下上传到系统的文件的MD5,在一个文件上传前先获取文件内容MD5值或者部分取值MD5,然后在匹配系统上的数据。

Breakpoint-http实现秒传原理,客户端选择文件之后,点击上传的时候触发获取文件MD5值,获取MD5后调用系统一个接口(/index/checkFileMd5),查询该MD5是否已经存在(我在该项目中用redis来存储数据,用文件MD5值来作key,value是文件存储的地址。)接口返回检查状态,然后再进行下一步的操作。相信大家看代码就能明白了。

嗯,前端的MD5取值也是用了webuploader自带的功能,这还是个不错的工具。

控件计算完文件MD5后会触发md5_complete事件,并传值md5,开发者只需要处理这个事件即可,

断点续传

up6已经自动对断点续传进行了处理,不需要开发都再进行单独的处理。

在f_post.php中接收这些参数,并进行处理,开发者只需要关注业务逻辑,不需要关注其它的方面。

断点续传,就是在文件上传的过程中发生了中断,人为因素(暂停)或者不可抗力(断网或者网络差)导致了文件上传到一半失败了。然后在环境恢复的时候,重新上传该文件,而不至于是从新开始上传的。

前面也已经讲过,断点续传的功能是基于分块上传来实现的,把一个大文件分成很多个小块,服务端能够把每个上传成功的分块都落地下来,客户端在上传文件开始时调用接口快速验证,条件选择跳过某个分块。

实现原理,就是在每个文件上传前,就获取到文件MD5取值,在上传文件前调用接口(/index/checkFileMd5,没错也是秒传的检验接口)如果获取的文件状态是未完成,则返回所有的还没上传的分块的编号,然后前端进行条件筛算出哪些没上传的分块,然后进行上传。

当接收到文件块后就可以直接写入到服务器的文件中

这是文件块上传的效果

这是文件夹上传完后的效果

这是文件夹上传完后在服务端的存储结构

参考文章:http://blog.ncmem.com/wordpress/2019/08/12/java-http%E5%A4%A7%E6%96%87%E4%BB%B6%E6%96%AD%E7%82%B9%E7%BB%AD%E4%BC%A0%E4%B8%8A%E4%BC%A0/


Guess you like

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