当前位置: 首页 > article >正文

SpringBoot实现文件上传

1. 配置文件上传限制

application.yml

spring:
  servlet:
    multipart:
      max-file-size: 10MB
      max-request-size: 10MB

2. 创建文件上传控制器

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.UUID;

@RestController
public class FileController {

    //文件上传管理
    @PostMapping("/uploadFile")
    public String uploadFile(MultipartFile[] files){

        for(MultipartFile file:files){
            // 获取文件名以及后缀名
            String fileName = file.getOriginalFilename();
            // 重新生成文件名(根据具体情况生成对应文件名)
            fileName = UUID.randomUUID()+"_"+fileName;
            // 指定上传文件本地存储目录,不存在需要提前创建
            String dirPath = "D:/file/";
            File filePath = new File(dirPath);
            if(!filePath.exists()){
                filePath.mkdirs();
            }
            try {
                file.transferTo(new File(dirPath+fileName));
                return "上传成功";
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "上传失败";
    }

}

3. 创建文件上传页面

src/main/resources/backend目录下创建upload.html文件

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>文件上传</title>
</head>
<body>
    <form action="/uploadFile" method="post" enctype="multipart/form-data" >

        <input type="file" name="files"> <br> <br>
        <input type="submit" value="提交" >

    </form>
</body>
</html>

这里backend目录事先有做了静态资源映射:自定义静态资源的映射

4. 运行测试

运行Spring Boot项目并访问http://127.0.0.1:8080/backend/upload.html。选择文件并点击“提交”按钮,文件将被上传到指定的目录(在这个例子中是D:/file/)。
在这里插入图片描述
查看上传目录
在这里插入图片描述

如果需要把上传文件(图片)能通过http请求显示出来,需要做静态资源映射(自定义静态资源的映射)。

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
        registry.addResourceHandler("/images/**").addResourceLocations("file:D:/file/");
    }

}

配置完成后访问http://127.0.0.1:8080/images/目录下的指定图片(文件)。
在这里插入图片描述


http://www.kler.cn/a/583003.html

相关文章:

  • Excel两列和依次相减
  • 【C++入门】变量和基本类型
  • 版本控制器Git(2)
  • [数据结构]排序之希尔排序( 缩小增量排序 )
  • upload-labs-master通关攻略(13~16)
  • 计算机视觉|一文读懂NeRF:为3D场景重建带来新突破
  • CMD批处理一些冷门命令,编写windows脚本常用?
  • java每日精进 3.11 【多租户】
  • ST电机库电流采样 三电阻单ADC
  • Oracle VirtualBox安装CentOS 7
  • FFmpeg入门:最简单的音视频播放器(Plus优化版)
  • dns劫持是什么?常见的劫持类型有哪些?如何预防?
  • XML Schema 实例
  • 深入理解 HTML 链接:网页导航的核心元素
  • 【Deepseek基础篇】--v3基本架构
  • 初学者快速入门Python爬虫 (无废话版)
  • 从零开始的python学习(五)P75+P76+P77+P78+P79+P80
  • Linux ALSA 驱动核心知识点解析
  • OpenHarmony-分布式硬件关键技术
  • redis增加ip白名单