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>"
}
}
精彩评论