AnsweredAssumed Answered

Simply Java Example to upload a document to Alfresco

Question asked by jlabuelo on Jun 2, 2015
Latest reply on Jun 12, 2015 by seifsaadaoui
Hi there

Sorry to ask this, but we have spent almost a week reading posts and googling to try to create a java code that could upload a file from an external system to Alfresco.

i think that after all what we have readed we have a complete mess in our heads, as we are not able to find an easy example that explains everything step by step.

We have this code so far

   public boolean UploadDocument()
      String ticket = null;
      ticket = this.getAlfticket(this.url,this.login,this.password);
      if (ticket!=null)
         String URL= this.url+"/service/api/upload/?alf_ticket="+ticket;

         File file = this.ToFile(this.fileobj)

         HttpClient client = new HttpClient();

         PostMethod post = new PostMethod(URL);

         Part[] parts = {
               new FilePart("filedata", filename, file, filetype, null),
               new StringPart("filename", filename),
               new StringPart("description", "description"),
               // new StringPart("destination", "workspace://SpacesStore/7118e242-86b1-489a-ac2e-193364ccf6e4"),
                new StringPart("contenttype", "hula_type"),
                new StringPart("departamento",this.departamento),
               post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));

         int status = client.executeMethod(post);


         return true;
      {System.out.println("Not able to connect");}

However the problem is that in the line

   // new StringPart("destination", "workspace://SpacesStore/7118e242-86b1-489a-ac2e-193364ccf6e4"),

we must set the noderef of the folder where we would like to upload the document… however we dont want it to be "fixed" as we want to upload it to the userhomes of the user that logs in

Could you please explain me with some code guideness how to obtain the noderef of the userhome space for the user so we could include it in this line and get the file to be uploaded there??

Thanks a lot.

We are using Alfresco 5.0c, and also afer all this reading, dont know if the code will work in this Alfresco version or not….:-((((

Also we would like to know how to set the type of the document once uploaded, and modify some properties… but first, we must upload the file!!

Thanks a lot in advance!!