Lwazi Prusent
Lwazi Prusent

Reputation: 192

Can't upload files in spring boot

I've been struggling with this for the past 3 days now, I keep getting the following exception when I try upload a file in my spring boot project. org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present

I'm not sure if it makes a differance but I am deploying my application as a war onto weblogic, here is my controller

@PostMapping
public AttachmentDto createAttachment(@RequestParam(value = "file") MultipartFile file) {
    logger.info("createAttachment - {}", file.getOriginalFilename());
    AttachmentDto attachmentDto = null;
    try {
        attachmentDto = attachmentService.createAttachment(new AttachmentDto(file, 1088708753L));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return attachmentDto;
}

multi part beans I can see in spring boot actuator

enter image description here

payload seen in chrome

enter image description here

Upvotes: 2

Views: 2556

Answers (4)

sam
sam

Reputation: 2004

None of the above solutions worked for me, but when I digged deeper i found that spring security was the main culprit. Even if i was sending the CSRF token, I repeatedly faced the issue POST not supported. I came to know that i was receiving forbidden 403 when i inspected using developer tools in google chrome and saw the status code in the network tab. I added the mapping to ignoredCsrfMapping in Spring Security configuration and then it worked absolutely without any other flaw. Don't know why i was not allowed to post multipart data by security. Some of the mandatory setting that needs to be stated in application.properties file are as follows:

spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
spring.http.multipart.max-file-size=10MB
spring.http.multipart.max-request-size=10MB
spring.http.multipart.enabled=true

Upvotes: 0

Amit Gujarathi
Amit Gujarathi

Reputation: 1100

You are using multi part to send files so there is nothing much configuration to do to get desired result. I m having the same requirement and my code just run fine :

@RestController
@RequestMapping("/api/v2")
public class DocumentController {

    private static String bucketName = "pharmerz-chat";
    //   private static String keyName        = "Pharmerz"+ UUID.randomUUID();

    @RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA)
    public URL uploadFileHandler(@RequestParam("name") String name,
                                 @RequestParam("file") MultipartFile file) throws IOException {

/******* Printing all the possible parameter from @RequestParam *************/

        System.out.println("*****************************");

        System.out.println("file.getOriginalFilename() " + file.getOriginalFilename());
        System.out.println("file.getContentType()" + file.getContentType());
        System.out.println("file.getInputStream() " + file.getInputStream());
        System.out.println("file.toString() " + file.toString());
        System.out.println("file.getSize() " + file.getSize());
        System.out.println("name " + name);
        System.out.println("file.getBytes() " + file.getBytes());
        System.out.println("file.hashCode() " + file.hashCode());
        System.out.println("file.getClass() " + file.getClass());
        System.out.println("file.isEmpty() " + file.isEmpty());

/**
        BUSINESS LOGIC
Write code to upload file where you want
*****/
return "File uploaded";
}

Upvotes: 1

Bhargav
Bhargav

Reputation: 713

Name attribute is required for @RequestParm 'file'

<input type="file" class="file" name="file"/>

Upvotes: 3

Pedro Rodrigues
Pedro Rodrigues

Reputation: 1678

You can try use @RequestPart, because it uses HttpMessageConverter, that takes into consideration the 'Content-Type' header of the request part.

Note that @RequestParam annotation can also be used to associate the part of a "multipart/form-data" request with a method argument supporting the same method argument types. The main difference is that when the method argument is not a String, @RequestParam relies on type conversion via a registered Converter or PropertyEditor while @RequestPart relies on HttpMessageConverters taking into consideration the 'Content-Type' header of the request part. @RequestParam is likely to be used with name-value form fields while @RequestPart is likely to be used with parts containing more complex content (e.g. JSON, XML).

Spring Documentation

Code:

@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public AttachmentDto createAttachment(@RequestPart("file") MultipartFile file) {
    logger.info("Attachment - {}", file.getOriginalFilename());

    try {
        return attachmentService.createAttachment(new AttachmentDto(file, 1088708753L));
    } catch (final IOException e) {
        logger.e("Error creating attachment", e);
    }

    return null;
}

Upvotes: 1

Related Questions