SpringCloudGateway网关服务实现文件上传功能

在这里插入图片描述

说明

Gateway网关服务本想实现前后端的文件上传及下载功能,但是在实际开发过程中屡屡产生报错,导致一直报错“400 bad request: Required request part ‘file’ is not present”后端无法解析接收到文件数据,从而导致无法实现前端文件上传及后端接收解析过程,本文就是为了记录成功案例,以及描述本人尝试其他方案的感受,便于其他人吸取经验,排雷。

SpringBoot和SpringCloudGateway项目区别说明

Spring Boot是一套基于Spring框架的微服务框架。
SpringCloudGateway基于webFlux框架实现的
框架不同就导致,之前网上传统的那套controller层方法就不适用,现在分别举例说明

1.SpringBoot的成功案例文件上传代码

pom

<!--文件上传-->
 <dependency>
     <groupId>commons-fileupload</groupId>
     <artifactId>commons-fileupload</artifactId>
     <version>1.3.3</version>
 </dependency>

前端代吗

<p>单文件上传并保存</p>
<form method="post" action="/excel/uploadExcel" enctype="multipart/form-data">
    <p><input type="file" name="file00"></p>
    <p><span th:if="${msg}"></span></p>
    <input type="submit" value="提交">
</form>

Controller代码 重点在:@RequestParam(“file00”) MultipartFile file

@PostMapping(value = "/uploadExcel")
    public String uploadExcel(@RequestParam("file00") MultipartFile file, Model model) throws IOException {
        try {
            if(file.isEmpty()){
                model.addAttribute("msg","上传失败,请选择文件!");
                return "excelIndex";
            }
            String filename = file.getOriginalFilename();
            //filePath获取的是编译后的路径,而不是项目看到的路径,filePath=/E:/WorkSpace/demo/target/classes/
            String filePath = ResourceUtils.getURL("classpath:").getPath()+"static/oneFile/";
            //避免文件重复覆盖
            String uuid= UUID.randomUUID().toString().replaceAll("-", "");
            //时间戳分类文件
            String time = new SimpleDateFormat("YYYY-MM").format(new Date());
            String realPath = filePath + time + "/" + uuid + "-" + filename;
            System.out.println("realPath:" + realPath);
            //最后保存的路径在这里:target/classes/static/oneFile/2022-02/548881060e3d417a91d87b0a10959077-sop.sql
            File dest = new File(realPath);
            //检测是否存在目录,无,则创建
            if(!dest.getParentFile().exists()){
                dest.getParentFile().mkdirs();//新建文件夹 多级目录
            }
            file.transferTo(dest);//文件写入
        } catch (IOException e) {
            e.printStackTrace();
        }
        model.addAttribute("msg","文件上传成功!");
        return "hello";
    }

2.SpringCloudGateway的成功案例文件上传代码

Controller代码

重点区别在:

1.注解中配置consumes = MediaType.MULTIPART_FORM_DATA_VALUE
2.形参采用@RequestPart(“file”) FilePart filePart,而不是传统的@RequestParam(“file00”) MultipartFile file,这是他两的区别

备注说明:

1.使用RequestPart来接收,得到的是FilePart
2.FilePart的content是Flux,可以使用DataBufferUtils写到文件或者直接使用transferTo写入到文件
详情可查看该文章了解 ->: SPRING WEBFLUX 前后端分离 文件上传

import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.MediaType;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.io.IOException;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  public Mono<String> requestBodyFlux(@RequestPart("file") FilePart filePart) throws IOException {
      System.out.println(filePart.filename());
      Path tempFile = Files.createFile(Paths.get("D:\\tmp\\"+filePart.filename()));

		//方法一
      AsynchronousFileChannel channel =
              AsynchronousFileChannel.open(tempFile, StandardOpenOption.WRITE);
      DataBufferUtils.write(filePart.content(), channel, 0)
              .doOnComplete(() -> {
                  System.out.println("finish");
              })
              .subscribe();

		//方法二
		//filePart.transferTo(tempFile.toFile());

      System.out.println(tempFile.toString());
      return Mono.just(filePart.filename());
  }

网上其他方案

说明:
1.针对前端代码格式书写错误导致
2.针对后端代码编写Filter过滤器文件进行解析
3.注入Bean方式 或者xml配置xxResolver解析器进行文件解析
下面方法直接拷贝可能会报错,里面某些类没有标注引用,会报错找不到或者调用还是报 “400 bad request: Required request part ‘file’ is not present”

其他方案1:配置filter

Spring-Cloud-Gateway获取multipart/form-data时无法正常获取

其他方案2:配置filter

SpringCloud-Gateway对multipart/form-data等其他POST请求类型的body体进行多次打开

其他方案3:注入Bean或者xml配置xxResolver解析器进行文件解析

Spring Cloud Gateway 之获取请求体(Request Body)的几种方式

其他方案4:既然MultipartFile后端接收不到,那就采用把上传文件进行Base64编码,通过json格式传给后台。

GATEWAY网关上传文件问题

其他方案5:修改前端vue

Vue上传通过“服务端签名后直传”上传文件到阿里云 报错 400 Bad Request

其他方案6:修改前端vue

vue put 提交 400 Bad Request(有时候可以提交成功)

其他方案7:xml配置xxResolver解析器进行文件解析

springMVC 文件上传 HTTP Status 400 – Bad Request

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘大猫.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值