关于Spring Boot上传文件大小的问题

Questions about the size of the Springboot upload file

上传大于设定值的文件时出错。我想要 捕获此异常并将其 return 发送到 browser.The e.getCause().getMessage() 有一个值,但没有成功 return 浏览器。通常,浏览器上会显示一段json。 handleFileFormatException没问题,但是handleIllegalStateException无法显示任何信息出现在browser.The路径上的"Unable to access this site"都显示:localhost:8080。这两个方法几乎一样,不同的是FileFormatExceptio是我定义的异常class,而IllegalStateException不是。为什么它没有 return json 实体到浏览器。我不知道do.Who有什么可以帮助我的?谢谢!

application.properties:

@ExceptionHandler

异常响应

public class ExceptionResponse {

private String message;
private Integer code;


public ExceptionResponse(Integer code, String message){
    this.message = message;
    this.code = code;
}

public static ExceptionResponse create(Integer code, String message){
    return new ExceptionResponse(code, message);
}

public Integer getCode() {
    return code;
}
public String getMessage() {
    return message;
}

}

控制台警告,没有错误

 2017-07-31 17:10:50.388  WARN 10940 --- [nio-8080-exec-4] .m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (42990730) exceeds the configured maximum (10485760)

浏览器按F12

控制器

@Controller
public class FileUploadController {

    private final StorageService storageService;

    @Autowired
    public FileUploadController(StorageService storageService) {
        this.storageService = storageService;
    }


   @GetMapping("/")
    public String listUploadedFiles(Model model) throws IOException {

        model.addAttribute("files", storageService
                .loadAll()
                .map(path ->
                        MvcUriComponentsBuilder
                                .fromMethodName(FileUploadController.class, "serveFile", path.getFileName().toString())
                                .build().toString())
                .collect(Collectors.toList()));

        return "index";
    }

    @GetMapping("/files/{filename:.+}")
    @ResponseBody
    public ResponseEntity<Resource> serveFile(@PathVariable String filename) {

        Resource file = storageService.loadAsResource(filename);
        return ResponseEntity
                .ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"")
                .body(file);
    }

    @PostMapping("/")
    public String handleFileUpload(@RequestParam("file") MultipartFile file,
                                   RedirectAttributes redirectAttributes){
        storageService.store(file);
        redirectAttributes.addFlashAttribute("message",
                "upload success! " + file.getOriginalFilename() + "!");
        return "redirect:/";
    }

getStatus

private HttpStatus getStatus(HttpServletRequest request) {
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        if (statusCode == null) {
            return HttpStatus.INTERNAL_SERVER_ERROR;
        }
        return HttpStatus.valueOf(statusCode);
    }

您遗漏了在异常处理程序处理异常时设置 http 响应状态代码的部分。

  • @ResponseStatus(value= HttpStatus.BAD_REQUEST) 添加到您的方法中。

  • HttpServletResponse response 添加到方法参数列表并在返回对象之前调用 response.setStatus(HttpStatus.BAD_REQUEST.value());