AnsweredAssumed Answered

Uploading file to alfresco repository using AlfrescoRest API

Question asked by skiran on Nov 8, 2012
Latest reply on May 5, 2014 by srowsell
Hi all,
I am developing a e-commerce project for magazines. I am using alfresco 3.4.c as Repository for storing content and I am using Jerse client and alfresco REST API to connect to alfresco repository.  In this project, I have to write a Jersey client program to upload content to alfresco repository.
My Jersey Client program is below. In this program, I am uploading Html file, but I want to upload any file (ex: pdf , word file etc).

I used the built in rest service  /alfresco/service/api/upload to upload file into alfresco repository. But it is not uploading content into repository.


    public class AlfrescoFileUpload {
   
   public static void main(String[] args) throws IOException{
   
        ClientConfig config = new DefaultClientConfig();

          Client client = Client.create(config);
         BufferedReader br=null;
         
          WebResource webResource = client.resource("http://127.0.0.1:8888/alfresco/service/api/login");
         
          MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
          queryParams.add("u", "admin");
          queryParams.add("pw", "alfresco");
      
          String s = webResource.queryParams(queryParams).get(String.class);
         
          client.addFilter(new HTTPBasicAuthFilter("admin", "alfresco"));
               
          WebResource wr= client.resource("http://localhost:8888/alfresco/service/api/upload");
                 
          File file=new File("/home/sivakiranm/drag.html");
          
          FormDataMultiPart form = new FormDataMultiPart();
             
           form.field("filedata", "@drag.html");
           form.field("username", "admin");
           form.field("password", "alfresco");
          
            form.field("siteid","gopustak");
           
            form.field("containerid","CompanyHome");
            form.field("uploaddirectory","documentLibrary");
            form.field("filename", "drag.html");
            form.field("noderef","nodeRef=workspace://SpacesStore/6eabdd9c-0489-4d71-8c43-c8ed4c3c6c85");
           // form.field("updatenoderef","alfresco/service/api/node/workspace/SpacesStore/23q1-34fr-5ab4-t4vf/drag.html");
            form.field("description","this is html file");
            form.field("contenttype","application/html");
            form.field("majorversion","1.0");
            form.field("overrite","yes");
            form.field("thumbnails", "yes");
               
               
          form.field("filename", file.getName());
          
           form.bodyPart(new FileDataBodyPart("file", file, MediaType.MULTIPART_FORM_DATA_TYPE));
         
          //InputStreamProvider response = wr.type(MediaType.MULTIPART_FORM_DATA).post(InputStreamProvider.class, form);
          
         InputStream in=wr.type(MediaType.MULTIPART_FORM_DATA).post(InputStream.class,form);
        
         OutputStream out=new FileOutputStream(form);
           byte buf[]=new byte[1024];
           int len;
           while((len=in.read(buf))>0)
           out.write(buf,0,len);
           out.close();
           in.close();
           System.out.println("\nFile is created………………..");
           }
              
}
}


Can any one help me how to upload file to Alfreco Repository using Rest api?

Outcomes