Hi Fan,

A couple of things to try.

1. Use a field name to associate to the file: curl -X PUT
    "http://localhost:8111"; -F "upload=@file.txt"
2. According to the @Put JavaDoc, you use a String return type with a
    String argument.  However, you usually don't need a return type from
    a PUT since the common paradigm is to redirect to a GET.  My
    declaration looks like this:
public void accept(final Representation payload) throws Exception
{
   ...
   final RestletFileUpload upload = new 
RestletFileUpload(myDiskItemFactory);
   final FileItemIterator fileItemIterator =
                     upload.getItemIterator(payload);
   while (fileItemIterator.hasNext())
   {
      final FileItemStream nextFileItemStream = fileItemIterator.next();
      if (nextFileItemStream.getFieldName().equals("upload"))
      {
        // Do upload stuff
      }
   }
}

And it works fine.

Dustin


On 07/16/2013 08:20 AM, Fan J wrote:
> Hi all,
>
> I am a novice to Restlet and learning to build something with it. I am using 
> Restlet 2.1.2 J2SE package. Now I am stuck with accepting file upload using 
> @Put annotation. The following is my code of @Put method.
>
>   private static final String tmp = "/tmp/restlet/";
> ...
> @Put
> public String store(Representation entity) throws Exception{
>               if(entity == null){
>                       setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
>                       return "";
>               }
>               
>               if(!MediaType.MULTIPART_FORM_DATA.equals(entity.getMediaType(), 
> true)){
>                       setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
>                       return "";
>               }
>               
>               DiskFileItemFactory factory = new DiskFileItemFactory();
>               factory.setSizeThreshold(1024000);
>               
>               RestletFileUpload upload = new RestletFileUpload(factory);
>               
>               List items = upload.parseRepresentation(entity);
>               
>               for(FileItem fi : items){
>                       File file = new File(tmp + fi.getName());
>                       fi.write(file);
>                       System.out.println(fi.getName());
>               }
>               
>               setStatus(Status.SUCCESS_OK);
>               return "";
>       }
> Then I send PUT requests using cURL like these:
>
>   curl -T file.txt http://localhost:8111/
> curl -X PUT -T file.txt http://localhost:8111/
> curl -X PUT --data @file.txt http://localhost:8111/
> but finally got a 400 error. I doubt it is probably the problem of my code 
> but not syntax of the cURL command. Basically, the problem is that the 
> parameter, entity, could not get the uploading file and is still a null as 
> the code proceeds. Is there anyone who has similar experience? Any ideas?
>
>
> Thanks,
> dchampion
>
> ------------------------------------------------------
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3060593

-- 
Dustin N. Jenkins | 250-363-3101


Dustin N. Jenkins | Tel/Tél: 250.363.3101 | dustin.jenk...@nrc-cnrc.gc.ca

facsimile/télécopieur: (250) 363-0045

National Research Council Canada | 5071 West Saanich Rd, Victoria BC. 
V9E 2E7

Conseil national de recherches Canada | 5071, ch. West Saanich, Victoria 
(C.-B) V9E 2E7

Government of Canada | Gouvernement du Canada

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3060611

Reply via email to