建议我使用java将文件上传到Alfresco的任何基本示例。我是Alfresco的新手。
答案 0 :(得分:5)
以下是使用Python和cmislib的示例:
>>> from cmislib import CmisClient
>>> client = CmisClient('http://cmis.alfresco.com/cmisatom', 'admin', 'admin')
>>> repo = client.defaultRepository
>>> root = repo.rootFolder
>>> someFolder = root.createFolder('someFolder')
>>> someFile = open('test.txt', 'r')
>>> someDoc = someFolder.createDocument('Test Document', contentFile=someFile)
以下是使用Java和OpenCMIS的示例:
// default factory implementation
SessionFactory factory = SessionFactoryImpl.newInstance();
Map<String, String> parameter = new HashMap<String, String>();
// user credentials
parameter.put(SessionParameter.USER, "Otto");
parameter.put(SessionParameter.PASSWORD, "****");
// connection settings
parameter.put(SessionParameter.ATOMPUB_URL, "http://<host>:<port>/cmis/atom");
parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
parameter.put(SessionParameter.REPOSITORY_ID, "myRepository");
// create session
Session session = factory.createSession(parameter);
Folder root = session.getRootFolder();
// properties
// (minimal set: name and object type id)
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder");
properties.put(PropertyIds.NAME, "a new folder");
// create the folder
Folder parent = root.createFolder(properties);
String name = "myNewDocument.txt";
// properties
// (minimal set: name and object type id)
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document");
properties.put(PropertyIds.NAME, name);
// content
byte[] content = "Hello World!".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(properties, contentStream, VersioningState.MAJOR);
答案 1 :(得分:1)
在alfresco中使用Java和OpenCMIS的示例: Download OpenCMIS jars ( Release 1.1.0 -> Commodity Packages -> Package -> OpenCMIS Client with dependencies )
参考Link
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, <username>);
parameter.put(SessionParameter.PASSWORD, <password>);
// 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");
// 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 Parth Patel!".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.");
}
}
答案 2 :(得分:0)
借助Web服务库,我们可以使用java连接到alfresco。您可以在下面找到示例代码
WebServiceFactory.setEndpointAddress("url");
WebSererviceFactory.setEndpointAddress("url");
AuthenticationUtils.startSession(prop.getProperty("user"),prop.getProperty("password"));
System.out.println(AuthenticationUtils.getTicket());
ContentServiceSoapBindingStub contService = WebServiceFactory.getContentService();
创建父参考:
String path = "/app:company_home/cm:dir" + </pre>
ParentReference parRef = new ParentReference(STORE, null,path,ASSOC_CONTAINS, "{"+ Constants.NAMESPACE_CONTENT_MODEL + "}"+ fileName); </pre>
parRef.setPath("/app:company_home/cm:vasanpmsrole");
String name = fileName;
ContentFormat contentFormat =
new ContentFormat(mimeType, "UTF-8");
NamedValue[] properties = new NamedValue[ {Utils.createNamedValue(Constants.PROP_NAME, fileName)};
CMLCreate create = new CMLCreate("1",parRef, null, null, null,Constants.TYPE_CONTENT, properties);
CML cml = new CML();
cml.setCreate(new CMLCreate[]{create});
UpdateResult[] result = WebServiceFactory.getRepositoryService().update(cml) ;
Reference newContentNode =
result[0].getDestination();
Content content = contService.write(
newContentNode, Constants.PROP_CONTENT,
b, contentFormat);
strUuid = newContentNode.getUuid();
在此我使用了以下目录: