一定要足够优秀才能堵住悠悠众口。
文件上传功能在SpringMVC中有两种方法(两种不同的处理器)
这篇文章我们将通过这两种方法来实现文件上传功能
创建一个新的Maven项目并在项目中进行基础的环境配置
- <dependency>
- <groupId>commons-fileuploadgroupId>
- <artifactId>commons-fileuploadartifactId>
- <version>1.4version>
- dependency>
注意:这里创建的是动态网页
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>Titletitle>
- head>
- <body>
- <form action="/play" method="post" enctype="multipart/form-data">
- <input type="file" name="file">
- <input type="submit" value="提交">
- form>
- body>
- html>
- package fileupload_demo1;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- import org.springframework.web.multipart.MultipartFile;
-
- import javax.servlet.http.HttpServletRequest;
- import java.io.File;
- import java.io.IOException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.UUID;
-
- @Controller
- public class CT1 {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/yyyy/MM/dd/"); //创建日期,并标注时间显示格式
- @GetMapping("/upload")
- public String play1(){
- return "hh";
- }
- @RequestMapping("/play")
- @ResponseBody
- public String play1(MultipartFile file, HttpServletRequest req) throws IOException {
- String format = simpleDateFormat.format(new Date()); //获取今天的日期
- String realpath = req.getServletContext().getRealPath("/img") +format; //获取Tomcat中的临时储存文件
- File folder = new File(realpath);
- if(!folder.exists()){ //检查目录是否存在
- folder.mkdirs(); //目录不存在进行创建
- }
- String oldname = file.getOriginalFilename(); //得到上传时的文件名
- String name = UUID.randomUUID().toString()+oldname.substring(oldname.lastIndexOf(".")); //UUID.randomUUID().toString():Java中自动生成主键的方法,机器中生成的一串生成数字
- file.transferTo(new File(folder,name)); //将前端传来的数据保存指定名字保存在指定文件夹下
- String url = req.getScheme()+ "://"+req.getServerName() +":"+req.getServerPort() +"/img" +format +name;
- return url;
-
- //http://localhost:8080/img/2022/11/13/8d3910d1-2a7c-47ce-8251-3cc4eeaf0bc4.jpeg
-
- }
-
- }
注意:下面标注了代码获取的结果
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
-
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
-
-
- <context:component-scan base-package="fileupload_demo1">context:component-scan>
- <mvc:annotation-driven/>
- <mvc:resources mapping="/**" location="/">mvc:resources>
- <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver">
-
- <property name="defaultEncoding" value="UTF-8"/>
-
- <property name="maxUploadSize" value="1048576"/>
-
- <property name="maxUploadSizePerFile" value="1048576"/>
-
- <property name="maxInMemorySize" value="4096"/>
-
- <property name="uploadTempDir" value="file:///E:\\tmp"/>
- bean>
-
-
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="modelAndView">
- <property name="prefix" value="/jsp/">property>
- <property name="suffix" value=".jsp">property>
- bean>
-
- beans>
这种方法的配置基本上与第一种方法相同,没有很大的区别
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
-
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
-
-
- <context:component-scan base-package="fileupload_demo1">context:component-scan>
- <mvc:annotation-driven/>
- <mvc:resources mapping="/**" location="/">mvc:resources>
- <bean class="org.springframework.web.multipart.support.StandardServletMultipartResolver" id="multipartResolver">bean>
-
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="modelAndView">
- <property name="prefix" value="/jsp/">property>
- <property name="suffix" value=".jsp">property>
- bean>
-
- beans>
- "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">
-
- <context-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:spring.xmlparam-value>
- context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
- listener>
-
-
- <servlet>
- <servlet-name>springmvcservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
- <init-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:SpringMVC.xmlparam-value>
- init-param>
-
- <multipart-config>
-
-
- <max-file-size>1048576max-file-size>
-
- <max-request-size>1048576max-request-size>
-
- <file-size-threshold>4096file-size-threshold>
- multipart-config>
- servlet>
- <servlet-mapping>
- <servlet-name>springmvcservlet-name>
- <url-pattern>/url-pattern>
- servlet-mapping>
- <filter>
- <filter-name>encodingfilter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
- <init-param>
- <param-name>encodingparam-name>
- <param-value>UTF-8param-value>
- init-param>
- <init-param>
- <param-name>forceRequestEncodingparam-name>
- <param-value>trueparam-value>
- init-param>
- <init-param>
- <param-name>forceResponseEncodingparam-name>
- <param-value>trueparam-value>
- init-param>
- filter>
- <filter-mapping>
- <filter-name>encodingfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
- web-app>