0

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?

1 Answer 1

1

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

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.