将文件上传到Spring Boot应用程序时出错:“由于未提供多部件配置,因此无法处理部件”

时间:2018-06-23 06:54:44

标签: spring-boot file-upload multipart

这是我要用来读取 .ods 文件的上载控制器。我正在通过Java Workbook逐一读取每个单元格。

@RestController
public class UploadController {
    @ApiOperation(value = "Upload mappings excel file", notes = "Uploads the excel file and stores on couchbase")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "File uploaded successfully"),
            @ApiResponse(code = 500, message = "Internal server error"),
            @ApiResponse(code = 400, message = "Bad request")
    })

    @RequestMapping(value = "/upload", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE, consumes = "multipart/form-data")
    public void uploadMappingsExcel(
            @ApiParam(value = "file", example = "user.ods", required = true)
            @RequestParam(value = "file", required = false) MultipartFile multipartFile) throws IOException{
        Path  path = null;
        for(MultipartFile file : Collections.singleton(multipartFile)) {
            path = Paths.get(file.getOriginalFilename());
            Files.write(path,file.getBytes());
        }
        FileInputStream file = new FileInputStream(path.toString());
        XSSFWorkbook workbook = new XSSFWorkbook(file);

        Sheet sheet = workbook.getSheetAt(0);
        DataFormatter dataFormatter = new DataFormatter();
        Iterator<Row> rowIterator = sheet.iterator();

        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                String cellValue = dataFormatter.formatCellValue(cell);
                System.out.print(cellValue + "\t");
            }
            System.out.println();

        }

    }

我正在尝试通过邮递员上传 .ods 文件。

0 个答案:

没有答案