• SpringMVC 系列教程1-文件上传-配置


    SpringMVC默认没有配置上传解析器

    • 使用SpringMVC来处理上传必须添加对MultipartResolver上传解析器的声明配置。

    • 配置之后,客户端每次进行请求的时候,SpringMVC都会检查request里面是否包含有上传文件信息的,如果包含了就会使用MultipartResolver进行解析

    • SpringMVC会使用一个支持文件处理的MultipartHttpServletRequest来包裹当前的HttpServletRequest,而MultipartHttpServletRequest是可以对文件进行处理的

    • SpringMVC内置了一个MultipartResolver的实现类org.springframework.web.multipart.commons.CommsMultipartResolver

    pom.xml

    <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>com.jege.spring.mvc</groupId>
        <artifactId>spring-mvc-upload-multipart-file</artifactId>
        <version>1.0.RELEASE</version>
        <packaging>war</packaging>
    
        <name>spring-mvc-upload-multipart-file</name>
        <url>http://maven.apache.org</url>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <springframework.version>3.2.15.RELEASE</springframework.version>
            <junit.version>4.12</junit.version>
            <java.version>1.7</java.version>
        </properties>
    
        <dependencies>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${springframework.version}</version>
            </dependency>
    
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>1.2.2</version>
            </dependency>
    
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>2.2</version>
            </dependency>
    
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
                <scope>provided</scope>
            </dependency>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
    
        </dependencies>
    
        <build>
            <finalName>spring-mvc-upload-multipart-file</finalName>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.eclipse.jetty</groupId>
                    <artifactId>jetty-maven-plugin</artifactId>
                    <version>9.3.14.v20161028</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.6</version>
                    <configuration>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    

    applicationContext.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: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">
    
        <!-- 组件扫描 -->
        <context:component-scan base-package="com.jege.spring.mvc" />
    
        <!-- 引入spirng-mvc配置文件 -->
        <import resource="classpath:config/applicationContext-mvc.xml" />
    
    </beans>

    applicationContext-mvc.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" 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/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <!-- 配置视图解析器 -->
        <bean 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">
            <!-- 配置上传文件最大尺寸为1000MB -->
            <property name="maxUploadSize" value="1048576000" />
            <!-- 设置在文件上传时允许写到内存中的最大值,以字节为单位计算,默认是10240 -->
            <property name="maxInMemorySize" value="4096" />
            <!-- 表示用来解析request请求的默认编码格式,当没有指定的时候根据Servlet规范会使用默认值ISO-8859-1。 -->
            <!-- 当request自己指明了它的编码格式的时候就会忽略这里指定的defaultEncoding -->
            <property name="defaultEncoding" value="UTF-8" />
        </bean>
    
        <!-- 可以访问静态资源 -->
        <mvc:default-servlet-handler />
    
        <!-- 解析@RequestMapping -->
        <mvc:annotation-driven />
    
    </beans>

    源码地址

    https://github.com/je-ge/spring

    如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。您的支持将鼓励我继续创作!谢谢!
    微信打赏
    支付宝打赏

  • 相关阅读:
    PHP中读写文件
    Java和PHP在Web开发方面的比较
    CentOS yum安装 Apache + PHP + MySQL
    PHP 时间参数大全
    数据库结果太大,PHP超出内存限制
    PHP扩展配置?
    php 实现KMP算法
    OS + Linux Shell bash / sh / ksh / csh / tcsh / adb shell
    my soft / text tools / EmEditor / Notepad++ / UltraEdit / EditPlus / Sublime Text
    JAVA EE Projects_sourceforge
  • 原文地址:https://www.cnblogs.com/je-ge/p/6287013.html
Copyright © 2020-2023  润新知