Reputation: 350
I have an issue with using Postman to upload an Excel file to a Spring Boot application. I'm constantly getting the error "Current Request is not a multipart request".
I've tried other solutions explained on removing content-type headers and selecting form-data, but nothing worked as of now.
This is my controller code.
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));
}
}
This is the curl request:
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"'
Below is the postman error message:
These are the headers postman is sending:
In application.properties, I have defined following details.
spring.servlet.multipart.max-file-size=2MB
spring.servlet.multipart.max-request-size=2MB
spring.http.multipart.enabled=false
Upvotes: 3
Views: 13510
Reputation: 9
enctype="multipart/form-data"
to your form tag.<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";
}
Upvotes: 0
Reputation: 350
It worked up-on removing the auto-generated Content-Type Header and adding this new header instead.
Content-Type:multipart/form-data; boundary=something
Also, updated the RequestMapping Annotation in the controller class to
@RequestMapping(value = "/upload/{teamId}",
method = RequestMethod.POST,
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
Upvotes: 5