springMVC上传文件

目录

🥑引入依赖

🥑配置文件上传解析器

🥑文件上传页面(目的:简单演示) 

🍑html

🍑controller层

🍒 真正的上传操作

🍑注意

🥑结果演示

🥑附加:

🍑web.xml

🍑springMVC.xml


引入依赖

<!--文件上传依赖-->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.4</version>
    </dependency>

配置文件上传解析器

在springMVC.xml文件中配置

<!--    文件上传解析器-->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--编码格式-->
        <property name="defaultEncoding" value="utf-8"/>
        <!--文件上传的大小限制,以字节的形式配置,-1 表示没有限制-->
        <property name="maxUploadSize" value="-1"/>
    </bean>

文件上传页面(目的:简单演示) 

html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>文件上传下载</title>
</head>
<body>
<form th:action="@{/files/uplode}" method="post" enctype="multipart/form-data">
  <input type="file" name="newfile">
   <br><br>
  <input type="submit" value="提交">
</form>
</body>
</html>

controller层

 真正的上传操作

    //上传
    @RequestMapping("uplode")
    public String uplodeFile(MultipartFile newfile, HttpServletRequest req)
            throws IOException {
        //获取上传的文件的全名称
        String filename = newfile.getOriginalFilename();
        //防止文件名重复,通过UUID生成随机名称
        filename = UUID.randomUUID().toString().replaceAll("-","")+
                   "-"+filename;
        //获取服务器上下文,并获取路径,
        // 也可以自己指定一个存放路径,例如在存到D盘:String path = "D:/"+filename;
        String realPath = req.getServletContext().getRealPath("/uplode/");
        //确定上传文件的路径
        String path = realPath+filename;
        //创建存放的目录
        File file = new File(path);
        if(!file.exists()){
            //创建目录
            file.mkdir();
        }
        //把文件写入磁盘!
        newfile.transferTo(file);
        return "hello";
    }

注意

结果演示

启动服务器

附加:

web.xml

<?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">
  <!-- 配置SpringMVC的前端控制器,对浏览器发送的请求统一进行处理 -->
  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 通过初始化参数指定SpringMVC配置文件的位置和名称 -->
    <init-param>
      <!-- contextConfigLocation为固定值 -->
      <param-name>contextConfigLocation</param-name>
      <!-- 使用classpath:表示从类路径查找配置文件,例如maven工程中的 src/main/resources -->
      <param-value>classpath:spring-MVC.xml</param-value>
    </init-param>
    <!-- 作为框架的核心组件,在启动过程中有大量的初始化操作要做 而这些操作放在第一次请求时才执行会严重影响访问速度 因此需要通过此标签将启动控制DispatcherServlet的初始化时间提前到服务器启动时 -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <!--设置springMVC的核心控制器所能处理的请求的请求路径 /所匹配的请求可以是/login或.html或.js或.css方式的请求路径 但是/不能匹配.jsp请求路径的请求 -->
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

springMVC.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 自动扫描包 base-package 扫描的包写上自己的controller层-->
    <context:component-scan base-package="com.soft.controller"/>
    <!-- 配置Thymeleaf视图解析器 -->
    <bean id="viewResolver"
          class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="order" value="1"/>
        <property name="characterEncoding" value="UTF-8"/>
        <property name="templateEngine">
            <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                <property name="templateResolver">
                    <bean
                            class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
                        <!-- 视图前缀 -->
                        <property name="prefix" value="/WEB-INF/templates/"/>
                        <!-- 视图后缀 -->
                        <property name="suffix" value=".html"/>
                        <property name="templateMode" value="HTML5"/>
                        <property name="characterEncoding" value="UTF-8" />
                    </bean>
                </property>
            </bean>
        </property>
    </bean>
    <!--
    处理静态资源,例如html、js、css、jpg
    若只设置该标签,则只能访问静态资源,其他请求则无法访问
    此时必须设置<mvc:annotation-driven/>解决问题
    -->
    <mvc:resources mapping="/js/*" location="/WEB-INF/js/"/>
    <mvc:default-servlet-handler/>
    <!-- 开启mvc注解驱动 -->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <!-- 处理响应中文内容乱码 -->
            <bean
                    class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="defaultCharset" value="UTF-8" />
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html</value>
                        <value>application/json</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
<!--    文件上传解析器-->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--编码格式-->
        <property name="defaultEncoding" value="utf-8"/>
        <!--文件上传的大小限制,以字节的形式配置,-1 表示没有限制-->
        <property name="maxUploadSize" value="-1"/>
    </bean>
</beans>

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值