后端:
@Controller
public class fileController {
@RequestMapping("/text")
public String test(HttpServletRequest request) throws Exception {
System.out.println("test执行了 ");
//创建request
String realPath = request.getSession().getServletContext().getRealPath("/SpringMVC/");
//判断当前项目的根目录有没有这个文件,没有的话就创建
File file = new File(realPath);
if (!file.exists()){
//创建文件
file.mkdir();
}
//解析request对象,获取上传文件项
//磁盘文件空间
DiskFileItemFactory itemFactory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(itemFactory);
//解析request
List<FileItem> fileItems = upload.parseRequest(request);
//遍历
for (FileItem item:fileItems) {
//进行判断,当前item对象是否是上文件
if (item.isFormField()){
//说明普通表单间
}else{
//说明上传文件项
//获取上传文件的名称
String filename = item.getName();
//完成文件上传
item.write(new File(realPath,filename));
//临时文件的删除
item.delete();
}
}
return "success";
}
}
JSP:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/11/22 0022
Time: 22:06
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="text" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="uplaod">
<input type="submit" value="上传">
</form>
</body>
</html>
Spring配置文件:
Spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--开启扫描-->
<context:component-scan base-package="com">
</context:component-scan>
<!--视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--prefix用于查找目录-->
<property name="prefix" value="/WEB-INF/pages/"></property>
<!--suffix用于指定目录下的文件后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
<!--注册自定义数据转换器-->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.young.FormatTest"></bean>
</set>
</property>
</bean>
<!-- <mvc:resources mapping="/css/**" location="/css/"></mvc:resources>-->
<!--设置前端静态资源不过滤-->
<mvc:resources mapping="/js/**" location="/js/"/>
<!--开始SpringMVC的注解支持-->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven >
<!-- <mvc:resources mapping="/css/**" location="/css/"></mvc:resources>-->
<!-- <mvc:resources mapping="/images/**" location="/images/"></mvc:resources>-->
<!-- <mvc:resources mapping="" location=""-->
</beans>
servlet配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--欢迎页-->
<welcome-file-list>
<welcome-file>file.jsp</welcome-file>
<welcome-file>four.jsp</welcome-file>
<welcome-file>anno.jsp</welcome-file>
<welcome-file>account.jsp</welcome-file>
<welcome-file>param.jsp</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--配置DispatcherServlet-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--加载前端控制器-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
<!--启动时创建contextConfigLocation对象来加载spring.xml文件-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<!-- / 代表发任何请求,这里DispatcherServlet都会拦截-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--建立一个过滤器,解决中文乱码-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SpringMVC</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--spring core-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!--SpringMVC -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!--spring context-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!--spring web-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!--serlver api-->
<!-- https://mvnrepository.com/artifact/org.mortbay.jetty/servlet-api -->
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5-6.0.0</version>
</dependency>
<!--JSP api-->
<!-- https://mvnrepository.com/artifact/javax.servlet/jsp-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.0</version>
<scope>test</scope>
</dependency>
<!--JQuery-->
<!-- https://mvnrepository.com/artifact/org.webjars/jquery -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>2.1.0</version>
</dependency>
<!--Jaskson-->
<!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-core-asl -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!--io-->
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<!--fileupload-->
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
</dependencies>
</project>