Reputation: 3636
I have a very big problem.I need to upload a file using a rest service . My problem is that i need to upload the file together with some info about the file ..all in one POST request.I am using Restlet.
Till now i didn't had to upload the file just the info and i did that using the outputStreamWriter. Here is an example:
URL adminServerUrl = new URL(wwww....);
HttpURLConnection adminConnection = (HttpURLConnection) adminServerUrl.openConnection();
adminConnection.setRequestMethod("POST");
adminConnection.setDoOutput(true);
adminConnection.setDoInput(true);
adminConnection.setUseCaches(false);
adminConnection.setAllowUserInteraction(false);
OutputStream conOutput = adminConnection.getOutputStream();
Writer writer = new OutputStreamWriter(conOutput, "UTF-8");
writer.write("&due_date=" + (project.getDueDate());
writer.write("&source=" + project.getSourceLanguage());
writer.close();
conOutput.close();
After that i would get the response and that was it....but now i have to upload the file and i have no clue how to do that
Upvotes: 1
Views: 2007
Reputation: 2892
I would recommend using both Restlet API on the client and server side if possible. That will make your code simpler.
The usual solution would be to use multipart fileupload, but this isn't necessary and more complex in general. You can first create an upload resource with the info/metadata and the send the file to a child resource.
On the client side, you should load you file using a FileRepresentation, and send it to your target server using ClientResource#post(myFileRep) for example.
On the server side, you should just retrieve the posted entity/representation and save it to a local file or somewhere else, using ClientResource again (file:/// URI scheme works with PUT).
Upvotes: 1