Tomcat:所需的请求部分“文件”不存在

时间:2018-10-30 12:25:08

标签: java spring-boot file-upload multipartform-data multipart

我正在使用Spring Boot测试上传功能,并且出现'Required MultipartFile parameter'file'not present'错误。 部署外部tomcat服务器时。 但它可以与Spring Boot Tomcat插件一起正常使用 以下是1)JSP,2)控制器3)Config属性

1)

<form id="initialUploadForm" action="${root}/upload/uploadCapFile" enctype="multipart/form-data" method="post">
                <table align="left" width="50%" cellspacing="0" cellpadding="5"
                       border="0" class="formTable">
                    <tr>
                        <td><label class="">Add Cap File : </label></td>
                        <td><input type="file" name="file" style="width:100%;"/></td>
                    </tr>
                    <tr>
                        <td colspan="2">&nbsp;</td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td><input class="btn btn-success" type="submit"
                                   value="Upload Data" id="btnInitialUpload"/></td>
                    </tr>
                    <tr>
                        <td colspan="2">&nbsp;</td>
                    </tr>
                </table>
</form>

2)

@RequestMapping(value = "uploadCapFile", method = RequestMethod.POST)
        ModelAndView uploadCapFileData(@RequestParam("file") MultipartFile file, Model model) {
            try {
                if (log.isDebugEnabled()) log.debug("UploadController  calling : ");

                System.out.println("File Name : " + file.getOriginalFilename());
                long lStartTime = new Date().getTime();
                    if (!file.isEmpty()) {
                        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
                        if (extension.equals(Constants.FILE_FORMAT)) {
                            File convFile = convertFile(file);
                            InputStream in = new FileInputStream(convFile);
                            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                            StringBuilder out = new StringBuilder();
                            String line;
                            while ((line = reader.readLine()) != null) {
                                out.append(line);
                            }
                            if (isProdUpdate == 1) {
                                hotelService.updateAgentMarkup(out.toString());
                            } else {
                                hotelService.updateHotelData(out.toString());
                            }
                        } else {
                            log.error("Invalid File Format ! File Format is :" + Constants.FILE_FORMAT);
                        }
                    } else {
                        log.error("File is missing ! Can not be Process");
                    }


                long lEndTime = new Date().getTime();
                long output = TimeUnit.MILLISECONDS.toSeconds(lEndTime - lStartTime);
                processingTime = String.valueOf(output);
            } catch (IOException e) {
                log.error("Error occurred while calling the UploadController : " + e);
            } catch (TalcacheException e) {
                log.error("Error occurred while calling the UploadController  : " + e.getErrorMsg());
            } catch (Exception e) {
                log.error("Error occurred while calling the UploadController : " + e);
            }
    }

3)

 spring.servlet.multipart.enabled=true
 spring.servlet.multipart.file-size-threshold=5KB
 spring.servlet.multipart.max-file-size=200MB
 spring.servlet.multipart.max-request-size=215MB

2 个答案:

答案 0 :(得分:1)

将此代码放入配置文件

@Bean
public CommonsMultipartResolver multipartResolver() {
  CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
  multipartResolver.setMaxUploadSize(-1);
  return multipartResolver;
        }

答案 1 :(得分:0)

尝试在方法签名中添加multipart/form-data

@RequestMapping(value = "uploadCapFile", method = RequestMethod.POST,headers = "content-type=multipart/form-data")