Hello Pierre,
I'm sorry for the delay of my answer...
in your case, there is no need of using the fileupload extension,
especially because the request is not a correctly formatted multipart
request (the Restlet framework does not support the generation of such
requests), and could be misunderstood by a server.
If you intend to write the posted entity as a file, just proceed as follow:
@Post
public void upload(Representation entity) throws ResourceException {
try {
entity.write(new FileOutputStream(new
File("/tmp/myfile.zip")));
} catch (Exception e) {
throw new ResourceException(e);
}
}
In addition, you can "enrich" the posted entity with the
"content-disposition" header as follow:
Representation rep = new FileRepresentation(myzipfile,
MediaType.APPLICATION_ZIP);
// Prepare the disposition header with the file name.
Form form = new Form();
form.add(Disposition.NAME_FILENAME, myzipfile.getName());
rep.setDisposition(new Disposition(Disposition.TYPE_INLINE, form));
filesResources.upload(rep);
And on the server side:
entity.write(new FileOutputStream(new File("/tmp/" +
entity.getDisposition().getFileName())));
Of course, you can also test the MediaType of the incoming entity and
make the distinction between entities sent as in a multipart request
(with clients that support multipart requests), or entity simply posted.
Best regards,
Thierry Boileau
> Hi,
>
> I searched for one day how to upload a simple zip file with Restlet and
> didn't manage to get it work. So I need some help.
>
> I have an interface :
> ************************
> public interface FileResource {
> @Get
> public ArrayList<String> getFiles();
>
> @Post
> public void upload(Representation fileEntity);
>
>
> @Delete
> public void archiveFile(String filename);
> }
> ****************
>
> I use Android as a client :
> *****************
> ClientResource clientResource = new ClientResource(BASE_URL + "/files/" +
> destUserDir);
> FileResource filesResources = clientResource.wrap(FileResource.class);
> ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC;
> ChallengeResponse authentication = new ChallengeResponse(scheme, user, pwd);
> clientResource.setChallengeResponse(authentication);
>
> filesResources.upload(new FileRepresentation(myzipfile,
> MediaType.APPLICATION_ZIP)); //also tried MULTIPART...
> *****************
>
> And I'd like to receive on my tomcat servlet :
> *****************
> public class FilesResource extends ServerResource implements FileResource {
>
>
> public void upload(Representation entity) {
> getLogger().log(Level.INFO, "UPLOADING");
>
> try {
> FileItemFactory factory = new DiskFileItemFactory();
> RestletFileUpload fileUpload = new RestletFileUpload(factory);
> List<FileItem> items = fileUpload.parseRequest(getRequest());
> //or parseRepresentation(entity)
>
> FileItem fileItem = items.get(0);
>
> ...
> *****************
>
> But I always get this kind of errors :
> FileUploadException: the request was rejected because no multipart boundary
> was found
> or
> InvalidContentTypeException: the request doesn't contain a
> multipart/form-data or multipart/mixed stream, content type header is
> application/zip
>
>
> Thanks for your help !
>
> ------------------------------------------------------
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2444808
>
>
------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2448048