如何解决错误 "Current Request is not a multipart request"

How to solve error "Current Request is not a multipart request"

我在使用 Postman 将 Excel 文件上传到 Spring 启动应用程序时遇到问题。我不断收到错误 “当前请求不是多部分请求”

我已经尝试了关于删除 content-type headers 和选择 form-data 的其他解决方案,但目前没有任何效果。

这是我的控制器代码。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Set;

@CrossOrigin
@Controller
@RequestMapping("/player")
public class PlayerController {

    @Autowired
    PlayerService playerService;

    @Autowired
    TeamService teamService;


    @PostMapping("/upload/{teamId}")
    public ResponseEntity<ResponseMessage> uploadFile(@RequestPart("file") MultipartFile file, @PathVariable int teamId) {
        String message;
        if (ExcelHelper.hasExcelFormat(file)) {
            try {
                playerService.save(file, teamId);

                message = "Uploaded the file successfully: " + file.getOriginalFilename();
                return ResponseEntity.status(HttpStatus.OK).body(new ResponseMessage(message));
            } catch (Exception e) {
                message = "Could not upload the file: " + file.getOriginalFilename() + "!";
                return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseMessage(message));
            }
        }
        message = "Please upload an excel file!";
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseMessage(message));
    }
  }

这是 curl 请求:

curl --location --request POST 'http://localhost:9090/player/upload/5' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJzcmlrYXJrIiwiZXhwIjoxNjM1NzEzMDAwLCJpYXQiOjE2MzU2MjY2MDB9._GcCznGiiWE4fRRkaRfhc7El9ETEOhbzL6ErhPsU_aY' \
--form '=@"/C:/Users/srika/Documents/Git_Repos/abc-100-abc-xyz-00-Team/resources/PlayerList.xlsx"'

以下是邮递员错误信息:

这些是 headers 邮递员发送的:

在application.properties中,我定义了以下细节。

spring.servlet.multipart.max-file-size=2MB
spring.servlet.multipart.max-request-size=2MB
spring.http.multipart.enabled=false

删除 auto-generated Content-Type Header 并添加这个新的 header 是可行的。

Content-Type:multipart/form-data; boundary=something

另外,将控制器 class 中的 RequestMapping 注释更新为

@RequestMapping(value = "/upload/{teamId}",
            method = RequestMethod.POST,
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

enctype="multipart/form-data" 添加到您的表单标签。

这是我的html

<form role="form" enctype="multipart/form-data" method="post" th:action="@{/upload_files}">
    <div class="form-group">
        <label for="exampleInputEmail1">Email address</label>
        <input type="email" name="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
    </div>
    <div class="form-group">
        <label for="exampleInputPassword1">Username</label>
        <input type="text" name="username" class="form-control" id="exampleInputPassword1" placeholder="Username">
    </div>
    <div class="form-group">
        <label for="exampleInputFile">Single File</label>
        <input type="file" name="sfile" id="exampleInputFile">
    </div>
    <div class="form-group">
        <label for="files">Multiple File</label>
        <input type="file" name="files" id="files" multiple>
    </div>
    single choose check box
    <div class="checkbox">
        <label>
            <input type="checkbox" name="check" value="yes"> Check me out
        </label>
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
</form>

这是我的控制器

    // 表单添加 enctype="multipart/form-data"
    @PostMapping(value = "/upload_files")
    public String when_upload(@RequestParam("email") String email,
                              @RequestParam("username") String username,
                              @RequestPart("sfile") MultipartFile file,
                              @RequestPart("files") MultipartFile[] files) {

        log.info("{}, {}, {}, {}", email, username, file.getOriginalFilename(), files.length);

        return "index";
    }