File upload along with other object in Jersey restful web service

后端 未结 6 1730
忘掉有多难
忘掉有多难 2020-11-22 13:34

I want to create an employee information in the system by uploading an image along with employee data. I am able to do it with different rest calls using jersey. But I want

6条回答
  •  难免孤独
    2020-11-22 13:40

    The request type is multipart/form-data and what you are sending is essentially form fields that go out as bytes with content boundaries separating different form fields.To send an object representation as form field (string), you can send a serialized form from the client that you can then deserialize on the server.

    After all no programming environment object is actually ever traveling on the wire. The programming environment on both side are just doing automatic serialization and deserialization that you can also do. That is the cleanest and programming environment quirks free way to do it.

    As an example, here is a javascript client posting to a Jersey example service,

    submitFile(){
    
        let data = new FormData();
        let account = {
            "name": "test account",
            "location": "Bangalore"
        }
    
        data.append('file', this.file);
        data.append("accountKey", "44c85e59-afed-4fb2-884d-b3d85b051c44");
        data.append("device", "test001");
        data.append("account", JSON.stringify(account));
        
        let url = "http://localhost:9090/sensordb/test/file/multipart/upload";
    
        let config = {
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        }
    
        axios.post(url, data, config).then(function(data){
            console.log('SUCCESS!!');
            console.log(data.data);
        }).catch(function(){
            console.log('FAILURE!!');
        });
    },
    

    Here the client is sending a file, 2 form fields (strings) and an account object that has been stringified for transport. here is how the form fields look on the wire,

    On the server, you can just deserialize the form fields the way you see fit. To finish this trivial example,

        @POST
    @Path("/file/multipart/upload")
    @Consumes({MediaType.MULTIPART_FORM_DATA})
    public Response uploadMultiPart(@Context ContainerRequestContext requestContext,
            @FormDataParam("file") InputStream fileInputStream,
            @FormDataParam("file") FormDataContentDisposition cdh,
            @FormDataParam("accountKey") String accountKey,
            @FormDataParam("account") String json)  {
        
    
        
        System.out.println(cdh.getFileName());
        System.out.println(cdh.getName());
        System.out.println(accountKey);
        
        try {
            Account account = Account.deserialize(json);
            System.out.println(account.getLocation());
            System.out.println(account.getName());
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return Response.ok().build();
        
    }
    

提交回复
热议问题