Upload a file in Alfresco using java | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link



Download Jars : Link > Release 1.1.0 > Commodity Packages > Package > OpenCMIS Client with dependencies

File : AlfrescoFileUpload.java

package com.alfresco;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
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.enums.VersioningState;
import org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl;

public class AlfrescoFileUpload {

	public static void main(String[] args) {

		// default factory implementation
		SessionFactory factory = SessionFactoryImpl.newInstance();
		Map<String, String> parameter = new HashMap<String, String>();

		// user credentials
		parameter.put(SessionParameter.USER, "admin");
		parameter.put(SessionParameter.PASSWORD, "admin");

		// connection settings
		parameter.put(SessionParameter.ATOMPUB_URL, "http://127.0.0.1:8080/alfresco/api/-default-/public/cmis/versions/1.1/atom");
		parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());

		// create session
		Session session = factory.getRepositories(parameter).get(0).createSession();
		Folder root = session.getRootFolder();

		// properties
		Map<String, Object> properties = new HashMap<String, Object>();
		properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder");
		properties.put(PropertyIds.NAME, "MyAlfrescoFolder"); // folder name

		// create the folder
		Folder parent = root.createFolder(properties);

		String name = "NewTextFile.txt";

		// properties
		Map<String, Object> properties2 = new HashMap<String, Object>();
		properties2.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document");
		properties2.put(PropertyIds.NAME, name);

		// content
		byte[] content = "Hello Code Factory...!".getBytes();
		InputStream stream = new ByteArrayInputStream(content);
		ContentStream contentStream = new ContentStreamImpl(name, BigInteger.valueOf(content.length), "text/plain", stream);

		// create a major version
		Document newDoc = parent.createDocument(properties2, contentStream, VersioningState.MAJOR);

		System.out.println("DONE.");

	}

}

5 thoughts on “Upload a file in Alfresco using java | Code Factory”

  1. Can I just say what a aid to seek out somebody who actually knows what theyre speaking about on the internet. You definitely know the way to convey an issue to gentle and make it important. More people have to read this and understand this facet of the story. I cant believe youre not more standard because you definitely have the gift.

    Liked by 1 person

  2. I think this is one of the most significant information for me. And i’m glad reading your article. But should remark on few general things, The site style is wonderful, the articles is really nice : D. Good job, cheers

    Liked by 1 person

  3. I just wanted to type a word to be able to express gratitude to you for some of the stunning guides you are placing on this site. My time consuming internet search has at the end been recognized with reasonable strategies to write about with my partners. I ‘d state that that most of us readers actually are unequivocally endowed to exist in a decent community with many outstanding people with valuable advice. I feel somewhat lucky to have come across your website page and look forward to some more fabulous minutes reading here. Thank you once again for a lot of things.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s