SpringMVC之文件上传

  上传是web程序中常见的功能,当使用上传时,需要把form表单中的enctype属性改为multipart/form-data,这样就使用了二进制进行上传,而后台需要解析这些数据。DispatcherServlet并不能解析它,而需要委托给Spring中的MultipartResolver接口实现去解析。在Spring3.1之后,Spring内置了两种MultipartResolver的实现:

  • CommonsMultipartResolver:使用了Jakarta Commons FileUpload来解析
  • StandardServletMultipartResolver:基于Serlvet3.0对multipart的支持

  本随笔将对二者就行举例:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isELIgnored="false"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="${pageContext.request.contextPath }/user/upload" method="post" enctype="multipart/form-data">
        <input name="txt" type="text" placeholder="文本信息"><br>
        <input name="file" type="file"><br>
        <input type="submit">
    </form>
</body>
</html>

Controller代码:

package cn.powerfully.demo.web.controller;

import java.io.IOException;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("user")
public class UserController {
    
    @RequestMapping(path = "upload", method = RequestMethod.GET)
    public String uploadUI(Model model) {
        return "upload";
    }

    @RequestMapping(path = "upload", method = RequestMethod.POST)
    public String upload(Model model, String txt, MultipartFile file) throws IllegalStateException, IOException {
        if (file == null) {
            model.addAttribute("msg", "上传失败!");
        } else {
            String msg = String.format("文本信息:%s<br>文件名:%s<br>文件大小:%d字节<br>", txt, file.getOriginalFilename(),
                    file.getSize());
       //... model.addAttribute(
"msg", msg); } return "msg"; } }

一、StandardServletMultipartResolver

  首先需要创建StandardServletMultipartResolver对象,可以在springMVC配置文件中添加如下代码,注意一定要加上id(按id装配)

<bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver" />

  对于上传的参数,不是在bean中配置的,而是在web.xml中的DispatcherServlet配置的:

<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <multipart-config>
        <location>/</location>
        <max-file-size>102400</max-file-size>
        <max-request-size>204800</max-request-size>
    </multipart-config>
</servlet>

  使用multipart-config节点进行配置,在multipart-config中,有四个子节点:

location 上传文件所存放的临时目录。必须指定
max-file-size 文件的最大大小,单位为字节。默认没有限制
max-request-size 请求的最大大小,单位为字节。默认没有限制
file-size-threshold 文件大小阈值,当大于这个阈值时将写入到磁盘,否则在内存中。默认值为0

二、CommonsMultipartResolver

  因为基于Jakarta Commons FileUpload,所以要先引入commons-fileupload

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>

  接着只需要在springMVC的配置文件中装载Bean即可

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

  要配置参数,只要对bean注入参数:

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="defaultEncoding" value="utf-8" /> 
    <property name="maxUploadSize" value="1024000" />
    <property name="maxInMemorySize" value="204800" />
    <property name="uploadTempDir" value="/" />
</bean>
defaultEncoding 默认的解析编码,默认为iso-8859-1
maxUploaadSize 上传文件的最大大小,默认为-1(无限制)
maxInMemorySize 内存缓冲区大小,超过时写入硬盘,默认为-1(无限制)
uploadTempDir   临时文件目录,默认是serlvet容器的临时目录

  除了以上参数,还有其他的......

posted @ 2017-01-28 15:55  兴趣使然~  阅读(2378)  评论(0编辑  收藏  举报