开发者

How to upload a file to server using jersey restful web services

开发者 https://www.devze.com 2023-04-01 04:00 出处:网络
I am working on jersey restful webservices(Java). I want to upload or sen开发者_开发技巧d a file to server using restful webservices.

I am working on jersey restful webservices(Java).

I want to upload or sen开发者_开发技巧d a file to server using restful webservices.

Please help me out on this?


First of all you have to explore such terms as HTTP query structure, Multipart MIME type etc. The simplest code with jersey would look like snippet below. It's written in scala, but you should easily get the sense:

@Path("/upload")
class UploadFileResource {

    @POST
    @Path("/file")
    @Consumes(Array(MediaType.MULTIPART_FORM_DATA))
    @Produces(Array(MediaType.TEXT_PLAIN))
    def processUpload(
        @FormDataParam("file") uploadedInputStream: InputStream,
        @HeaderParam("Content-Length") length: Int) = {
        println("Content-Length: " + length)
    }

    @GET
    @Path("/form")
    @Produces(Array(MediaType.TEXT_HTML))
    def getFormMurkup() = {
        "<html><body><form method='post' action='file' enctype='multipart/form-data'>" +
            "<input type='file' name='file' />" +
            "<input type='submit' value='Upload' />" +
            "</form>" +
            "</html></body>"
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消