Spring MVC 导入文件详解

该博客介绍了Spring Boot项目文件上传的开发步骤。首先要创建Spring Boot项目并添加web依赖,编写jsp页面时需将form表单声明为multipart/form-data。开发控制器类有三种方式,分别是入参HttpServletRequest、直接使用MultipartFile对象、使用Servlet的API。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

准备工作

先建一个springboot项目
在这里插入图片描述
添加web依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

页面编写及注意事项

jsp页面

<form action="./upload" method="post" enctype="multipart/form-data">
    上传文件:<input type="file" name="file" value="请选择上传的文件">
    <input type="submit" value="上传"><br><br>
</form>

注意
form 表单声明为multipart/form-data,如果没有这个声明,Spring MVC会解析文件请求出错

开发控制器类

配置文件配置

#是否启用Spring MVC多分部上传功能
spring.servlet.multipart.enabled=true
# 指定默认上传的文件夹
spring.servlet.multipart.location=E:\work\download
#将文件写入磁盘的阈值。值可以使用后缀MB或KB来表示兆字节或字节大小
spring.servlet.multipart.file-size-threshold=0
#限制单个文件最大大小
spring.servlet.multipart.max-file-size=1MB
#限制所有文件最大大小
spring.servlet.multipart.max-request-size=10MB

1. 入参HttpServletRequest

控制器

package com.example.mvcdemo.controller;/**
 * Created by weimingyue on 2021/1/10
 */

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * @ClassName: FileController
 * @Description TODO
 * @Author: weimingyue
 * @Date: 2021/1/10 10:08
 * @Version: 1.0
 */

@Controller
@RequestMapping("/file")
public class FileController {

    @GetMapping("/upload/page")
    public String uploadPage(){
        return "/file/upload";
    }

    //
    // 使用HttpServletRequest
    @PostMapping("/upload/request")
    @ResponseBody
    public JSONObject uploadReauest(HttpServletRequest request){
        JSONObject jsonObject = new JSONObject();
        boolean flag=false;
        MultipartHttpServletRequest multipartHttpServletRequest = null;
        if(request instanceof MultipartHttpServletRequest){
            multipartHttpServletRequest = (MultipartHttpServletRequest) request;
        }else{
            jsonObject.put("msg","传输类型不为file");
            return jsonObject;
        }
        MultipartFile file = multipartHttpServletRequest.getFile("upload");
        String fileName = file.getOriginalFilename();
        System.out.println(fileName);
        File newFile= new File(fileName);
        try {
            file.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
            jsonObject.put("msg","保存文件失败");
            return jsonObject;
        }

        return jsonObject;
    }



}

2. 直接使用MultipartFile对象

@PostMapping("/upload/multipartFile")
    @ResponseBody
    public JSONObject uploadMultipartFile(MultipartFile file){
        JSONObject jsonObject = new JSONObject();
        String name = file.getOriginalFilename();
        File dest = new File(name);
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
            jsonObject.put("msg","保存文件失败");
            return jsonObject;
        }
        return jsonObject;
    }

3. 使用Servlet的API

@PostMapping("/upload/part")
    @ResponseBody
    public JSONObject uploadPart(Part file){
        JSONObject jsonObject = new JSONObject();
        String name = file.getSubmittedFileName();
        try {
            file.write(name);
        } catch (IOException e) {
            e.printStackTrace();
            jsonObject.put("msg","保存文件失败");
            return jsonObject;
        }
        return jsonObject;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值