再谈SpringMvc文件上传异常处理

 
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public void handleException(MaxUploadSizeExceededException ex, HttpServletResponse response) throws Exception {
        StringBuilder error = new StringBuilder();
        if (ex instanceof MaxUploadSizeExceededException) {
            MaxUploadSizeExceededException ms = (MaxUploadSizeExceededException) ex;
            SizeLimitExceededException cause = (SizeLimitExceededException) ms.getCause();
            long maxUploadSize = ms.getMaxUploadSize();
            String actualSize = String.valueOf(cause.getActualSize());
            double parseDouble = Double.parseDouble(actualSize) / 1024 / 1024;
            BigDecimal b = new BigDecimal(parseDouble);
            double d = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            error.append("最大上传文件为:" + maxUploadSize / 1024 / 1024).append("M;");
            error.append("实际文件大小为:").append(d).append("M");
            System.out.println(error.toString());
        }

        error.append("上传文件出错");
        System.out.println(error.toString());
    }

 

 XML配置

  <!-- 支持上传文件 -->
  <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
          <property name="resolveLazily" value="true" />
        <!--最大上为2M -->
        <property name="maxUploadSize" value="5242880" />
   </bean>

 

微信公众号

                          
posted @ 2020-01-13 15:03  盲目的拾荒者  阅读(330)  评论(0编辑  收藏  举报