<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/12/20
Time: 23:57
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>文件上传 </title>
</head>
<body>
<form action="/user/fileupload1" method="post" enctype="multipart/form-data">
文件上传:<input type="file" name="upload"/><br/>
<input type="submit" value="传统提交"/><br/>
</form>
<form action="/user/fileupload2" method="post" enctype="multipart/form-data">
文件上传:<input type="file" name="upload1"/><br/>
<input type="submit" value="SpringMVC提交"/><br/>
</form>
</body>
</html>
package com.itheima.controller;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.UUID;
@Controller
@RequestMapping(value = "/user")
public class UserContreller {
@RequestMapping(value = "/fileupload1")
public String fileupload1(HttpServletRequest request) throws Exception {
System.out.println("文件上传111");
String path = request.getSession().getServletContext().getRealPath("/uploads/");
System.out.println(path);
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> fileItems = upload.parseRequest(request);
for (FileItem item : fileItems) {
if (item.isFormField()) {
} else {
String uuid = UUID.randomUUID().toString().replace("-", "");
String filename = uuid + item.getName();
item.write(new File(path, filename));
item.delete();
}
}
return "success";
}
@RequestMapping(value = "/fileupload2")
public String fileupload2(HttpServletRequest request, MultipartFile upload1) throws Exception {
System.out.println("文件上传222");
String path = request.getSession().getServletContext().getRealPath("/uploads/");
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
String name = upload1.getOriginalFilename();
String uuid = UUID.randomUUID().toString().replace("-", "");
String filename = uuid + name;
upload1.transferTo(new File(path, filename));
return "success";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置扫描-->
<context:component-scan base-package="com.itheima"/>
<!-- 视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 文件解析器对象-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485760"/>
</bean>
<!-- 前段控制器,哪些静态资源不拦截 !!! location 可以没有** 但是mapping 一定要有** springmvc注解-->
<mvc:resources mapping="/js/**" location="/js/**"/>
<mvc:resources mapping="/css/**" location="/css/**"/>
<mvc:resources mapping="/images/**" location="/images/**"/>
<mvc:annotation-driven/>
</beans>