SpringMvc4x高级配置(一) 文件上传配置

一. 点睛

文件上传是一个项目里经常要用到的功能,Spring MVC通过配置一个MultipartResolver来上传文件。

Spring的控制器中,通过MultipartFile file来接收文件,通过MultipartFile[] files接收多个文件上传。

二. 示例

  1. 添加文件上传依赖

1
2
3
4
5
6
7
8
9
10
11
12
复制代码<!-- file upload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!-- 非必需,可简化IO操作 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
  1. 增加上传页面upload.jsp。

src/main/resources/views下新建upload.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
复制代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>upload page</title>

</head>
<body>

<div class="upload">
<form action="upload" enctype="multipart/form-data" method="post">
<input type="file" name="file"/><br/>
<input type="submit" value="上传">
</form>
</div>


</body>
</html>
  1. 添加转向到upload页面的ViewController

在文件MyMvcConfigaddViewControllers方法里面增加下面的转向配置,代码如下:

1
复制代码registry.addViewController("/toUpload").setViewName("/upload");

添加完成之后的代码如下所示:

1
2
3
4
5
6
复制代码
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/index").setViewName("/index");
registry.addViewController("/toUpload").setViewName("/upload");
}
  1. MultipartResolver配置

在文件MyMvcConfig中增加下面的代码,提供对上传文件的支持:

1
2
3
4
5
6
复制代码@Bean
public MultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(1000000);
return multipartResolver;
}
  1. 控制器

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
复制代码package org.light4j.springMvc4.web;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
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;

@Controller
public class UploadController {

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody
String upload(MultipartFile file) {// ①

try {
FileUtils.writeByteArrayToFile(new File("e:/upload/" + file.getOriginalFilename()),file.getBytes()); // ②
return "ok";
} catch (IOException e) {
e.printStackTrace();
return "wrong";
}
}
}

代码解释:

① 使用MultipartFile file接受上传的文件。
② 使用FileUtils.writeByteArrayToFile快速写文件到磁盘。

  1. 运行

访问http://localhost/springMvc4.x-fileUpload/toUpload效果如下图所示:
xxx
单击”上传”按钮之后,上传文件之后页面显示ok,如下图所示:

查看f:/upload/文件夹下面增加了刚刚上传的文件,如下图所示:
xxx

三. 源代码示例:

github地址:点击查看
码云地址:点击查看

打赏 欢迎关注人生设计师的微信公众账号
公众号ID:longjiazuoA

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%