cancel
Showing results for 
Search instead for 
Did you mean: 

Simply Java Example to upload a document to Alfresco

jlabuelo
Champ on-the-rise
Champ on-the-rise
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);

         System.out.println(post.getResponseBodyAsString());

         post.releaseConnection();
         return true;
      }
   else
      {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!!
3 REPLIES 3

iv0id
Champ in-the-making
Champ in-the-making
Hi,

i have an example but using the OpenCMIS API, do you have the ability to use it ?

seifsaadaoui
Champ in-the-making
Champ in-the-making
hello, I need the exemple with open cmis if you can help me

iv0id
Champ in-the-making
Champ in-the-making
Don't forget to change the login informations

<java>
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import org.apache.chemistry.opencmis.client.api.Document;
import org.apache.chemistry.opencmis.client.api.Folder;
import org.apache.chemistry.opencmis.client.api.Session;
import org.apache.chemistry.opencmis.client.api.SessionFactory;
import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
import org.apache.chemistry.opencmis.commons.PropertyIds;
import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.apache.chemistry.opencmis.commons.data.ContentStream;
import org.apache.chemistry.opencmis.commons.enums.BindingType;
import org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl;


public class Test {

   public static void main(String[] args) throws UnsupportedEncodingException {
      Map<String, String> sessionParameters = new HashMap<String, String>();
      sessionParameters.put(SessionParameter.USER, "admin");
      sessionParameters.put(SessionParameter.PASSWORD, "alfresco");
      sessionParameters.put(SessionParameter.ATOMPUB_URL, "http://localhost:8081/alfresco/api/-default-/public/cmis/versions/1.1/atom");
      sessionParameters.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
      SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
      Session lSession = sessionFactory.getRepositories(sessionParameters).get(0).createSession();
      Folder root = lSession.getRootFolder();
                Map<String, Object> folderProperties = new HashMap<String, Object>();
                folderProperties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder");
                folderProperties.put(PropertyIds.NAME, "testfolder");
                Folder newFolder = root.createFolder(folderProperties);
      Map<String, Object> lProperties = new HashMap<String, Object>();
      String name = "testdocument.txt";
      lProperties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document");
      lProperties.put(PropertyIds.NAME, name);
      byte[] content = "CMIS Testdata One".getBytes();
                InputStream stream = new ByteArrayInputStream(content);
                ContentStream contentStream = new ContentStreamImpl(name, new BigInteger(content), "text/plain", stream);
                Document newContent1 =  newFolder.createDocument(lProperties, contentStream, null);
      System.out.println("Document created: " + newContent1.getId());
   }
}

</java>