SpringMVC笔记05 文件上传
coconutnut

https://www.bilibili.com/video/BV1Sb411s7qa

P33~38

传统方式

上传的文件和访问的应用在同一台服务器上

依赖

使用 Commons-fileupload 组件

1
2
3
4
5
6
7
8
9
10
11
<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.4</version>
</dependency>

jsp页面

1
2
3
4
<form action="/user/fileupload1" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="upload" /><br/>
<input type="submit" value="上传" />
</form>

控制器

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
// 使用fileupload组件完成文件上传
@RequestMapping("/fileupload1")
public String fileuoload1(HttpServletRequest request) throws Exception {

// 上传的位置
String path = request.getSession().getServletContext().getRealPath("/uploads/");
// 判断路径是否存在
File file = new File(path);
if(!file.exists()){
// 创建该文件夹
file.mkdirs();
}

// 解析request对象,获取上传文件项
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
// 解析request
List<FileItem> items = upload.parseRequest(request);
// 遍历
for(FileItem item:items){
// 判断当前item对象是否是上传文件项
if(item.isFormField()){
// 普通表单向
}else{
// 获取上传文件的名称
String filename = item.getName();
// 把文件的名称设置唯一值,uuid
String uuid = UUID.randomUUID().toString().replace("-", "");
filename = uuid+"_"+filename;
// 完成文件上传
item.write(new File(path,filename));
// 删除临时文件
item.delete();
}
}

return "success";
}

测试上传后

在target/uploads中找到上传的文件

SpringMVC传统方式上传

原理分析

jsp页面

和传统方式一样

1
2
3
4
<form action="/user/fileupload2" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="upload" /><br/>
<input type="submit" value="上传" />
</form>

配置文件解析器

spingmvc.xml中

1
2
3
4
<!--配置文件解析器对象-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10*1024*1024" />
</bean>

里面可以设置一些属性,如文件大小

控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@RequestMapping("/fileupload2")
public String fileuoload2(HttpServletRequest request, MultipartFile upload) throws Exception {

// 上传的位置
String path = request.getSession().getServletContext().getRealPath("/uploads/");
// 判断该路径是否存在
File file = new File(path);
if(!file.exists()){
// 创建该文件夹
file.mkdirs();
}

// 获取上传文件的名称
String filename = upload.getOriginalFilename();
// 把文件的名称设置唯一值,uuid
String uuid = UUID.randomUUID().toString().replace("-", "");
filename = uuid+"_"+filename;
// 完成文件上传
upload.transferTo(new File(path,filename));

return "success";
}

相较传统方式,省了解析操作,简化上传步骤

SpringMVC跨服务器上传

用2个tomcat服务器,文件服务器端口号此处用的9090

文件服务器

新建一个工程

在webapp下新建uploads文件夹用来存文件

启动

依赖

1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.18.1</version>
</dependency>

<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.18.1</version>
</dependency>

jsp页面

仍没什么区别

1
2
3
4
<form action="/user/fileupload3" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="upload" /><br/>
<input type="submit" value="上传" />
</form>

控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@RequestMapping("/fileupload3")
public String fileuoload3(MultipartFile upload) throws Exception {

// 定义上传文件服务器路径
String path = "http://localhost:9090/uploads/";

// 获取上传文件的名称
String filename = upload.getOriginalFilename();
// 把文件的名称设置唯一值,uuid
String uuid = UUID.randomUUID().toString().replace("-", "");
filename = uuid+"_"+filename;

// 创建客户端的对象
Client client = Client.create();

// 和图片服务器进行连接
WebResource webResource = client.resource(path + filename);

// 上传文件
webResource.put(upload.getBytes());

return "success";
}

出现409异常可以手动创建一下uploads文件夹