Spring Boot Required Request Part 'File' Is Not Present

upload file springboot Required request part 'file' is not present

This is how your request in Postman should look like:

enter image description here

My sample code:

application.properties

#max file and request size 
spring.http.multipart.max-file-size=10MB
spring.http.multipart.max-request-size=11MB

Main Application Class:

Application.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

Rest controller class:

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


@Controller
@RequestMapping("/fileupload")
public class MyRestController {

@RequestMapping(value = "/file", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String myService(@RequestParam("file") MultipartFile file,
@RequestParam("id") String id) throws Exception {

if (!file.isEmpty()) {

//your logic
}
return "some json";

}
}

pom.xml

//...

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

....



<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>

//...

Spring Boot 2.5.x: Required request part 'file' is not present

It turns out this issue was affected after the Spring Boot 2.2. Since that version, the filter HttpHiddenMethodFilter was disabled by default. The issue got fixed after enabling the filter in application.properties.

spring.mvc.hiddenmethod.filter.enabled=true

My Findings in Detail

The purpose of the above filter has nothing to do with the error I was getting. But the request parts was getting initialized as a side effect of executing the filter. More specifically, when the filter tries to retrieve the _method parameter value (e.g. request.getParameter("_method"), the getParameter method of the request instance internally seems to parse the parameters which then initializes the request parts. So when the filter was disabled in spring-boot version 2.2, there was nothing to initialize the request parts.

I feel like the request parts initialization should be fixed within the Spring framework itself. But until then, either we could enable the HttpHiddenMethodFilter filter, or we could define a custom filter that takes care of initializing the request parts, something like below:

@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
public class RequestInitializerFilter extends OncePerRequestFilter {

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

request.getParameterNames(); // this takes care of initializing request `parts`

filterChain.doFilter(request, response);
}
}

Spring Boot Required request part 'file' is not present

After a while, I was able to solve this issue: In my application.properties file I added the following:

spring.servlet.multipart.max-file-size=128MB
spring.servlet.multipart.max-request-size=128MB
spring.http.multipart.enabled=true
upload.path=/export/home/

Required request part 'file' is not present in Spring Boot

issue is in declaration of MockMultipartFile, first parameter should match controller @RequestParam param. So, in your case, should be:

MockMultipartFile file = new MockMultipartFile("file", "url", MediaType.APPLICATION_JSON_VALUE, image);

Also, I recommend to update your controller method to the following one:

@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<LogoDto> uploadLogo(@RequestPart("file") MultipartFile multipartFile) {
...
}

[org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present

As on the server side in the controller you've mentioned @RequestParam("file") MultipartFile file so you need to pass the key as file in the form-data while uploading the file from the client.

Springboot Required request part 'file' is not present

In postman under "key" I wasn't setting anything. I needed to set this as 'file'. I previously made the assumption all I had to do was click the drop-down and select file.

I will include below all the updated code & a link to the image which explains this better(I couldn't display image here as reputation < 10)

link to postman Image

@RestController
public class UploadController {

@PostMapping("/upload")
@ResponseBody
public boolean upload(@RequestParam("file") MultipartFile file) {
try{
if(file.isEmpty() ==false){
System.out.println("Successfully Uploaded: "+ file.getOriginalFilename());

return true;
}
else{
System.out.println("ERROR");
return false;
}
}
catch(Exception e){
System.out.println(e);
return false;
}
}
}

Springboot uploading files Required request part 'file' is not present

Change

<input type="file" name="file_upload" class="form-control mb-4 col-4" placeholder="Изображение">

To

<input type="file" name="file" class="form-control mb-4 col-4" placeholder="Изображение">

Your controller expects a param file, but from html you are sending file_upload. Thats why spring shows error message "Required request part 'file' is not present"

Required request part 'file' is not present] springboot client

This will not work with postForObject.

Use postForEntity instead:

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART__FORM__DATA);

MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file", new FileSystemResource(file));

HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);

RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity(url+"/doc_file", requestEntity, String.class);

upload multipart file springboot mockMvc Required request part 'file' is not present, testing controller

The name of the file should be "file" here

MockMultipartFile file
= new MockMultipartFile(
"file",
"photo.jpeg",
MediaType.IMAGE_JPEG_VALUE,
"photo".getBytes()
);



Related Topics



Leave a reply



Submit