最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Receiving files using Java23 and spring-webflux - Stack Overflow

programmeradmin2浏览0评论

I'm implementing a function which receives an excel file and parses its content then writes it into postgresql.

Considering requirment for a good througput for handing request, I decided using spring-webflux in springboot and coding in its reactive fashion.

Here is my code:

@PostMapping(value = "/import/excel")
    public Mono<ApiResponse<ImportResult>> importExcel(@RequestPart("file") Mono<MultipartFile> file,
                                                       @RequestPart("modelName") String modelName) {
        return file.flatMap(excel -> {
            BatchUtils.excelValidate(excel);
            try (InputStream inputStream = excel.getInputStream()) {
                Class<?> excelHeaderClass = ExcelModel.classOf(modelName);
                GenericExcelDataListener<?> excelDataListener = GenericExcelDataListener.build(excelHeaderClass, excelDataService);
                // do read excel using excelDataListener
                FastExcel.read(inputStream, excelHeaderClass, excelDataListener).sheet().doRead();
                return Mono.just(ApiResponse.success());
            } catch (IOException | ExcelModelNotFoundException e) {
                return Mono.error(e);
            }

        });
    }

But when I test it using command:

curl --location --request POST 'http://localhost:8080/path/to/import/excel' \
--header 'User-Agent: Apifox/1.0.0 ()' \
--header 'Accept: */*' \
--header 'Host: localhost:8080' \
--header 'Connection: keep-alive' \
--header 'Content-Type: multipart/form-data; boundary=--------------------------675960818497163398905741' \
--form 'modelName="someName"' \
--form 'file=@"/path/to/file/upload-file.xlsx"'

It got error message like:

{
    ... omitted.
    "status": 415,
    "error": "Unsupported Media Type",
    "message": "Content type 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' not supported for bodyType=.springframework.web.multipart.MultipartFile"
}

I have asked AIs for help but the approches given just didn't work at all.

P.S spring-webflux official doc .html doesn't tell much of what I should do in my scenario and it really has no necessity to save the uploaded file first for currently those files would be very small.

If anyone has idea about this question, I'd be thankful.

发布评论

评论列表(0)

  1. 暂无评论