Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 使用AJAX时Spring文件上载过大错误_Spring Boot - Fatal编程技术网

Spring boot 使用AJAX时Spring文件上载过大错误

Spring boot 使用AJAX时Spring文件上载过大错误,spring-boot,Spring Boot,我正在通过jQueryAjax进行一些文件上传,因此希望在任何情况下都能得到适当的响应。我现在正在努力解决我在应用程序中设置的大小限制。属性: spring.servlet.multipart.max-file-size=5MB spring.servlet.multipart.max-request-size=5MB spring.http.multipart.enabled=false 我找到了很多教程,甚至一些答案,但似乎都不适合我。这里有两个例子: 我总是得到一个空响应,在日志中我

我正在通过jQueryAjax进行一些文件上传,因此希望在任何情况下都能得到适当的响应。我现在正在努力解决我在应用程序中设置的大小限制。属性:

spring.servlet.multipart.max-file-size=5MB
spring.servlet.multipart.max-request-size=5MB
spring.http.multipart.enabled=false
我找到了很多教程,甚至一些答案,但似乎都不适合我。这里有两个例子:

我总是得到一个空响应,在日志中我看到另一个异常(错误来自我的代码,以查看我的处理程序是否起作用:

2018-10-19 15:57:54.183 ERROR 12940 --- [http-nio-9002-exec-2] c.i.p.m.mvc.RestExceptionHandler         : CAUSE: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (12698493) exceeds the configured maximum (5242880)
2018-10-19 15:57:54.185  WARN 12940 --- [http-nio-9002-exec-2] .m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (12698493) exceeds the configured maximum (5242880)
这是我的代码,注释掉的部分是我尝试过的一些变体

@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(RestExceptionHandler.class);

    @ExceptionHandler(MultipartException.class)
    //@ExceptionHandler(MaxUploadSizeExceededException.class)
    @ResponseBody
    public String handleTooLargeFiles(HttpServletRequest request, Throwable ex) {
    //public ResponseEntity<?> handleTooLargeFiles(HttpServletRequest request, Throwable ex) {
        LOGGER.error("CAUSE: " + ex.getCause().getMessage());
        return "File size exceeds the allowable limit! (5MB)";
        //return new ResponseEntity("File size exceeds the allowable limit! (5MB)", HttpStatus.PAYLOAD_TOO_LARGE);
    }

}
@ControllerAdvice
公共类RestExceptionHandler扩展了ResponseEntityExceptionHandler{
私有静态最终记录器Logger=LoggerFactory.getLogger(RestExceptionHandler.class);
@ExceptionHandler(MultipartException.class)
//@ExceptionHandler(MaxUploadSizeExceedeException.class)
@应答器
公共字符串HandletToolArgeFiles(HttpServletRequest请求,Throwable ex){
//公共响应HandletToolArgeFiles(HttpServletRequest请求,Throwable ex){
LOGGER.error(“原因:+ex.getCause().getMessage());
返回“文件大小超过允许的限制!(5MB)”;
//返回新的响应属性(“文件大小超过允许的限制!(5MB)”,HttpStatus.PAYLOAD\u过大);
}
}

我做错了什么?

您还必须为嵌入式Tomcat设置属性,以避免过早终止您的请求:

# By default they're both "2MB"

server.tomcat.max-swallow-size=-1
server.tomcat.max-http-form-post-size=-1

(或者选择一些较大的大小,如
20MB
,而不是
-1

您的控制器建议很好,并返回“文件大小超出允许的限制!(5MB)”,正如我检查的那样。您可以通过调用rest客户端(post-man或Advance rest客户端)进行检查吗客户端接收时可能会出现问题,因为您的日志中说它已通过controller advise,所以我相信客户端在处理字符串作为响应时会出现问题。@kj007似乎是在兜圈子,我确定我尝试过邮递员,也没有得到正确的响应,但现在我得到了响应。尽管当我尝试使用ajax,我仍然看不到任何响应,但是这个错误:net::ERR\u CONNECTION\u reseth然后您的ajax请求似乎没有到达您的spring boot服务器您可以在控制器上进行调试,然后尝试从ajax发送一个小的大小,然后是5 mb,并检查它是否到达controller@kj007小规模的工作,但现在我看到了一些奇怪的事情,我觉得在服务器上进行了一段时间的oop,这是我在一篇ajax文章之后得到的(省略了实际的消息,但与上面相同):2018-10-19 16:59:11.155 WARN 17100 2018-10-19 16:59:12.154 WARN 17100 2018-10-19 16:59:13.154 WARN 17100 2018-10-19 16:59:14.154 WARN 17100 2018-10-19 16:59:15.156 WARN 17100 2018-10-19 16:59:16.155 WARN 17100谢谢你的回答,但我的案例中没有tomcat。只有带嵌入式网络的普通弹簧靴server@Thomas…和那个嵌入式服务器是。