建议我使用 java 将文件上传到 Alfresco 的任何基本示例。我是 Alfresco 的新手。
问问题
9739 次
3 回答
6
这是一个使用 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);
于 2012-05-17T14:34:45.333 回答
2
在露天使用 Java 和 OpenCMIS 的示例: 下载 OpenCMIS jars(版本 1.1.0 -> Commodity Packages -> Package -> OpenCMIS Client with dependencies)
参考链接
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.");
}
}
于 2017-08-03T10:24:15.017 回答
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();
在此我使用了以下目录:
- 轴1.4.jar
- AlfrescoWebService.jar
于 2013-05-13T08:42:50.053 回答