文件上传
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import java.io.*;
import java.util.Map;
@Slf4j
@Controller
@RequestMapping(value = "/file")
public class BigFileController {
@RequestMapping(value = "/upload", method = RequestMethod.GET)
public String upload(Map model) {
return "upload";
}
@RequestMapping(value = "/uploadSimple", method = RequestMethod.GET)
public String uploadSimple(Map model) {
return "uploadSimple";
}
@RequestMapping(value = "saveUpload")
@ResponseBody
public void upload(MultipartHttpServletRequest request) {
String guid = request.getParameter("guid");
MultiValueMap multiFileMap = request.getMultiFileMap();
MultipartFile multipartFile = multiFileMap.getFirst("file");
try {
InputStream in = multipartFile.getInputStream();
String chunk = request.getParameter("chunk")==null?"0":request.getParameter("chunk");
String path = "D:\\bigFile" + File.separator + request.getParameter("id")+File.separator+guid;
File exist = new File(path);
if (!exist.exists()) {
exist.mkdirs();
}
OutputStream out = new FileOutputStream(path + File.separator + chunk + "_" + request.getParameter("name"));
byte[] bytes = new byte[1024];
int len = -1;
while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
}
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 文件合并
* @param guid
* @param realFileName
* @param chunks
* @param folder
*/
@RequestMapping(value = "merge",method = RequestMethod.POST)
@ResponseBody
public void merge(String guid,String realFileName,int chunks,String folder ) {
String path = "D:\\bigFile"+File.separator+folder+File.separator+guid;
try {
FileOutputStream fileOutputStream = new FileOutputStream(path+File.separator+realFileName);
byte[] buf = new byte[1024];
for(int i=0;i
2.1. 前端页面
选择文件
2.2.后端代码
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import java.io.*;
import java.util.Map;
@Slf4j
@Controller
@RequestMapping(value = "/file")
public class BigFileController {
@RequestMapping(value = "/upload", method = RequestMethod.GET)
public String upload(Map model) {
return "upload";
}
@RequestMapping(value = "/uploadSimple", method = RequestMethod.GET)
public String uploadSimple(Map model) {
return "uploadSimple";
}
@RequestMapping(value = "saveUpload")
@ResponseBody
public void upload(MultipartHttpServletRequest request) {
String guid = request.getParameter("guid");
MultiValueMap multiFileMap = request.getMultiFileMap();
MultipartFile multipartFile = multiFileMap.getFirst("file");
try {
InputStream in = multipartFile.getInputStream();
String chunk = request.getParameter("chunk")==null?"0":request.getParameter("chunk");
String path = "D:\\bigFile" + File.separator + request.getParameter("id")+File.separator+guid;
File exist = new File(path);
if (!exist.exists()) {
exist.mkdirs();
}
OutputStream out = new FileOutputStream(path + File.separator + chunk + "_" + request.getParameter("name"));
byte[] bytes = new byte[1024];
int len = -1;
while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
}
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 文件合并
* @param guid
* @param realFileName
* @param chunks
* @param folder
*/
@RequestMapping(value = "merge",method = RequestMethod.POST)
@ResponseBody
public void merge(String guid,String realFileName,int chunks,String folder ) {
String path = "D:\\bigFile"+File.separator+folder+File.separator+guid;
try {
FileOutputStream fileOutputStream = new FileOutputStream(path+File.separator+realFileName);
byte[] buf = new byte[1024];
for(int i=0;i
2.3.展示效果
注意:如果进度条不显示,很多情况下应该是样式的问题。覆盖下webuploader.css的样式即可。
webuploader.css样式代码如下
.webuploader-container {
position: relative;
}
.webuploader-element-invisible {
position: absolute !important;
clip: rect(1px 1px 1px 1px); /* IE6, IE7 */
clip: rect(1px,1px,1px,1px);
}
.webuploader-pick {
position: relative;
/display: inline-block;/
cursor: pointer;
background: #047a0a;
padding: 5px 8px;
color: #fff;
text-align: center;
border-radius: 3px;
overflow: hidden;
}
.webuploader-pick-hover {
background: #047a0a;
}
.webuploader-pick-disable {
opacity: 0.6;
pointer-events:none;
}
/**/
element.style {
opacity: 0;
width: 100%;
height: 100%;
display: block;
cursor: pointer;
background: rgb(255, 255, 255);
}
picker {
display: inline-block;
line-height: 1.428571429;
position: relative;
top: 12px;
}
.wu-example {
position: relative;
padding: 45px 15px 15px;
margin: 15px 0;
background-color: #fafafa;
box-shadow: inset 0 3px 6px rgba(0, 0, 0, .05);
border-color: #e5e5e5 #eee #eee;
border-style: solid;
border-width: 1px 0;
}
.wu-example:after {
content: "示例";
position: absolute;
top: 15px;
left: 15px;
font-size: 12px;
font-weight: bold;
color: #bbb;
text-transform: uppercase;
letter-spacing: 1px;
}
.uploader-list {
width: 100%;
overflow: hidden;
}
.file-item {
float: left;
position: relative;
margin: 0 20px 20px 0;
padding: 4px;
}
.file-item .error {
position: absolute;
top: 4px;
left: 4px;
right: 4px;
background: red;
color: white;
text-align: center;
height: 20px;
font-size: 14px;
line-height: 23px;
}
.file-item .info {
position: absolute;
left: 4px;
bottom: 4px;
right: 4px;
height: 20px;
line-height: 20px;
text-indent: 5px;
background: rgba(0, 0, 0, 0.6);
color: white;
overflow: hidden;
white-space: nowrap;
text-overflow : ellipsis;
font-size: 12px;
z-index: 10;
}
.upload-state-done:after {
content:"示例";
font-family: FontAwesome;
font-style: normal;
font-weight: normal;
line-height: 1;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
font-size: 32px;
position: absolute;
bottom: 0;
right: 4px;
color: #4cae4c;
z-index: 99;
}
.file-item .progress {
position: absolute;
right: 4px;
bottom: 4px;
height: 3px;
left: 4px;
height: 4px;
overflow: hidden;
z-index: 15;
margin:0;
padding: 0;
border-radius: 0;
background: transparent;
}
.file-item .progress span {
display: block;
overflow: hidden;
width: 0;
height: 100%;
background: #d14 url(progress.png) repeat-x;
-webit-transition: width 200ms linear;
-moz-transition: width 200ms linear;
-o-transition: width 200ms linear;
-ms-transition: width 200ms linear;
transition: width 200ms linear;
-webkit-animation: progressmove 2s linear infinite;
-moz-animation: progressmove 2s linear infinite;
-o-animation: progressmove 2s linear infinite;
-ms-animation: progressmove 2s linear infinite;
animation: progressmove 2s linear infinite;
-webkit-transform: translateZ(0);
}
@-webkit-keyframes progressmove {
0% {
background-position: 0 0;
}
100% {
background-position: 17px 0;
}
}
@-moz-keyframes progressmove {
0% {
background-position: 0 0;
}
100% {
background-position: 17px 0;
}
}
@keyframes progressmove {
0% {
background-position: 0 0;
}
100% {
background-position: 17px 0;
}
}
.progress {
height: 20px;
margin-bottom: 20px;
overflow: hidden;
background-color: #f5f5f5;
border-radius: 4px;
-webkit-box-shadow: inset 0 1px 2px rgba(0,0,0,0.1);
box-shadow: inset 0 1px 2px rgba(0,0,0,0.1);
}
.progress.active .progress-bar {
-webkit-animation: progress-bar-stripes 2s linear infinite;
animation: progress-bar-stripes 2s linear infinite;
}
.progress-striped .progress-bar {
background-image: linear-gradient(45deg,rgba(255,255,255,0.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,0.15) 50%,rgba(255,255,255,0.15) 75%,transparent 75%,transparent);
background-size: 40px 40px;
}
.progress-bar {
background-image: -webkit-linear-gradient(top,#428bca 0,#3071a9 100%);
background-image: linear-gradient(to bottom,#428bca 0,#3071a9 100%);
background-repeat: repeat-x;
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff428bca',endColorstr='#ff3071a9',GradientType=0);
}
.progress-bar {
float: left;
height: 100%;
font-size: 12px;
line-height: 20px;
color: #fff;
text-align: center;
background-color: #428bca;
box-shadow: inset 0 -1px 0 rgba(0,0,0,0.15);
transition: width .6s ease;
}
.btn-default {
text-shadow: 0 1px 0 #fff;
background-image: -webkit-linear-gradient(top,#fff 0,#e0e0e0 100%);
background-image: linear-gradient(to bottom,#fff 0,#e0e0e0 100%);
background-repeat: repeat-x;
}
.btn {
display: inline-block;
padding: 6px 12px;
margin-bottom: 0;
font-size: 14px;
font-weight: normal;
line-height: 1.428571429;
text-align: center;
white-space: nowrap;
vertical-align: middle;
cursor: pointer;
border: 1px solid transparent;
border-radius: 4px;
user-select: none;
}