MultipartException: Current request is not a multipart request - java

I am trying to make a restful controller to upload files. I have seen this
and made this controller:
#RestController
public class MaterialController {
#RequestMapping(value="/upload", method= RequestMethod.POST)
public String handleFileUpload(
#RequestParam("file") MultipartFile file){
String name = "test11";
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));
stream.write(bytes);
stream.close();
return "You successfully uploaded " + name + " into " + name + "-uploaded !";
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name + " because the file was empty.";
}
}
}
and then i used postman to send a pdf:
But the server crashes with the error:
.MultipartException: Current request is not a multipart request
Again i have found this, and added a bean.xml file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
</beans>
Unfortunately, it still complains with the same error.

When you are using Postman for multipart request then don't specify a custom Content-Type in Header. So your Header tab in Postman should be empty. Postman will determine form-data boundary. In Body tab of Postman you should select form-data and select file type. You can find related discussion at https://github.com/postmanlabs/postman-app-support/issues/576

It looks like the problem is request to server is not a multi-part request. Basically you need to modify your client-side form. For example:
<form action="..." method="post" enctype="multipart/form-data">
<input type="file" name="file" />
</form>
Hope this helps.

I was also facing the same issue with Postman for multipart. I fixed it by doing the following steps:
Do not select Content-Type in the Headers section.
In Body tab of Postman you should select form-data and select file type.
It worked for me.

That happened once to me: I had a perfectly working Postman configuration, but then, without changing anything, even though I didn't inform the Content-Type manually on Postman, it stopped working; following the answers to this question, I tried both disabling the header and letting Postman add it automatically, but neither options worked.
I ended up solving it by going to the Body tab, change the param type from File to Text, then back to File and then re-selecting the file to send; somehow, this made it work again. Smells like a Postman bug, in that specific case, maybe?

In application.properties, please add this:
spring.servlet.multipart.max-file-size=128KB
spring.servlet.multipart.max-request-size=128KB
spring.http.multipart.enabled=false
and in your html form, you need an : enctype="multipart/form-data".
For example:
<form method="POST" enctype="multipart/form-data" action="/">
Hope this help!

Check the file which you have selected in the request.
For me i was getting the error because the file was not present in the system, as i have imported the request from some other machine.

In my case, I removed:
'Content-Type': 'application/json',
from my Interceptor, and everything works.
intercept(httpRequest: HttpRequest<any>, httpHandler: HttpHandler): Observable<HttpEvent<any>> {
if (this.authService.isUserLoggedIn() && httpRequest.url.indexOf('login') === -1) {
const authReq = httpRequest.clone({
headers: new HttpHeaders({
'Content-Type': 'application/json',
Authorization: this.authService.getBasicAuth()
})
});
return httpHandler.handle(authReq);
} else {
return httpHandler.handle(httpRequest);
}}

You need to add consumes = {MULTIPART_FORM_DATA_VALUE} to your mapping. Full example :
#PostMapping(path = "/{idDocument}/attachments", consumes = {MULTIPART_FORM_DATA_VALUE})
ResponseEntity<String> addAttachmentsToDocumentForm(#PathVariable Long idDocument, #RequestParam("file") MultipartFile[] files){
documentService.addAttachments(idDocument, files);
return ok("your response");
}

#PostMapping("/upload")
public ResponseEntity<ResponseMessage> uploadFile(#RequestParam("file") MultipartFile uploadFile) {
String message = "";
try {
service.save(uploadFile);
message = "Uploaded the file successfully: " + uploadFile.getOriginalFilename();
return ResponseEntity.status(HttpStatus.OK).body(new ResponseMessage(message));
} catch (Exception e) {
message = "Could not upload the file: " + uploadFile.getOriginalFilename() + "!";
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseMessage(message));
}
}

in ARC (advanced rest client) - specify as below to make it work
Content-Type multipart/form-data (this is header name and header value)
this allows you to add form data as key and values
you can specify you field name now as per your REST specification and select your file to upload from file selector.

i was facing the same issue with misspelled enctype="multipart/form-data", i was fix this exception by doing correct spelling .
Current request is not a multipart request client side error so please check your form.

i was facing this issue but it was due to the file i was uploading in postman wasn't in the postman working directory
Go to Settings -> General and scroll down to location to find it's location folder and put your files to upload there.
See : Setup Potman working directory

add required=false with the parameter
#RequestParam(required = false,value = "file") MultipartFile file

Related

React Spring Boot App Using Multipart Form Data - Required request part 'file' is not present

After days of attempting to upload a file using a React frontend and Spring Boot backend, I'm coming here to see if anyone can guide me in the right direction. Everything seems to be in place - I select my file, I see the file properties in the console, and I see form data being passed to the REST API, but I still get an error.
Some React snippets:
const onFileChangeHandler = (e) => {
e.preventDefault();
setFileAttachment({
fileAttachment: e.target.files[0]
})};
const formData = new FormData();
formData.append('file',fileAttachment)
const requestOptionsInsertNote = {
method: "POST",
body: formData
};
<input type="file" name="file" onChange={onFileChangeHandler}/>
Some Spring Boot snippets:
#PostMapping( "/api/notes/insertNote")
public void insertJournalNote(#RequestPart(value="file") MultipartFile file{
UploadedFileInfo uploadedFileInfo = new UploadedFileInfo();
try{
uploadedFileInfo.setFileData(file.getBytes());
uploadedFileInfo.setFileType(file.getContentType());
uploadedFileInfo.setFileName(file.getOriginalFilename());
}catch (IOException e){
e.printStackTrace();
}}
Console log data for console.log(fileAttachment):
Object { fileAttachment: File }​
fileAttachment: File { name: "file.jpg", lastModified: 1650655091391, size: 148823, … }​​
lastModified: 1650655091391​​
name: "file.jpg"​​
size: 148823​​
type: "image/jpeg"​​
webkitRelativePath: ""
Request sent to rest api:
-----------------------------174062142330182702901981377266
Content-Disposition: form-data; name="file"
[object Object]
-----------------------------174062142330182702901981377266--
Error message in Intellij:
Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present]
You should enable multipart in application.properties file:
spring.servlet.multipart.enabled=true
spring.servlet.multipart.max-file-size = -1
spring.servlet.multipart.max-request-size=-1
Also, don't forget to set the 'Content-Type':'multipart/form-data' in request header while sending your request.
EDIT:You need to define the CommonMultipartResolver Bean.
#Bean
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver multipart = new CommonsMultipartResolver();
multipart.setMaxUploadSize(3 * 1024 * 1024);
return multipart;
}
#Bean
#Order(0)
public MultipartFilter multipartFilter() {
MultipartFilter multipartFilter = new MultipartFilter();
multipartFilter.setMultipartResolverBeanName("multipartResolver");
return multipartFilter;
}
Also, it seems like we should leave the 'Content-Type' to blank, so that Chrome would set the boundary itself.
Turns out this:
setFileAttachment({
fileAttachment: e.target.files[0]
})
Needed to be this:
setFileAttachment(e.target.files[0])
It's always the little things

How to set a multipartfile parameter as "not required" in java with spring boot?

my problem is that I have a controller developed in Java with Spring Boot in which I edit "Portfolio" entities. Here I get the different attributes to edit this entity and also an image. My idea is that if I receive the empty parameters, it will reassign to the entity the values it had before and only modify the attributes that were sent in the form with some value.
This works correctly when I test in Postman, but when I send the image attribute as "undefined" or as "null" from the form in angular, my controller in the back end shows me an error that says: Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present]
My idea is to make the "file" attribute which is a MultipartFile can be empty and have no problems.
My controller is as follows:
#PutMapping("/portfolio/edit-data")
public Portfolio editPortfolio (#RequestParam("image")MultipartFile image,
#ModelAttribute("port") Portfolio port,
BindingResult result){
Portfolio portOriginal = portServ.buscarPortfolio(Long.valueOf(1));
if(!image.isEmpty()){
String rutaAbsoluta = "C:\\Users\\Paul";
try{
byte[] bytesImg = image.getBytes();
Path fullPath = Paths.get(absolutePath + "//" + image.getOriginalFilename());
Files.write(fullPath, bytesImg);
port.setImage(image.getOriginalFilename());
}catch (IOException e){
}
}else{
port.setImage(portOriginal.getImagen());
}
if("".equals(port.getName())){
port.setName(portOriginal.getName());
}
if("".equals(port.getTitle())){
port.setTitle(portOriginal.getTitle());
}
if("".equals(port.getIntroduction())){
port.setIntroduction(portOriginal.getIntroduction());
}
if("".equals(port.getFooter())){
port.setFooter(portOriginal.getFooter());
}
return portServ.editarPortfolio(port, Long.valueOf(1));
}
The following query is correct in Postman. So it would be useful to be able to submit an empty file in a form from angular.
Postman request
Try #RequestParam(required = false) (no need for the name to be specified with the value param, because spring takes the variable name by default and in your case they're the same)
You're method definition would look like this:
#PutMapping("/portfolio/edit-data")
public Portfolio editPortfolio (#RequestParam(required = false) MultipartFile image,
#ModelAttribute Portfolio port,
BindingResult result)

HTTP Error 400 "Required request part 'file' is not present" when uploading file

I'm trying to make an upload utility to load an excel file and make a bulk insert into DB.
I'm using AngularJS and Spring.
I've been trying for days, but I can't get java to read the uploaded file.
I'm using ngFileUpload and javascript is getting my file, but when I call my Controller the param is null or whatever the http error is the 400 "Required request part 'file' is not present".
Request header
I tried everything, adding the multipartresolver into config; enabling the multipart encoding into application.properties; adding the library into the pom.xml, but it still doesn't works.
server.port=8081
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.multipart.max-file-size=15000000
spring.http.multipart.max-request-size=15000000
#Bean
public MultipartResolver multipartResolver() {
return new CommonsMultipartResolver();
}
#PostMapping(value = "/excelUploader", consumes = "multipart/form-data")
public ResponseEntity<?> saveEnvironmentConfig(#RequestParam("file") MultipartFile submissions){
return null;
}
EDIT
Client Side Part
$scope.upload = function() {
Upload.upload({
url: $scope.uploadServiceUrl,
data: { file: $scope.files,
method: 'POST'}
}).then(function(resp) {
if ($scope.uploadCompleteFn) {
$scope.uploadCompleteFn();
}
console.log('Upload complete.');
}, function(resp) {
console.log('Error status: ' + resp.status);
});
};

HTTP REST API File upload gives 400 in windows

I have a REST API for uploading files to server written in Java with Spring Boot. I am testing the API through postman. The API is working fine in Linux Environment, but in windows environment it returns 400 Bad Request as response. There is no change in code in both systems, as I am using the same jar file in both environments.
The POST request consist of a file attachment in the form-data and a parameter userName. My API accepts the data in that format only. I have checked the headers in the request in both the machines, and I am just passing a single header Content-Type as application/json.
Could someone guide me, what might be causing the error? I have checked some answers in stackoverflow for what might be the reasons for HTTP 400 other than the endpoint no being existing. Nothing answered my query.
Edit: Adding the code I am using to upload the file.
private static String UPLOADED_FOLDER_BIRTHDAY = "D:/uploads/birthday/";
#PostMapping("/api/upload/birthday")
public ResponseEntity<?> uploadFileForBirthday(#RequestParam("file") MultipartFile uploadfile, #RequestParam("userName") String userName) {
if (uploadfile.isEmpty()) {
return new ResponseEntity("please select a file!", HttpStatus.OK);
}
try {
saveUploadedFiles(Arrays.asList(uploadfile), UPLOADED_FOLDER_BIRTHDAY);
} catch (IOException e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
return new ResponseEntity("Successfully uploaded - " +
uploadfile.getOriginalFilename(), new HttpHeaders(), HttpStatus.OK);
}
private void saveUploadedFiles(List<MultipartFile> files, String uploadPath) throws IOException {
for (MultipartFile file : files) {
if (file.isEmpty()) {
continue;
}
byte[] bytes = file.getBytes();
Path path = Paths.get(uploadPath + file.getOriginalFilename());
Files.write(path, bytes);
}
}

Upload Binary file on aws s3 using java Spring

I am able to upload file Using Form data as given below
but when i am trying to upload file by selecting binary as input getting error as given below;
{"timestamp":1490680735011,"status":500,"error":"Internal Server Error","exception":"org.springframework.web.multipart.MultipartException","message":"Current request is not a multipart request","path":"/uploadBinary"}
for that i am writing code as
#RequestMapping(value = "/uploadBinary", method = RequestMethod.POST)
public ResponseEntity<Object> uploadBinary(
#RequestParam("file") MultipartFile[] multipartFiles) {
try {
System.out.println("starting....");
fileService.upload(multipartFiles);
System.out.println("uploaded successfully ");
} catch (Exception ex) {
System.out.println(ex.getMessage());
return new ResponseEntity<Object>(new String(
"Something Went wrong while uploading data to server"),
HttpStatus.OK);
}
return new ResponseEntity<Object>(new String("uploaded successfully "),
HttpStatus.OK);
}
this works fine for uploading form data, but for binary selection its not working. how we resolve this issues??
Check whether you have added CommonsMultipartResolver in your spring configuration file
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

Categories