springboot实现多文件上传

一说明

spingMVC支持文件上传,我们通过Apach 的 commons-fileupload 包的CommonsMultipartResolver 去实现了
spingMVC的MultipartResolver 。
本文章的示例是个简单的多文件上传,根据不同的业务自行修改。

二pom.xlm

<dependencies>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

三 application.yml

spring:
  servlet:
    multipart:
      max-file-size: 200MB #单个文件上传大小
      max-request-size: 600MB #连续上传文件大小

youku1327:
 file:
  root:
    path: "C:\\mydata\\generator\\version06\\" #存储路径

四controller

/**
 * @Author lsc
 * @Description <p> </p>
 * @Date 2019/10/2 20:58
 * @Version 1.0
 */
@RestController
public class FileUploadController {

    @Value("${youku1327.file.root.path}")
    private String fileRootPath;

    @PostMapping("/file/upload")
    public String fileUpload(@RequestParam("files")MultipartFile[] files){
        String filePath = "";
        // 多文件上传
        for (MultipartFile file : files){
            // 上传简单文件名
            String originalFilename = file.getOriginalFilename();
            // 存储路径
             filePath = new StringBuilder(fileRootPath)
                    .append(System.currentTimeMillis())
                    .append(originalFilename)
                    .toString();
            try {
                // 保存文件
                file.transferTo(new File(filePath));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return filePath;
    }
}

五启动类

/**
 * @Author lsc
 * @Description <p> </p>
 * @Date 2019/10/2 20:54
 * @Version 1.0
 */
@SpringBootApplication
public class FileUploadApplication {
    public static void main(String[] args) {
        SpringApplication.run(FileUploadApplication.class,args);
    }
}

六测试

发送http的post请求,使用表单形式,key为files需要与MultipartFile[] 的参数名称一致,挑选两个文件,发送成功后,会看到最后返回的文件路径;


打开保存的文件路径可以发现已经实现文件上传。

原文地址:https://www.cnblogs.com/zszxz/p/12089522.html

时间: 2024-08-30 05:55:46

springboot实现多文件上传的相关文章

Springboot如何启用文件上传功能

网上的文章在写 "springboot文件上传" 时,都让你加上模版引擎,我只想说,我用不上,加模版引擎,你是觉得我脑子坏了,还是觉得我拿不动刀了. springboot如何启用文件上传功能: 其实你啥都不用做,直接用就是了.文件上传相关的随着你的webStarter引入,就被引入到你的项目里面了. POM依赖: 代码: 注意事项: springboot文件上传 单个请求包含的文件默认大小:1MB-10MB 请求格式POST 参数格式form-data 如果出错了,请仔细查看报错信息!

springboot框架,文件上传问题===org.springframework.web.HttpMediaTypeNotSupportedException: Content type &#39;multipart/form-data;

使用IDEA开发springboot项目,需求中要提交数据项和文件上传,同时接收实体bean对象和文件,后台Controller接收配置方式: Controller代码如下: 1 @RequestMapping(value="/comment",method = RequestMethod.POST) 2 public @ResponseBody RetResult saveIndustryComment(HttpServletRequest request,@RequestParam

SpringBoot: 6.文件上传(转)

1.编写页面uploadFile.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>上传文件</title> </head> <body> <form action="uploadFile" method="post" enct

Eclipse搭建springboot项目(三)文件上传

知识点:SpringBoot2.x文件上传:HTML页面文件上传和后端处理 1.springboot文件上传 MultipartFile file,源自SpringMVC 1)静态页面直接访问:localhost:8080/index.html 注意点:如果想要直接访问html页面,则需要把html放在springboot默认加载的文件夹下面 2)MultipartFile 对象的transferTo方法,用于文件保存(效率和操作比原先用FileOutStream方便和高效) 访问路径 http

springboot+vue实现文件上传

https://blog.csdn.net/mqingo/article/details/84869841 技术: 后端:springboot 前端框架:vue 数据库:mysql pom.xml: <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.3</versi

Minio 整合springboot 开发 实现文件上传

Minio 作为对象存储,灵活方便,结合java 实现minio 文件上传 1.搭建maven环境,添加依赖包 <properties> <minio.version>4.0.0</minio.version></properties> <dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <versio

springboot最全文件上传步骤,详细,ajax文件上传,formdata封装

话不多说上代码 jsp页面: <form id="updateForm" enctype="multipart/form-data"> <input type="hidden" name="uId"> <div class="form-group"> <label class="control-label">商品名称:</label&

SpringBoot: 浅谈文件上传和访问的坑 (MultiPartFile)

本次的项目环境为 SpringBoot 2.0.4, JDK8.0. 服务器环境为CentOS7.0, Nginx的忘了版本. 前言 SpringBoot使用MultiPartFile接收来自表单的file文件,然后进行服务器的上传是一个项目最基本的需求,我以前的项目都是基于SpringMVC框架搭建的,所以在使用SpringBoot的时候进行MultiPartFile上传遇到了坑,这里说一下,其中主要包含两个坑点. 使用transferTo()方法写入File时找不到文件路径. 访问文件时Ng

springBoot的文件上传功能

知识点: 后台:将上传的图片写入指定服务器路径,保存起来,返回上传后的图片路径(在springBoot中,参考博客:http://blog.csdn.net/change_on/article/details/59529034) 前端:在Vue.js前端框架中,使用Vue_Core_Image_Upload插件,上传图片 (github地址:https://github.com/Vanthink-UED/vue-core-image-upload) 后台: 1)在Controller中写一个方法