Upload data method in REST web service

不打扰是莪最后的温柔 提交于 2020-01-11 10:04:56

问题


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:


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();
    }

}


来源:https://stackoverflow.com/questions/5496023/upload-data-method-in-rest-web-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!