Springboot框架中request.getInputStream()获取不到上传的文件流
Springboot框架中用下面的代码,使用request.getInputStream()获取不到上传的文件流
@PostMapping("/upload_img") public void uploadImage(HttpServletRequest request) { int len = request.getInputStream().available(); // len 值为:0,所以无法获取文件流 }
改用下面的代码(@RequestParam的参数就是上传文件的参数名):
@PostMapping("/upload_img") public void uploadImage(@RequestParam("file1") MultipartFile file) { InputStream fis = file.getInputStream(); //fis 既是上传的文件流 }
或使用下面的代码:
@PostMapping("/upload_img") public void uploadImage(HttpServletRequest request) { MultipartHttpServletRequest params=((MultipartHttpServletRequest) request); MultipartFile file = ((MultipartHttpServletRequest) request).getFiles("file1").get(0); InputStream fis = file.getInputStream(); // fis 既是上传的文件流 }