加入依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
提交表单
<form action="user/fileUpload" method="post" enctype="multipart/form-data">
文件提交:<input type="file" name="Upload"/>
<input type="submit" value="提交">
</form>
代码
@RequestMapping(path = "/fileUpload", method = {RequestMethod.POST}, headers = {"Accept"})
public String fileUpload()
{
System.out.println("文件上传");
return "success";
}
传统的文件上传
@RequestMapping(path = "/fileUpload", method = {RequestMethod.POST}, headers = {"Accept"})
public String fileUpload(HttpServletRequest request) throws Exception {
System.out.println("文件上传");
String realPath = request.getSession().getServletContext().getRealPath("/uploads/");
File file = new File(realPath);
if (!file.exists())
{
file.mkdirs();
}
// 解析request对象,获取上传文件
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> filename = upload.parseRequest(request);
for (FileItem item : filename)
{
if (item.isFormField())
{
}else {
String fileName = item.getName();
// 生成唯一id
String replace = UUID.randomUUID().toString().replace("-", "");
fileName = replace +"_"+ fileName;
item.write(new File(realPath, fileName));
item.delete();
}
}
return "success";
}
}
Spring文件上传
使用文件解析器确定上传文件对象,再将上传的文件项MultipartFile(注意:上传的input的name值必须和参数MultipartFile的名一样)传入上传文件对象中
配置文件解析器
<!-- 配置文件解析器对象-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value= "10485760"/>
</bean>
代码
@RequestMapping(path = "/fileUpload2", method = {RequestMethod.POST}, headers = {"Accept"})
public String fileUpload2(HttpServletRequest request, MultipartFile Upload) throws Exception {
System.out.println("文件上传");
String realPath = request.getSession().getServletContext().getRealPath("/uploads/");
File file = new File(realPath);
if (!file.exists())
{
file.mkdirs();
}
String fileName = Upload.getOriginalFilename();
// 生成唯一id
String replace = UUID.randomUUID().toString().replace("-", "");
fileName = replace +"_"+ fileName;
Upload.transferTo(new File(realPath, fileName));
return "success";
}
跨服务器上传文件
@RequestMapping(path = "/fileUpload3", method = {RequestMethod.POST}, headers = {"Accept"})
public String fileUpload3(MultipartFile Upload) throws Exception {
String realPath = "http://localhost:8081/fileiamge_war_exploded/upload/";
System.out.println(realPath);
String fileName = Upload.getOriginalFilename();
// 生成唯一id
String replace = UUID.randomUUID().toString().replace("-", "");
fileName = replace +"-"+ fileName;
System.out.println(realPath + fileName);
//创建客户端对象
Client client = Client.create();
//和图片服务器进行连接
WebResource resource = client.resource("http://localhost:8081/upload/" + fileName);
//上传文件
resource.put(Upload.getBytes());
return "success";
}
网友评论