有关Springboot上载文件大小的问题

时间:2017-07-31 07:27:54

标签: java spring-mvc exception spring-boot exceptionhandler

当我上传文件大小超过设定值时发生错误。我想要 捕获此异常并将其返回到浏览器 handleIllegalStateException有一个值,但没有成功返回 浏览器。通常,浏览器上会显示一条json。 FileFormatExceptio没问题,但IllegalStateException无法显示任何消息,并且在浏览器上显示“无法访问此站点”。路径全部显示为:localhost:8080。这两种方法几乎相同,区别在于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) 不是。为什么不将json实体返回给浏览器。我不知道该怎么办。谁能帮帮我?谢谢!

application.properties: application.properties

@ExceptionHandler @ExceptionHandler

ExceptionResponse

@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:/";
    }

}

控制台警告,无错误

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);
    }

浏览器按F12 enter image description here

enter image description here

控制器

 Where Date(Order_Created_at) >= ''' + convert(varchar(10), @strdate, 112) + ''' AND

的getStatus

public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static String TAG = "PUSH";

@Override
public void onCreate() {
    super.onCreate();
    Log.e("PUSH","FCM Service started");
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
   Log.e("PUSH","Push received");
   UnityPlayer.UnitySendMessage("Canvas", "PushReceived", "This is your push message");
}
}

1 个答案:

答案 0 :(得分:0)

当您的异常处理程序处理异常时,您缺少设置http响应状态代码的部分。

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

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