srikanth
srikanth

Reputation: 103

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

I am working on jersey restful webservices(Java).

I want to upload or send a file to server using restful webservices.

Please help me out on this?

Upvotes: 0

Views: 618

Answers (1)

Artem Vlasov
Artem Vlasov

Reputation: 33

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>"
    }

}

Upvotes: 1

Related Questions