我有一个简单的下游上传文件服务。示例代码
@RestController
@RequestMapping("/file")
public class FileController {
@PostMapping("/upload")
public ResponseEntity<?> uploadFile(@RequestParam("file") MultipartFile file,
@RequestParam(value = "delay", required = false, defaultValue = "0") int delay) throws Exception {
System.out.println(String.join(System.getProperty("line.separator"),
"File Name => " + file.getOriginalFilename(),
"File Size => " + file.getSize() + "bytes",
"File Content Type => " + file.getContentType()));
TimeUnit.MILLISECONDS.sleep(delay);
return ResponseEntity.ok(file.getName() + " uploaded");
}
}
,如果有CustomExceptionHandler
,则返回BAD_REQUEST
的{{1}}:
MultipartException
application.yml的大小限制为10MB:
@Configuration
@ControllerAdvice
public class CustomExceptionHandler {
@ExceptionHandler(MultipartException.class)
public ResponseEntity<String> handleMultipartException(MultipartException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage());
}
}
如果我上传了一个大文件,它会为我提供spring:
servlet:
multipart:
max-file-size: 10MB
max-request-size: 10MB
状态
当我尝试通过 spring cloud gateway 命中相同的结果时,会得到以下结果:
,日志显示如下:
400
请注意,该网关配置为采用大容量文件,并且全局设置2019-11-08 00:36:10.797 ERROR 21904 --- [ctor-http-nio-2] a.w.r.e.AbstractErrorWebExceptionHandler : [86e57f7e] 500 Server Error for HTTP POST "/product-service/file/upload"
reactor.netty.http.client.PrematureCloseException: Connection has been closed BEFORE response, while sending request body
过滤器以占用超过10MB的空间。
如何获得与下游服务相同的响应代码?
此外,我检查了传统的Zuul,也遇到了RequestSize
错误。
示例代码spring-cloud-gateway / product-service / eureka-https://github.com/dhananjay12/spring-cloud/tree/master/spring-routing
答案 0 :(得分:0)
您是否可以尝试直接通过文件大小的非限制限制,而不进行限制?为属性尝试值-1:
您要在其中上传文件的MS的属性文件
public triggerSnapshot(): void {
this.trigger.next();
this.imageservice.imageid= this.webcamImage.imageAsDataUrl;
}
public toggleWebcam(): void {
this.showWebcam = !this.showWebcam;
}
public handleInitError(error: WebcamInitError): void {
this.errors.push(error);
}
public showNextWebcam(directionOrDeviceId: boolean|string): void {
this.nextWebcam.next(directionOrDeviceId);
}
public handleImage(webcamImage: WebcamImage): void {
console.info('received webcam image', webcamImage);
this.webcamImage = webcamImage;
}
public cameraWasSwitched(deviceId: string): void {
this.deviceId = deviceId;
}
如果良好,则可能会导致zuul代理的功能区套接字大小出现问题,有针对这种情况的属性通知,如下所示:
getway的属性文件:
spring.servlet.multipart.max-file-size =-1
spring.servlet.multipart.max-request-size =-1