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

SpringMVC默認沒有配置上傳解析器

  • 使用SpringMVC來處理上傳必須添加對MultipartResolver上傳解析器的聲明配置。java

  • 配置以後,客戶端每次進行請求的時候,SpringMVC都會檢查request裏面是否包含有上傳文件信息的,若是包含了就會使用MultipartResolver進行解析git

  • SpringMVC會使用一個支持文件處理的MultipartHttpServletRequest來包裹當前的HttpServletRequest,而MultipartHttpServletRequest是能夠對文件進行處理的github

  • SpringMVC內置了一個MultipartResolver的實現類org.springframework.web.multipart.commons.CommsMultipartResolverweb

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/springspring

若是以爲個人文章或者代碼對您有幫助,能夠請我喝杯咖啡。您的支持將鼓勵我繼續創做!謝謝!
微信打賞
支付寶打賞apache

相關文章
相關標籤/搜索