在pom引入两个jar:
<!-- 文件上传配置 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 非必须操作,只是简化IO操作 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
1、具体代码。在addViewcontroller中添加界面 upload界面。在/SpringBoot1/src/main/resources/templates目录下新建一个upload界面
package com.example.Spring.Boot1.Interceptor;
import java.nio.charset.Charset;
import java.util.List;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@RestController
public class DemoInterceptorTest extends WebMvcConfigurerAdapter {
/************解决乱码的问题*******************************/
public HttpMessageConverter<String> responseBodyConverter() {
StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
return converter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
converters.add(responseBodyConverter());
}
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false);
}
/*******************这里是拦截器的调用********************/
public DemoInterceptor demoInterceptor() {
return new DemoInterceptor();
}
//注册拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(demoInterceptor());
}
/*转发页面
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// TODO Auto-generated method stub
registry.addViewController("/index2").setViewName("index2");
registry.addViewController("/toUploud").setViewName("upload");//尽量这个名字不要相同 前面参数自定义 ,后面参数是html/jsp的文件名字
}
}
2、具体上传代码Controller
package com.example.Spring.Boot1.controller;
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 {
/* 功能:上传文件
* /运行http://localhost:8011/toUploud就可以 这个是在 DemoInterceptorTest这个定义的界面上
上传成功后,文件就会出现在D:/upload这个路径下
*/
@RequestMapping("/upload")
public @ResponseBody String upload(MultipartFile file) { //接受上传文件
try {
FileUtils.writeByteArrayToFile(new File("D:/upload/"+file.getOriginalFilename()), file.getBytes());//快速写入磁盘
return "OK";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "wrong";
}
}
}
代码解释:MultipartFile file 接受上传文件
FileUtils.writeByteArrayToFile 快速写入磁盘中
3 、界面代码:<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>上传文件</title>
</head>
<body>
<div calss="upload">
<form action="upload" enctype="multipart/form-data" method="post" >
<input type="file" name="file" /><br/>
<input type="submit" value="上传">
</form>
</div>
</body>
</html>
最后实现界面:
运行http://localhost:8011/toUploud就可以出现下面的界面
选择任何一个文件,最后点击上传就会到 D:\upload 下。