1. 程式人生 > >大檔案分塊上傳第二彈(秒傳、斷點續傳)

大檔案分塊上傳第二彈(秒傳、斷點續傳)

關鍵部分

  • 前端用file.slice()分塊
  • 前端用FileReader獲取每一分塊的md5值
  • 後端用MultipartFile接受分塊檔案
  • 後端用FileOutputStream拼裝分塊檔案

話不多說,直接上程式碼,我想這是你們最喜歡的這裡寫圖片描述

html

<!DOCTYPE HTML>
<html>
<head>

    <meta charset="utf-8">

    <title>HTML5大檔案分片上傳示例</title>

    <script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"
></script> <script src="md5.js"></script> <script> var i = -1; var succeed = 0; var databgein; //開始時間 var dataend; //結束時間 var action=false; //false檢驗分片是否上傳過(預設); true上傳檔案 var page = { init: function(){ $("#upload").click(function(){ databgein=new Date(); var file = $("#file"
)[0].files[0]; //檔案物件 isUpload(file); }); } }; $(function(){ page.init(); }); function isUpload (file) { //構造一個表單,FormData是HTML5新增的 var form = new FormData();    var r = new FileReader();   r.readAsBinaryString(file); $(r).load(function(e){   var bolb = e.target.result;   var md5 = hex_md5(bolb); form.append("md5"
, md5); //Ajax提交 $.ajax({ url: "http://localhost:8080//bookQr/test/isUpload", type: "POST", data: form, async: true, //非同步 processData: false, //很重要,告訴jquery不要對form進行處理 contentType: false, //很重要,指定為false才能形成正確的Content-Type success: function(data){ var dataObj = eval("("+data+")"); var uuid = dataObj.fileId; var date = dataObj.date; if (dataObj.flag == "1") { //沒有上傳過檔案 upload(file,uuid,md5,date); } else if(dataObj.flag == "2") { //已經上傳部分 upload(file,uuid,md5,date);       } else if(dataObj.flag == "3") { //檔案已經上傳過 alert("檔案已經上傳過,秒傳了!!"); $("#uuid").append(uuid);         } },error: function(XMLHttpRequest, textStatus, errorThrown) { alert("伺服器出錯!"); } });    }) } /* * file 檔案物件 * uuid 後端生成的uuid * filemd5 整個檔案的md5 * date 檔案第一個分片上傳的日期(如:20170122) */ function upload (file,uuid,filemd5,date) { name = file.name; //檔名 size = file.size; //總大小 var shardSize = 5 * 1024 * 1024, //以5MB為一個分片 shardCount = Math.ceil(size / shardSize); //總片數 if (i > shardCount) { i = -1; i = shardCount; } else { if(!action){ i += 1; //只有在檢測分片時,i才去加1; 上傳檔案時無需加1 } } //計算每一片的起始與結束位置 var start = i * shardSize, end = Math.min(size, start + shardSize); //構造一個表單,FormData是HTML5新增的 var form = new FormData(); if(!action){ form.append("action", "check"); //檢測分片是否上傳 $("#param").append("action==check "); }else{ form.append("action", "upload"); //直接上傳分片 form.append("data", file.slice(start,end)); //slice方法用於切出檔案的一部分 $("#param").append("action==upload "); } form.append("uuid", uuid); form.append("filemd5", filemd5); form.append("date", date); form.append("name", name); form.append("size", size); form.append("total", shardCount); //總片數 form.append("index", i+1); //當前是第幾片 var ssindex = i+1; $("#param").append("index=="+ssindex+"<br/>"); //按大小切割檔案段   var data = file.slice(start, end);    var r = new FileReader();   r.readAsBinaryString(data); $(r).load(function(e){   var bolb = e.target.result;   var md5 = hex_md5(bolb); form.append("md5", md5); //Ajax提交 $.ajax({ url: "http://localhost:8080//bookQr/test/upload", type: "POST", data: form, async: true, //非同步 processData: false, //很重要,告訴jquery不要對form進行處理 contentType: false, //很重要,指定為false才能形成正確的Content-Type success: function(data){ var dataObj = eval("("+data+")"); var fileuuid = dataObj.fileId; var flag = dataObj.flag; //伺服器返回該分片是否上傳過 if(!action){ if (flag == "1") { //未上傳 action = true; } else if(dataObj.flag == "2") { //已上傳   action = false; ++succeed;     } //遞迴呼叫     upload(file,uuid,filemd5,date); }else{ //伺服器返回分片是否上傳成功 //改變介面 ++succeed; $("#output").text(succeed + " / " + shardCount); if (i+1 == shardCount) { dataend=new Date(); $("#uuid").append(fileuuid); $("#usetime").append(dataend.getTime()-databgein.getTime()); } else { //已上傳成功,然後檢測下一個分片   action = false; //遞迴呼叫     upload(file,uuid,filemd5,date);     } } },error: function(XMLHttpRequest, textStatus, errorThrown) { alert("伺服器出錯!"); } });    }) } </script> </head> <body> <input type="file" id="file" /> <button id="upload">上傳</button> <span id="output" style="font-size:12px">等待</span> <span id="usetime" style="font-size:12px;margin-left:20px;">用時</span> <span id="uuid" style="font-size:12px;margin-left:20px;">uuid==</span> <br/> <br/> <br/> <br/> <span id="param" style="font-size:12px;margin-left:20px;">param==</span> </body> </html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315

controller

package com.yxtech.sys.controller;

import com.yxtech.common.Constant;
import com.yxtech.sys.domain.FileRes;
import com.yxtech.sys.service.FileResService;
import com.yxtech.sys.service.ResService;
import com.yxtech.utils.file.FileMd5Util;
import com.yxtech.utils.file.NameUtil;
import com.yxtech.utils.file.PathUtil;
import jodd.datetime.JDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2015/10/9.
 */
@RestController
@Scope("prototype")
@RequestMapping(value = "/test")
public class testController {
    @Autowired
    private FileResService 
            
           

相關推薦

檔案第二(斷點)

關鍵部分 前端用file.slice()分塊 前端用FileReader獲取每一分塊的md5值 後端用MultipartFile接受分塊檔案 後端用FileOutputStream拼裝分塊檔案 話不多說,直接上程式碼,我想這是你們最喜歡的 html &l

springboot 整合 gridfs webUploader實現檔案斷點

主要的pom.xml: <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId>

js檔案與tornado接收檔案和下載

將檔案分塊上傳使用的是HTML5的功能(IE可能不支援) <div class="layui-container"> <input type="file" id="file" multiple required> <br>

檔案以及斷點

var fileMd5; //檔案唯一標識 /******************下面的引數是自定義的*************************/ var fileName;//檔名稱 var oldJindu;//如果該檔案之前上傳過 已經上傳的進度

java檔案分享

    先說說大檔案上傳種用的點以及原理,也希望各位指正。    思路以及大部分原始碼來自於  haohao123naa博主,我只是在他的基礎上進行完善點選開啟連結,在此表示感謝。    1.檔案分塊:一個超過幾個G的檔案上傳到伺服器,如果你只使用最簡單上傳、接收、處理、還能

檔案第二(分片斷點)

關鍵部分 前端用file.slice()分塊 前端用FileReader獲取每一分塊的md5值 後端用MultipartFile接受分塊檔案 後端用FileOutputStream拼裝分塊檔案 話不多說,直接上程式碼,我想這是你們最喜歡的

Java 檔案客戶端原始碼

        本部落格介紹如何進行檔案的分塊上傳。本文側重介紹客戶端,伺服器端請參考部落格《Java 檔案分塊上傳伺服器端原始碼》。建議讀者朋友在閱讀本文程式碼前先了解一下 MIME 協議。         所謂分塊上傳並非把大檔案進行物理分塊,然後挨個上傳,而是依次讀

網頁內實現檔案分片斷點

最近做公司的專案,需要在後臺控制系統中新增一個功能-------向伺服器傳送程式更新包;這些程式更新包大小不固定,但基本都在1G到4G之間,剛開始還真是難倒我了,因為之前的專案中沒有上傳過這麼大的檔案,還要斷點續傳,後來經過查資料,寫DEMO,這個問題終於解決了; 解決辦法: 使用XMLHt

pandas 讀取csv檔案讀取指定行 讀取csv檔案讀取方法

    當用pandas的read_csv函式或者是read_table函式讀取檔案時,如果遇到大的檔案,需要分塊讀取,在這個基礎上可以 讀取指定行,比如讀取標籤全為0的行。 程式碼如下: f

檔案那些事兒:多圖檔案斷點功能實現與分析

簡介 看了不少的教程,在系統整合搭建的過程中一般寫到檔案上傳這一節時,基本上實現一個檔案上傳功能就不再繼續拓展,而是就此截止轉而去講解其他的內容了,因為企業級應用開發中這些功能肯定會使用到,企業網站的檔案上傳不可能只有一個單圖上傳,也不可能不實現大檔案的功能處

檔案斷點beegovue

## 大檔案上傳 ### 0、專案原始碼地址 原始碼地址 :https://github.com/zhuchangwu/large-file-upload > 它是個demo,僅供參考 前端基於 vue-simple-uploader (感謝這個大佬)實現: https://github.com/s

Retrofit下載檔案監聽進度斷點

前些天有個同學問我會不會使用Retrofit下載大檔案,我就給了他我專案中使用的方法。發現有很多人還不會用Retrofit實現下載檔案,即使會下載,也可能會出現問題,比如:不知道如何獲取進度;一旦下載大檔案就會OOM;不知道如何暫停下載,或者不知道如何實現斷點續傳等。今天這個

Java實現多線程下載斷點

get import 服務 結束 parseint RR range turn con 開三個線程下載,代碼: package demo; import java.io.InputStream; import java.io.RandomAccessFile; impo

網絡基礎ftp任務(進度條計算文件大小斷點搭建框架示例)

... 請求 pro dal hashlib one win lap ase 一、網絡基礎 1、端口,是什麽?為什麽要有端口?   端口是為了將同一個電腦上的不同程序進行隔離。   IP是找電腦;端口是找電腦上的應用程序;   端口範圍:1 – 6553

SFTP下載客戶端[單使用者多執行緒限速取消斷點]

採用JSCH API(本例引用了jsch-0.1.52.jar) 官網參考 http://www.jcraft.com/jsch/1,建立Session,對應一個使用者賬戶,並在無傳輸執行緒時自動關閉sessionpublic class SFTPProcessor2 {

C# Http多執行緒下載斷點

using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using Sy

04redis主從複製原理斷點無磁碟化複製過期key處理

1、主從架構的核心原理 當啟動一個slave node的時候,它會發送一個PSYNC命令給master node 如果這是sl

android 檔案分割

由於android自身的原因,對大檔案(如視訊檔案)的操作很容易造成OOM,即:Dalvik堆記憶體溢位,利用檔案分割將大檔案分割為小檔案可以解決問題。檔案分割後分多次請求服務。//檔案分割上傳 public void cutFileUpload(String

C#為例,檔案

前臺: <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>&

java使用WebUploader做檔案斷點

前言: WebUploader是由Baidu WebFE(FEX)團隊開發的一個簡單的以HTML5為主,FLASH為輔的現代檔案上傳元件。在現代的瀏覽器裡面能充分發揮html5的優勢,同時又不摒棄主流IE瀏覽器,沿用原來的FLASH執行時,相容IE6+,iOS 6+,