restful image upload exception

2019-05-12 00:51发布

问题:

I have a restful interface as shown below. I am trying to upload an image using jaxrs interface but I am faced with an error

    @POST
        @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
        @Path("createUserphotoDirectory/{userid}/{serverName}")
        @Consumes("multipart/form-data")
        public String createUserDirectory(@PathParam("userid") Long userid,
                   @PathParam("serverName") String serverName,
                   MultipartFormDataInput input) {
                System.out.println("1");
               photoService.createServerImages(userid,serverName,input);
               return responseMessageSource.getMessage("SUCCESSFULL_CRATED_ALBUM",null,null);
            }

When I request using this form

<html>
<body>
    <h1>JAX-RS Upload Form</h1>

    <form action="/AlbumApplication/rest/createUserphotoDirectory/1/FeedServer" method="post" enctype="multipart/form-data">

       <p>
        Select a file : <input type="file" name="uploadedFile" size="50" />
       </p>

       <input type="submit" value="Upload It" />
    </form>

</body>
</html>

I get this error - The request sent by the client was syntactically incorrect (java.lang.RuntimeException: Could find no Content-Disposition header within part).


i forgot to write ,i use Springmvc at mvc side,it may be pertain spring mvc block?

回答1:

Changing the REST service signature as the following may solve your problem

public String createUserDirectory(@PathParam("userid") Long userid,
         @PathParam("serverName") String serverName, 
         @FormDataParam("uploadedFile") File file,
         @FormDataParam("uploadedFile") FormDataContentDisposition disposition) {