11【SpringMVC的文件上传】

文章目录

  • 五、文件上传
    • 5.1 回顾文件上传
    • 5.2 SpringMVC处理文件上传
      • 5.2.1 配置文件上传解析器
      • 5.3.2 准备表单
      • 5.3.3 编写处理器
    • 5.3 多文件上传
      • 5.3.1 准备表单
      • 5.3.2 编写处理器


五、文件上传

5.1 回顾文件上传

表单的entype类型必须改为multipart/form-data,提交方式必须为post提交(因为post提交才有请求体)

引入依赖:

<dependencies>
    
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webmvcartifactId>
        <version>5.2.9.RELEASEversion>
    dependency>

    
    <dependency>
        <groupId>com.fasterxml.jackson.coregroupId>
        <artifactId>jackson-databindartifactId>
        <version>2.9.8version>
    dependency>

    
    <dependency>
        <groupId>org.apache.tomcatgroupId>
        <artifactId>tomcat-apiartifactId>
        <version>8.5.41version>
    dependency>

    
    <dependency>
        <groupId>commons-fileuploadgroupId>
        <artifactId>commons-fileuploadartifactId>
        <version>1.4version>
    dependency>
dependencies>

5.2 SpringMVC处理文件上传

5.2.1 配置文件上传解析器


<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    
    <property name="maxUploadSize" value="102400000">property>
    
    <property name="defaultEncoding" value="UTF-8">property>
bean>

5.3.2 准备表单

  • Demo01.jsp:
<form action="/demo03/demo01" enctype="multipart/form-data" method="post">
    <input type="file" name="pic">
    <input type="submit">
form>

5.3.3 编写处理器

package com.dfbz.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.UUID;

/**
 * @author lscl
 * @version 1.0
 * @intro:
 */
@Controller
@RequestMapping("/demo01")
public class Demo01Controller {

    @RequestMapping("/demo01")
    public void demo01(
        // 当表单项名称和形参名不一样可以使用@RequestPart注解来指定名称
        @RequestPart("pic") MultipartFile pic,      
        HttpServletResponse response) throws Exception {


        // 表单项的名称
        String name = pic.getName();

        // 文件的名称
        String fileName = pic.getOriginalFilename();

        // 文件的大小
        long size = pic.getSize();

        // 请求的类型
        String contentType = pic.getContentType();

        // 将前端传递的文件写入到磁盘
        pic.transferTo(new File("D:/000/" + fileName));

        response.setContentType("text/html;charset=utf8");
        response.getWriter().write("
name: "
+ name); response.getWriter().write("
fileName: "
+ fileName); response.getWriter().write("
size: "
+ size); response.getWriter().write("
contentType: "
+ contentType); } }

5.3 多文件上传

5.3.1 准备表单

<form action="/demo01/demo02" enctype="multipart/form-data" method="post">
    <input type="file" name="pics">
    <input type="file" name="pics">
    <input type="file" name="pics">
    <input type="file" name="pics">
    <input type="submit">
form>

5.3.2 编写处理器

/**
 * 多文件上传
 *
 * @param pics
 * @throws Exception
 */
@RequestMapping("/demo02")
@ResponseBody
public String demo02(MultipartFile[] pics) throws Exception {

    for (MultipartFile file : pics) {
        file.transferTo(new File("D:/000/" + UUID.randomUUID() + "-" + file.getOriginalFilename()));
    }
    return "success";
}

你可能感兴趣的:(#,《SSM框架系列》,servlet,java,前端,springmvc,spring)