猿问

spring mvc上传文件超过设定大小异常处理客户端无响应

spring mvc上传文件超过设定大小异常处理客户端无响应;


controller类


    @RequestMapping(path = "/file", method = RequestMethod.POST)

    public UploadMsg uploadFile(@RequestParam("uploadFile") MultipartFile file, String userCode) throws Exception {

        String fileId = genFileId();

        saveFileToSystem(file, fileId);

        UploadMsg uploadMsg = getUploadMsg(userCode, fileId, file.getSize());

        return uploadMsg;

    }

    

    @ExceptionHandler(MaxUploadSizeExceededException.class)

    public ResponseEntity<String> handleException(MaxUploadSizeExceededException ex) {

        System.out.println("=====================" + ex.getClass().getName());

        return ResponseEntity.ok("ok");

    }

spring配置文件配置文件大小不能超过1M


    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

        <property name="defaultEncoding" value="UTF-8"/>

        <property name="maxUploadSize" value="#{1*1024*1024}"/>

        <property name="resolveLazily" value="true"/>

    </bean>

下面是我用小于1M的文件访问时的情况

这是上传失败的情况
https://img1.mukewang.com/5cbd7cd60001241908000294.jpg

下面是我的日志打印情况

22:40:28,569 DEBUG http-apr-8080-exec-5 support.DefaultListableBeanFactory:251 - Returning cached instance of singleton bean 'uploadFileController'

=====================org.springframework.web.multipart.MaxUploadSizeExceededException

22:40:28,574  WARN http-apr-8080-exec-5 commons.CommonsMultipartResolver:194 - Failed to perform multipart cleanup for servlet request

org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size of 1048576 bytes exceeded; nested exception is org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (6145455) exceeds the configured maximum (1048576)

 

这句是我在@ExceptionHandler(MaxUploadSizeExceededException.class)注解的方法中打印的,我也在这个方法中作了响应处理,但是客户端访问的时候就会显示无响应,不知道是什么鬼,求大神解答!!!!


函数式编程
浏览 863回答 3
3回答

翻过高山走不出你

从这里找到了答案关于spring mvc MaxUploadSizeExceededException 死循环解决方据说是tomcat的bug,我试了下用jetty运行,果然是没有问题的,至于低版本的tomcat还没测试。

四季花海

@ExceptionHandler(MaxUploadSizeExceededException.class)@ResponseBodypublic ResponseEntity<String> handleException(MaxUploadSizeExceededException ex) {&nbsp; &nbsp; System.out.println("=====================" + ex.getClass().getName());&nbsp; &nbsp; return ResponseEntity.ok("ok");}看看是不是少了个@ResponseBody。

紫衣仙女

weblogic也有这个问题,图片上传超时了,客户端status还是pending,没有响应,已经写了@ExceptionHandler和@ResponseBody,返回一个map对象,但是客户端没有响应,怀疑weblogic没做响应处理
随时随地看视频慕课网APP

相关分类

Java
我要回答