大文件上传第二弹(分片、秒传、断点续传)

关键部分

前端用file.slice()分块

前端用FileReader获取每一分块的md5值

后端用MultipartFile接受分块文件

后端用FileOutputStream拼装分块文件

话不多说,直接上代码,我想这是你们最喜欢的

工程截图

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

String clientCookie = request.getHeader("Cookie");

%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

  <head>

     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>up6.2-MySQL演示页面</title>

    <link href="js/up6.css" type="text/css" rel="Stylesheet"/>

    <script type="text/javascript" src="js/jquery-1.4.min.js"></script>

    <script type="text/javascript" src="js/json2.min.js" charset="utf-8"></script>

    <script type="text/javascript" src="js/up6.config.js" charset="utf-8"></script>

    <script type="text/javascript" src="js/up6.app.js" charset="utf-8"></script>

    <script type="text/javascript" src="js/up6.edge.js" charset="utf-8"></script>

    <script type="text/javascript" src="js/up6.file.js" charset="utf-8"></script>

    <script type="text/javascript" src="js/up6.folder.js" charset="utf-8"></script>

    <script type="text/javascript" src="js/up6.js" charset="utf-8"></script>

    <script language="javascript" type="text/javascript">

    var cbMgr = new HttpUploaderMgr();

    cbMgr.event.md5Complete = function (obj, md5) { /*alert(md5);*/ };

     cbMgr.event.fileComplete = function (obj) { /*alert(obj.pathSvr);*/ };

    cbMgr.Config["Cookie"] = 'JSESSIONID=<%=request.getSession().getId()%>';

    cbMgr.Config.Fields["test"] = "test";

 

    $(function()

    {

         cbMgr.load_to("FilePanel");//加载控件

        });

    </script>

  </head>

 

  <body>

     <p>up6.2多文件上传演示页面</p>

     <div id="FilePanel"></div>

     <div id="msg"></div>

  </body>

</html>

 

文件MD5计算

     /**

      * 把文件转成md5字符串

      * @param file

      * @return

      */

     public static String fileToMD5(File file) {

         if(file == null) {

              return null;

         }

         if(file.exists() == false) {

              return null;

         }

         if(file.isFile() == false) {

              return null;

         }

         FileInputStream fis = null;

         try {

              //创建一个提供信息摘要算法的对象,初始化为md5算法对象

              MessageDigest md = MessageDigest.getInstance("MD5");

              fis = new FileInputStream(file);

              byte[] buff = new byte[1024];

              int len = 0;

              while(true) {

                   len = fis.read(buff, 0, buff.length);

                   if(len == -1){

                       break;

                   }

                   //每次循环读取一定的字节都更新

                   md.update(buff,0,len);

              }

              //关闭流

              fis.close();

              //返回md5字符串

              return bytesToHex(md.digest());

         } catch (Exception e) {

              e.printStackTrace();

         }

         return null;

     }

 

文件夹文件夹名称生成逻辑

package up6.biz;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.UUID;

import up6.model.FileInf;

public class PathBuilderUuid extends PathBuilder{

     /* 生成文件夹存储路径,完全与客户端文件夹结构保持一致

      * 格式:    

      *  upload/2016/05/17/uuid/folder_name

      * 更新记录:

      *  2016-03-01 upload/uid/folders/uuid/folder_name

      *   2016-05-17 将格式改为日期格式

      *

      */

     public String genFolder(int uid,String nameLoc) throws IOException

     {

         SimpleDateFormat fmtDD = new SimpleDateFormat("dd");

         SimpleDateFormat fmtMM = new SimpleDateFormat("MM");

         SimpleDateFormat fmtYY = new SimpleDateFormat("yyyy");

        

         Date date = new Date();

         String strDD = fmtDD.format(date);

         String strMM = fmtMM.format(date);

         String strYY = fmtYY.format(date);

        

         String uuid = UUID.randomUUID().toString();

         uuid = uuid.replace("-","");

        

         String path = this.getRoot() + "/";

         path = path.concat(strYY);

         path = path.concat("/");

         path = path.concat(strMM);

         path = path.concat("/");

         path = path.concat(strDD);

         path = path.concat("/");

        

         path = path.concat(uuid);

         path = path.concat("/");

         path = path.concat(nameLoc);

         return path;

     }

    

     /* 保留原始文件名称,不检查文件是否重复

      * 格式:

      *   upload/uid/年/月/日/uuid/file_name

      * @see Xproer.PathBuilder#genFile(int, Xproer.xdb_files)

      */

     public String genFile(int uid,FileInf f) throws IOException{

         String uuid = UUID.randomUUID().toString();

         uuid = uuid.replace("-", "");

        

 

         SimpleDateFormat fmtDD = new SimpleDateFormat("dd");

         SimpleDateFormat fmtMM = new SimpleDateFormat("MM");

         SimpleDateFormat fmtYY = new SimpleDateFormat("yyyy");

        

         Date date = new Date();

         String strDD = fmtDD.format(date);

         String strMM = fmtMM.format(date);

         String strYY = fmtYY.format(date);

        

         String path = this.getRoot() + "/";

         path = path.concat(strYY);

         path = path.concat("/");

         path = path.concat(strMM);

         path = path.concat("/");

         path = path.concat(strDD);

         path = path.concat("/");

         path = path.concat(uuid);

         path = path.concat("/");

         path = path.concat(f.nameLoc);

         return path;

     }

}

 

文件保存位置及逻辑

在up6中有两种保存模式,一种是md5一种是uuid。

md5由PathBuilderMd5生成存储路径。md5主要提供给文件使用,可在服务器端保存唯一的文件,有效避免重复文件。

uuid由PathBuilderUuid生成存储路径。uuid主要提供给文件夹使用,可以与本地文件夹结构完全保持一致。使用uuid模式上传文件夹时秒传功能会失效。

 

文件默认保存位置在项目路径下:

https://i-blog.csdnimg.cn/blog_migrate/9acafb71681f577355fade3d787246d4.png

demo会自动生成upload文件夹

规则:upload/年/月/日/md5

https://i-blog.csdnimg.cn/blog_migrate/4a771cc5adf203e8ebc7a4274535d66f.png

 

代码截图:

https://i-blog.csdnimg.cn/blog_migrate/100ac54f8f1db9090b0e1d8d21bbe7e8.png

 

https://i-blog.csdnimg.cn/blog_migrate/eadfa9887c7460746bc38f4b6ca48b91.png

 

https://i-blog.csdnimg.cn/blog_migrate/1942e271b6488deca401ed52bc33b990.png

 

文件逻辑:

https://i-blog.csdnimg.cn/blog_migrate/f5c2ae3235537bcfd141ebea3a1c9de8.png

生成文件服务器存储路径

https://i-blog.csdnimg.cn/blog_migrate/5ee8bd88593fd40b7a9c9c812b20698c.png

 

文件夹逻辑:

https://i-blog.csdnimg.cn/blog_migrate/3b56495caef3ab128b6303093f5c65f3.png

https://i-blog.csdnimg.cn/blog_migrate/262a0237e709ad9956f546853a011af3.png

生成文件夹存储路径

 

https://i-blog.csdnimg.cn/blog_migrate/29a17090665477ff15eba1bc5f3863f5.png

生成子文件路径

 

https://i-blog.csdnimg.cn/blog_migrate/9cb55873f0b7c550cb5ef64f78dddd97.png

生成子目录路径

 

https://i-blog.csdnimg.cn/blog_migrate/187be76acd0cf795c9e2ce1332de4afb.png

功能与效果展示:

树形目录导航。您可以通过树型目录导航和路径导航栏快速跳转到指定目录。在跳转后树型目录将会自动选中当前的目录。

 

路径导航,点击根目录按钮便可返根目录

 

文件和目录重命名

 
 

点击删除按钮

说明: http://qqadapt.qpic.cn/adapt/0/a08d8c16-78a3-7ce5-c2cd-5df1e60fb8cc/800?pt=0&ek=1&kp=1&sce=0-12-12

点击确定后,页面中的文件消失


 

 

 

批量上传文件

 
 

粘贴上传

复制文件夹、文件或图片

说明: http://qqadapt.qpic.cn/adapt/0/ab61b9d4-755b-ee18-376d-f4ac80ad6417/800?pt=0&ek=1&kp=1&sce=0-12-12

在页面中选择好相应的上传目录,点击粘贴上传按钮,数据即可快速开始上传

说明: http://qqadapt.qpic.cn/adapt/0/b9fa01e5-ebec-3887-6726-67dcbc1a77ef/800?pt=0&ek=1&kp=1&sce=0-12-12


 

 

 

批量上传文件和文件夹

 

数据库记录

 

文件和目录下载

 

批量下载

同时选择多个需要下载的文件 

然后点击下载按钮,设置下载目录文件夹

说明: C:\Users\Administrator\Desktop\2222.png


 

 

点击全部下载,开始下载

 

自动加载未上传完的任务。在刷新浏览器或重启电脑后仍然可以自动加载未完成的任务。

 

 

下载完成后打开我们设置的下载目录文件夹,发现需下载的文件或文件夹确认已下载成功,经确认文件夹内的内容与下载文件夹内容一致

说明: http://qqadapt.qpic.cn/adapt/0/3224196a-396c-8ee1-b509-3179302fbd86/800?pt=0&ek=1&kp=1&sce=0-12-12

 

数据库记录

 
 

控件包下载:
cab(x86):http://t.cn/Ai9pmG8S

cab(x64):http://t.cn/Ai9pm04B

xpi:http://t.cn/Ai9pubUc

crx:http://t.cn/Ai9pmrcy

exe:http://t.cn/Ai9puobe

 

示例下载:

asp.net:http://t.cn/Ai9pue4A

jsp-eclipse:http://t.cn/Ai9p3LSx

jsp-myeclipse:http://t.cn/Ai9p3IdC

php: http://t.cn/Ai9p3CKQ

 

在线教程:
asp.net-文件管理器教程:http://j.mp/2MLoQWf

jsp-文件管理器教程:http://j.mp/2WJ2Y1m

php-文件管理器教程:http://j.mp/2MudPs3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值