I have a problem with upload file in REST web service using java. I don't know how to get the source file. The curl command is like that
curl --upload-file /home/student1/text1.txt http://localhost:20260/project/resources/user1/sub-folder/text1.txt
My question is anyone know how to get the source file url "/home/student1/text1.txt" in REST web service @PUT method?Or any other way to get it?
thanks
I'm not terribly familiar with JAX-RS but I think the following code does what you are looking for:
I haven't verified that it is safe against trying to put
..
for eitheruser
ordirectory
so it might require input sanitation. Also, the errors and exceptions needs to be handled better.I tested this out with curl and it creates the file on the server file system containing the contents of the file passed via
--upload-file
which can then be retrieved by issuing a GET to the same resource. I did have to add the Content-Type header to the curl request to get it working though:I tested this out using Restlet's implementation of JAX-RS.
This new version handles your additional requirement of supporting the query parameters you mentioned in the other comment. I refactored it to use properties instead of method arguments, since the argument lists were growing unwieldy. I learned that it is thread safe to do so from the JAX-RS spec. Also, I modified the code to support plain text and binary files, though I haven't tried it with different file encodings. Since the method is expecting a byte array input I'm hoping that the JAX-RS framework does the right thing and can tell which encoding to use if one is provided in the request's Content-Type header.
I'm assuming that curl uses mltipart/form-data as content type for the upload. The filename would be part of the Content-Disposition header of the first MIME part.