问题:

  B/S通常都会涉及到文件的上传,普通文件上传使用文件框,后台接收文件即可

  我遇到的问题是要开发一个大文件上传的功能,那就肯定要支持文件的分片

分析:

  1.参考网上的资料后,通常的多文件和大文件上传会用到三种框架

多文件上传的插件常用的有:
1、jquery uploadify 下载【http://www.uploadify.com/download/】
2、jquery file upload 下载【https://github.com/blueimp/jQuery-File-Upload/tags】
3、webuploader 下载 【http://fex.baidu.com/webuploader/download.html】

  2.最后我选的是百度开发的 【webuploader】,网上比较推崇

解决:

  1.新建一个测试页面,引入相关的样式表css,js文件

    注意,要首先引入jquery的js文件,我的页面是嵌套的,首页已经引入

    引入文件清单:

      @ bootstrap的css,js文件

      @ 在【http://fex.baidu.com/webuploader/】这里下载webuploader的压缩包,我这只是引入了

        【webuploader.css】和【webuploader.js】

      @ 引入自己的业务js文件

<!--引入CSS-->
<link rel="stylesheet" type="text/css" href="static/html/bigFileUpload/assets/bootstrap-3.3.7-dist/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="static/html/bigFileUpload/assets/webuploader.css"> <!--官网例子-->
<!--<div id="uploader" class="wu-example">-->
<!--&lt;!&ndash;用来存放文件信息&ndash;&gt;-->
<!--<div id="thelist" class="uploader-list"></div>-->
<!--<div class="btns">-->
<!--<div id="picker">选择文件</div>-->
<!--<button id="ctlBtn" class="btn btn-default">开始上传</button>-->
<!--</div>-->
<!--</div>--> <!--单文件-->
<div id="uploader" class="wu-example">
<!--用来存放文件信息-->
<div id="thelist" class="uploader-list"></div>
<div class="btns">
<div id="attach"></div>
<button id="upload" class="btn btn-default">开始上传</button>
</div>
</div> <!--多文件-->
<!--<div id="uploader1" class="wu-example">-->
<!--&lt;!&ndash;用来存放文件信息&ndash;&gt;-->
<!--<div id="thelist1" class="uploader-list"></div>-->
<!--<div class="btns">-->
<!--<div id="multi"></div>-->
<!--<input type="button" value="上传" id="multiUpload"/>-->
<!--</div>-->
<!--</div>--> <!--引入JS-->
<script type="text/javascript" src="static/html/bigFileUpload/assets/webuploader.js"></script>
<script type="text/javascript" src="static/html/bigFileUpload/assets/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<script type="text/javascript" src="static/html/bigFileUpload/upload.js"></script>

  2.编写业务js文件

$(function(){

    var $list = $("#thelist");
var uploader;
   // 初始化uploader
uploader = WebUploader.create({
auto:false, //不自动提交,需要点击
pick: {
id: '#attach',
label: '选择文件',
},
server: 'test/save', //后台接收服务
resize: false,
formData: {"Authorization": localStorage.token}, //额外参数传递,可以没有
chunked: true, //分片
chunkSize: 10 * 1024 * 1024, //分片大小指定
threads:1, //线程数量
disableGlobalDnd: true //禁用拖拽
}); //添加文件页面提示
uploader.on( "fileQueued", function( file ) {
$list.html( "<div id='"+ file.id + "' class='item'>" +
"<h4 class='info'>" + file.name + "</h4>" +
"<p class='state'>等待上传...</p>" +
"</div>" );
}); //开进度条
uploader.on( 'uploadProgress', function( file, percentage ) {
var $li = $( '#'+file.id ),
$percent = $li.find('.progress .progress-bar');
if ( !$percent.length ) {
$percent = $('<div class="progress progress-striped active">' +
'<div class="progress-bar" role="progressbar" style="width: 0%">' +
'</div>' +
'</div>').appendTo( $li ).find('.progress-bar');
}
$li.find('p.state').text('上传中');
$percent.css( 'width', percentage * 100 + '%' );
}); //上传成功
uploader.on( "uploadSuccess", function( file ) {
$( "#"+file.id ).find("p.state").text("已上传");
$('#' + file.id).find('.progress').fadeOut();
}); //上传失败
uploader.on( "uploadError", function( file ) {
$( "#"+file.id ).find("p.state").text("上传出错");
uploader.cancelFile(file);
uploader.removeFile(file,true);
}); //点击上传
$("#upload").on("click", function() {
uploader.upload();
}) });

  3.编写后台文件接收文件

package org.triber.portal.upload;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
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 java.io.*;
import java.util.HashMap;
import java.util.Map; @Slf4j
@RestController
@RequestMapping(value = "/test", produces = MediaType.APPLICATION_JSON_VALUE)
public class UploadController { @Value("${AREA_FILE}")
private String AREA_FILE;//excel下载文件名
@Value("${AREA_DIR}")
private String AREA_DIR;//excel临时存储文件夹 /**
* 上传文件
* @param file
* @return
* @throws IOException
*/
@RequestMapping(value = "/save", produces = {"application/json"})
public Map<String,String> importExcel(@RequestParam MultipartFile file) throws IOException { //获取文件名
String originalFilename = file.getOriginalFilename(); //合并文件
RandomAccessFile raFile = null;
BufferedInputStream inputStream=null;
try{
File dirFile = new File(AREA_DIR, originalFilename);
//以读写的方式打开目标文件
raFile = new RandomAccessFile(dirFile, "rw");
raFile.seek(raFile.length());
inputStream = new BufferedInputStream(file.getInputStream());
byte[] buf = new byte[1024];
int length = 0;
while ((length = inputStream.read(buf)) != -1) {
raFile.write(buf, 0, length);
}
}catch(Exception e){
throw new IOException(e.getMessage());
}finally{
try {
if (inputStream != null) {
inputStream.close();
}
if (raFile != null) {
raFile.close();
}
}catch(Exception e){
throw new IOException(e.getMessage());
}
}
     
     //以下信息没用。比较扯淡
//初始化返回信息
Map<String, String> map = new HashMap<String, String>();
String result = "";
int res = -1;
//返回提示信息
map.put("result", result);
return map;
} }

总结:

  说一下我遇到的问题吧,

  网上查了好多资料,看了好多例子,可能例子业务要求高,设置一堆参数,最后发现还是官网靠谱,没有没用的东西,但是你单独拷贝下来会出现无法运行的情况

  额,最后拷贝精简代码,到最小可用,大家可以视情况而定

  我使用的springBoot,后台在接收文件时为 【MultipartFile】

  试了File来接受总是提示我不能接收,后来没办法。只能采用多文件类来接收

  我写的这个例子适用于单文件,而且文件比较大的情况

  据说IE不能上传4g之上的文件,现在没有这个限制了,无论大小,切割后后台组装即可

  后期可能会再加  多文件,重复文件校验,断点续传,块MD5校验  等功能,等功能上来后再为大家更新

记一次SpringBoot使用WebUploader的坑的更多相关文章

  1. 记一次项目使用webuploader爬坑之旅

       因前端页面开发使用的为VUE开发,又要支持IE9,遂只有基于webuploader封装一个上传组件.地址:https://github.com/z719725611/vue-upload-web ...

  2. 记一次SpringBoot 开发中所遇到的坑和解决方法

    记一次SpringBoot 开发中所遇到的坑和解决方法 mybatis返回Integer为0,自动转型出现空指针异常 当我们使用Integer去接受数据库中表的数据,如果返回的数据中为0,那么Inte ...

  3. 记一次 Spring 事务配置踩坑记

    记一次 Spring 事务配置踩坑记 问题描述:(SpringBoot + MyBatisPlus) 业务逻辑伪代码如下.理论上,插入数据 t1 后,xxService.getXxx() 方法的查询条 ...

  4. 记一次RabbitMq 安装和配置坑

    记一次RabbitMq 安装和配置坑 正常情况下安装 先安装erl ,在安装rabbitmq 这个在windows下的安装没什么技巧,按照默认一路下一步就ok.安装好后可以到cmd测试是否安装好. 测 ...

  5. 记一次UICollectionView中visibleCells的坑

    记一次UICollectionView中visibleCells的坑 项目的要求是这样的 其实也是一个轮播图,而已,所以依照轮播图的实现原理,这里觉得也很简单,还是利用UICollectionView ...

  6. 教你 Shiro 整合 SpringBoot,避开各种坑

    教你 Shiro 整合 SpringBoot,避开各种坑-----https://www.cnblogs.com/HowieYuan/p/9259638.html

  7. SpringBoot + SpringCloud学习踩坑实记

     踩的坑: 1).springcloud框架中,依赖一直报错,很可能是没有添加springcloud的依赖,或者是依赖的版本号过低.并且springboot也有一个父依赖. 2.springcloud ...

  8. vue-element-admin跟springboot+shiro部署爬坑记

    今天把前端采用vue-element-admin与springboot的项目部署到正是线上,在开发线上很OK的,一放上去我的天啊,坑是真的多阿.下面听我一一道来:我这边采用的是nginx服务器部署. ...

  9. 记一次springboot+mybatis+phoenix在代码集成中的坑

    场景: 希望使用phoenix做查询服务,给服务端提供接口 设计: 通过springboot做restful的接口发布,通过mybatis做phoenix的sql处理,因此是springboot+my ...

随机推荐

  1. Mysql不带条件更新报错

    执行命令SET SQL_SAFE_UPDATES = 0;修改下数据库模式即可: 恢复安全模式:SET SQL_SAFE_UPDATES = 1;

  2. Mybatis和Mysql的Json类型

    Mysql5.7新增加了Json类型字段,但是目前Mybatis中并不支持 1.新建MybatisJsonTypeHandler.java import com.fasterxml.jackson.a ...

  3. Django中模型(二)

    Django中模型(二) 三.定义模型 1.模型.属性.表.字段间的关系: 一个模型类在数据库中对应一张表:在模型类中定义的属性,对应该模型对照表中的字段. 2.定义属性 A.概述 ·django根据 ...

  4. Tomcat的免安装配置

    Tomcat免安装配置 以下配置说明全部针对免安装版本 基于tomcat的安装目录和运行目录是可以不同的,本文都会进行说明 首先简单介绍一下tomcat的目录结构,一般情况下,tomcat包括以下子目 ...

  5. Extjs自定义验证介绍

    表单验证实例(空验证,密码确认验证,email验证) 我们可以用单独的js写表单验证,但是extjs已经为我们想到了(自己单独写反而不方便). 在验证之前,我不得不提两个小知识点: //大家在很多的e ...

  6. 【Step By Step】将Dotnet Core部署到Docker下

    一.使用.Net Core构建WebAPI并访问Docker中的Mysql数据库 这个的过程大概与我之前的文章<尝试.Net Core—使用.Net Core + Entity FrameWor ...

  7. CC2640R2F&TI-RTOS 拿到 TI CC2640R2F 开发板 第四件事就是 修改第三件事信号量超时改为 事件 超时,并增加 事件控制 ,用于控制LED 闪烁时间或者关闭

    /* * data_process.c * * Created on: 2018年7月5日 * Author: admin */ #include "board_led.h" #i ...

  8. HTML和CSS基础知识

    html基本结构<html>内容</html> html开始标记<head>内容</head> html文件头标记<title>内容< ...

  9. Leetcode名企之路

    微信扫码关注,每天推送一道面试题! 公众号:Leetcode名企之路 作者简介 知乎ID: 码蹄疾 码蹄疾,毕业于哈尔滨工业大学. 小米广告第三代广告引擎的设计者.开发者: 负责小米应用商店.日历.开 ...

  10. #leetcode刷题之路16-最接近的三数之和

    给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...