SKG*_*SKG 2 java spring multipartform-data spring-boot postman
我在使用 Postman 将 Excel 文件上传到 Spring Boot 应用程序时遇到问题。我不断收到错误“当前请求不是多部分请求”。
我已经尝试了有关删除内容类型标头和选择表单数据的其他解决方案,但到目前为止没有任何效果。
这是我的控制器代码。
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));
}
}
Run Code Online (Sandbox Code Playgroud)
这是卷曲请求:
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"'
Run Code Online (Sandbox Code Playgroud)
以下是邮递员错误消息:
这些是邮递员发送的标头:
在 application.properties 中,我定义了以下详细信息。
spring.servlet.multipart.max-file-size=2MB
spring.servlet.multipart.max-request-size=2MB
spring.http.multipart.enabled=false
Run Code Online (Sandbox Code Playgroud)
它致力于删除自动生成的 Content-Type 标头并添加此新标头。
内容类型:多部分/表单数据;边界=某物
此外,还将控制器类中的 RequestMapping 注解更新为
@RequestMapping(value = "/upload/{teamId}",
method = RequestMethod.POST,
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9794 次 |
| 最近记录: |