Home > Joe's Stories > Upload Files using REST

Upload Files using REST

I have been working on an interesting problem of trying to upload a file to a server using a REST service in Java. So far things have been quite complicated.

I haven’t been able to find anything that would work. Originally I thought it would be quite easy to just send it a Http Form that held the file, but I cannot get the service to read anything but a String. I was hoping I would be able to handle this without using a framework, but most of the solutions I find use Springs or Struts.

I wanted to send an HttpServletRequest and pull the file out of it, but I cannot get the request into the REST method. There must be a simple way to do this that I am missing. I post here if I manage to solve this.

EDIT: I finally managed to do this but forgot to update my post to explain how I figured it out.

It turned out to be something really simple that I kicked myself for not thinking of earlier.

createHttpProduct( @Context HttpServletRequest request )

All I needed was  javax.ws.rs.core.Context to give me access to the data that I needed. I should have known that already because I was using javax.ws.rs.core.SecurityContext earlier. Oh well, live and learn I guess.

Categories: Joe's Stories Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.