Java:SpringBoot实现文件上传

简介: Java:SpringBoot实现文件上传

后端上传代码如下


package com.mouday.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
 * 文件上传
 * 参考
 * https://blog.csdn.net/gnail_oug/article/details/80324120
 */
@Controller
public class FileController {
    // 获取上传测试页面
    @GetMapping("/upload")
    public String upload() {
        return "upload";
    }
    // 单文件上传
    @PostMapping("/upload")
    @ResponseBody
    public String upload(MultipartFile file) {
        if (file.isEmpty()) {
            return "file is Empty";
        }
        String filename = file.getOriginalFilename();
        try {
            File path = this.getUploadDirectory();
            File dest = new File(path, filename);
            System.out.println(dest.getPath());
            file.transferTo(dest);
            return new File("/upload", filename).toString();
        } catch (IOException e) {
            e.printStackTrace();
            return "error";
        }
    }
    // 多文件上传
    @PostMapping("/multiUpload")
    @ResponseBody
    public List<String> upload(@RequestParam("file") List<MultipartFile> files) {
        List<String> list = new ArrayList<>();
        for (MultipartFile file : files) {
            list.add(this.upload(file));
        }
        return list;
    }
    /**
     * 获取文件保存路径
     * 参考:https://www.bbsmax.com/A/GBJrE67Wz0/
     *
     * @return
     * @throws FileNotFoundException
     */
    public File getUploadDirectory() throws FileNotFoundException {
        String pathName = ResourceUtils.getURL("classpath:").getPath();
        File path = new File(pathName, "/public/upload");
        if (!path.exists()) {
            path.mkdirs();
        }
        return path;
    }
}

前端代码


src/main/resources/templates/upload.html


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h2>单文件上传</h2>
<form action="/upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" value="提交">
</form>
<h2>多文件上传</h2>
<form action="/multiUpload" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="file" name="file">
    <input type="file" name="file">
    <input type="submit" value="提交">
</form>
</body>
</html>

因为前端页面需要使用模板引擎,所以需要引入依赖


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

application.properties 配置上传的文件大小限制

# 上传文件总的最大值
spring.servlet.multipart.max-request-size=10MB
# 单个文件的最大值
spring.servlet.multipart.max-file-size=10MB

参考


Spring Boot教程(十三):Spring Boot文件上传

Spring Boot 上传文件 获取项目根路径 物理地址 resttemplate上传文件

相关文章
|
17小时前
|
移动开发 前端开发 NoSQL
ruoyi-nbcio从spring2.7.18升级springboot到3.1.7,java从java8升级到17(二)
ruoyi-nbcio从spring2.7.18升级springboot到3.1.7,java从java8升级到17(二)
|
2天前
|
前端开发 Java 关系型数据库
Java医院绩效考核系统源码B/S架构+springboot三级公立医院绩效考核系统源码 医院综合绩效核算系统源码
作为医院用综合绩效核算系统,系统需要和his系统进行对接,按照设定周期,从his系统获取医院科室和医生、护士、其他人员工作量,对没有录入信息化系统的工作量,绩效考核系统设有手工录入功能(可以批量导入),对获取的数据系统按照设定的公式进行汇算,且设置审核机制,可以退回修正,系统功能强大,完全模拟医院实际绩效核算过程,且每步核算都可以进行调整和参数设置,能适应医院多种绩效核算方式。
20 2
|
4天前
|
Java
springboot项目出现Exception in thread “main“ java.lang.NoClassDefFoundError: javax/servlet/Filter
springboot项目出现Exception in thread “main“ java.lang.NoClassDefFoundError: javax/servlet/Filter
12 0
|
4天前
|
传感器 人工智能 前端开发
JAVA语言VUE2+Spring boot+MySQL开发的智慧校园系统源码(电子班牌可人脸识别)Saas 模式
智慧校园电子班牌,坐落于班级的门口,适合于各类型学校的场景应用,班级学校日常内容更新可由班级自行管理,也可由学校统一管理。让我们一起看看,电子班牌有哪些功能呢?
45 4
JAVA语言VUE2+Spring boot+MySQL开发的智慧校园系统源码(电子班牌可人脸识别)Saas 模式
|
9天前
|
缓存 前端开发 Java
15:Servlet 3.0文件上传与下载-Java Web
15:Servlet 3.0文件上传与下载-Java Web
24 5
|
11天前
|
Java 测试技术 Maven
Spring Boot单元测试报错java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]
Spring Boot单元测试报错java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]
|
12天前
|
安全 Java 测试技术
利用Java反射机制提高Spring Boot的代码质量:概念与实战
【4月更文挑战第29天】Java反射机制提供了一种强大的方法来在运行时检查或修改类和对象的行为。在Spring Boot应用中,合理利用反射可以提高代码的灵活性和可维护性。本篇博客将探讨Java反射的核心概念,并展示如何通过反射提高Spring Boot项目的代码质量。
29 0
|
17天前
|
Java Nacos 开发者
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例
|
23天前
|
人工智能 前端开发 Java
Java语言开发的AI智慧导诊系统源码springboot+redis 3D互联网智导诊系统源码
智慧导诊解决盲目就诊问题,减轻分诊工作压力。降低挂错号比例,优化就诊流程,有效提高线上线下医疗机构接诊效率。可通过人体画像选择症状部位,了解对应病症信息和推荐就医科室。
185 10
|
前端开发 Java Spring
Java:SpringBoot实现文件上传
Java:SpringBoot实现文件上传
106 0