Upload data method in REST web service

前端 未结 1 1970
悲哀的现实
悲哀的现实 2021-01-21 04:31

Do anyone know how can I write POST method in RESTful web service to upload data using java ? I found that smartupload and commons.upload are just for web page.

1条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-21 05:06

    You can use some JAX-RS library, like Apache Wink, so you can write something like this:

    @Path("/upload")
    class UploadResource {
    
        @POST
        @Consumes(MediaType.APPLICATION_OCTET_STREAM)
        public Response upload(byte[] input) {
            // store input somewhere
            return Response.ok().build();
        }
    
    }
    

    So you will receieve your file is byte[]. You can also receive as InputStream:

    @Path("/upload")
    class UploadResource {
    
        @POST
        @Consumes(MediaType.APPLICATION_OCTET_STREAM)
        public Response upload(InputStream input) {
            // store input somewhere
            return Response.ok().build();
        }
    
    }
    

    0 讨论(0)
提交回复
热议问题