使用fileupload

文章目录
  1. 1. js
  2. 2. java后台实现

使用commons-fileupload插件来帮我们处理上传后的数据而让我们自己手动处理的话,也是可以的,但是十分麻烦,因为我们需要将所有的请求体获取到,


首先maven下载fileupload和commons-io可以这两个类可以对浏览器的发送请求头保存,并能实现结束

1
2
3
4
5
6
7
8
9
10
11
<!--upload 文件-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>

浏览器端的代码

#html #

1
2
3
4
5
6
7
8
9
10
<br>
<!--<form enctype="multipart/form-data">-->
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">拖入文件上传</h4>
</div>
<div class="form-group">
<input id="file-1" type="file" class="file" data-overwrite-initial="false"
data-min-file-count="1">
</div>

js

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
//文件上传
$("#file-1").fileinput({
language: 'zh', //设置语言
uploadUrl: "adminAddRoute?methodName=updatePic", //上传的地址
showUpload: true, //是否显示上传按钮
showCaption: true,//是否显示标题
browseClass: "btn btn-primary", //按钮样式 previewId, index
uploadExtraData: function () {
var data = {
parentId: $("#fileType").val()
};
return data;
},
'theme': 'explorer',
maxFileCount: 1, //表示允许同时上传的最大文件个数
enctype: 'multipart/form-data',
validateInitialCount: true,
msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}!",
allowedFileExtensions : ['jpg', 'png','gif'],
overwriteInitial: false,
maxFileSize: 1000,
maxFilesNum: 1,
//allowedFileTypes: ['image', 'video', 'flash'],
slugCallback: function(filename) {
return filename.replace('(', '_').replace(']', '_');
}
});

将html文档放在需要实现的地方即可

java后台实现

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
String path_temp = this.getServletContext().getRealPath("temp");
//DiskFileItemFactory factory = new DiskFileItemFactory(1024*1024, new File(path_temp));
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(1024*1024);
factory.setRepository(new File(path_temp));
//2、创建文件上传的核心类
ServletFileUpload upload = new ServletFileUpload(factory);
//设置上传文件的名称的编码
upload.setHeaderEncoding("UTF-8");
List<FileItem> fileItems = upload.parseRequest(request);
FileItem item = fileItems.get(0);
//文件上传项
//文件的名
String fileName1 = item.getName();
System.out.println("上传文件名称 = " + fileName1);
String uuid = UUID.randomUUID().toString().replace("-", "");

String fileName = uuid + ".jpg";
//获得上传文件的内容
InputStream in = item.getInputStream();

//设置图片保存名字和路径
String path_store = this.getServletContext().getRealPath("img/product");
// if(path_store)


String st = path_store + "/" + fileName;
OutputStream outSize2 = new FileOutputStream(st);
// OutputStream outSize4 = new FileOutputStream(path_store+"/size4/"+fileName);
// OutputStream outSize3 = new FileOutputStream(path_store+"/size3/"+fileName);

//保存文件
IOUtils.copy(in, outSize2);
in.close();
outSize2.close();