1

I want to upload a .png file and storage that.when I send it with content-type=multipart/form-data I receive this Exception

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 
  'application/octet-stream' not supported . 

I think my application doesn't understand form-data contentType and throws an irrelevant exception. What's wrong?

    @Controller
    public class ProfileController {
        private static final Logger logger = LoggerFactory.getLogger(ProfileController.class);

        @Autowired
        ProfileService service;

        //http : http --pretty all --form PUT localhost:8080/profile/v1?thumb=glb
        @PutMapping(value = "/v1")
        public Mono<ResponseEntity<String>> upload(@RequestPart(value = "thumb", required = false) String thumbFilterName,
                                                   @RequestPart(value = "genthumb", required = false) String genthumb,
                                                   @RequestPart("hash") String hash,
                                                   @RequestPart("uploader") Long uploader,
                                                   @RequestPart("file") MultipartFile file) throws Exception {


        System.out.println(thumbFilterName);
        System.out.println(gentumb);
        System.out.println(uploader);
        System.out.println(file);
//and doing storage stuff!
return Mono.just(ResponseEntity.ok().build());
        }
    }

and this is my Rest Client that is used for sending file. Pay attention to response.

These are screenshots of restClient. Sending file with restClient:

sending file with restClient

And this is setting headers

and this is setting headers

1 Answer 1

2

In uploader you must change @RequestPart to @RequestParam

@RequestPart("uploader") Long uploader,

Javadoc explaination:

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).

Sign up to request clarification or add additional context in comments.

1 Comment

hi , realy realy thanks for replying , it's worked :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.