使用java实现在Alfresco中上载文件

使用java实现在Alfresco中上载文件,alfresco,Alfresco,建议我使用java将文件上载到Alfresco的任何基本示例。我不熟悉Alfresco。下面是一个使用Python和: 下面是一个使用Java和: //默认工厂实现 SessionFactory工厂=SessionFactoryImpl.newInstance(); Map参数=new HashMap(); //用户凭据 parameter.put(SessionParameter.USER,“Otto”); parameter.put(SessionParameter.PASSWORD,***

建议我使用java将文件上载到Alfresco的任何基本示例。我不熟悉Alfresco。

下面是一个使用Python和:

下面是一个使用Java和:

//默认工厂实现
SessionFactory工厂=SessionFactoryImpl.newInstance();
Map参数=new HashMap();
//用户凭据
parameter.put(SessionParameter.USER,“Otto”);
parameter.put(SessionParameter.PASSWORD,*****);
//连接设置
parameter.put(SessionParameter.ATOMPUB_URL,“http:/:/cmis/atom”);
parameter.put(SessionParameter.BINDING_TYPE,BindingType.ATOMPUB.value());
parameter.put(SessionParameter.REPOSITORY_ID,“myRepository”);
//创建会话
Session Session=factory.createSession(参数);
Folder root=session.getRootFolder();
//性质
//(最小集合:名称和对象类型id)
映射属性=新的HashMap();
properties.put(PropertyIds.OBJECT_TYPE_ID,“cmis:folder”);
properties.put(PropertyIds.NAME,“一个新文件夹”);
//创建文件夹
文件夹父项=root.createFolder(属性);
String name=“myNewDocument.txt”;
//性质
//(最小集合:名称和对象类型id)
映射属性=新的HashMap();
properties.put(PropertyIds.OBJECT_TYPE_ID,“cmis:document”);
properties.put(PropertyIds.NAME,NAME);
//内容
byte[]content=“Hello World!”.getBytes();
InputStream=新的ByteArrayInputStream(内容);
ContentStream ContentStream=new ContentStreamImpl(名称,biginger.valueOf(content.length),“text/plain”,stream);
//创建主要版本
Document newDoc=parent.createDocument(properties、contentStream、VersioningState.MAJOR);

在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();
String path=“/app:company\u home/cm:dir”+
parentreferef=newparentreference(STORE,null,path,ASSOC_包含,“{”+常量.名称空间_内容_模型+“}”+文件名);
parRef.setPath(“/app:company_home/cm:vasanpmsrole”);
字符串名称=文件名;
ContentFormat ContentFormat=
新的内容格式(mimeType,“UTF-8”);
NamedValue[]属性=新的NamedValue[{Utils.createNamedValue(Constants.PROP_NAME,fileName)};
CMLCreate create=new CMLCreate(“1”,parRef,null,null,Constants.TYPE_CONTENT,properties);
CML=新的CML();
setCreate(新的CMLCreate[]{create});
UpdateResult[]result=WebServiceFactory.getRepositoryService().update(cml);
引用newContentNode=
结果[0]。getDestination();
Content=contService.write(
newContentNode,Constants.PROP_CONTENT,
b、 内容格式);
struid=newContentNode.getUuid();
在本文中,我使用了以下目录:

  • axis-1.4.jar
  • AlfrescoWebService.jar

在露天使用Java和OpenCMIS的示例:

参考文献

package com.alfresco;
导入java.io.ByteArrayInputStream;
导入java.io.InputStream;
导入java.math.biginger;
导入java.util.HashMap;
导入java.util.Map;
导入org.apache.chemistry.opencmis.client.api.Document;
导入org.apache.chemistry.opencmis.client.api.Folder;
导入org.apache.chemistry.opencmis.client.api.Session;
导入org.apache.chemistry.opencmis.client.api.SessionFactory;
导入org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
导入org.apache.chemistry.opencmis.commons.PropertyIds;
导入org.apache.chemistry.opencmis.commons.SessionParameter;
导入org.apache.chemistry.opencmis.commons.data.ContentStream;
导入org.apache.chemistry.opencmis.commons.enums.BindingType;
导入org.apache.chemistry.opencmis.commons.enums.VersioningState;
导入org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl;
公共舱露天甲板{
公共静态void main(字符串[]args){
//默认工厂实现
SessionFactory工厂=SessionFactoryImpl.newInstance();
Map参数=new HashMap();
//用户凭据
parameter.put(SessionParameter.USER,);
parameter.put(SessionParameter.PASSWORD,);
//连接设置
parameter.put(SessionParameter.ATOMPUB_URL,“http://127.0.0.1:8080/alfresco/api/-默认值-/public/cmis/versions/1.1/atom”);
parameter.put(SessionParameter.BINDING_TYPE,BindingType.ATOMPUB.value());
//创建会话
会话会话=factory.getRepositories(参数).get(0).createSession();
Folder root=session.getRootFolder();
//性质
映射属性=新的HashMap();
properties.put(PropertyIds.OBJECT_TYPE_ID,“cmis:folder”);
property.put(propertyId.NAME,“MyAlfrescoFolder”);
//创建文件夹
文件夹父项=root.createFolder(属性);
String name=“NewTextFile.txt”;
//性质
Map properties2=新的HashMap();
properties2.put(PropertyIds.OBJECT_TYPE_ID,“cmis:document”);
properties2.put(PropertyIds.NAME,NAME);
//内容
byte[]content=“你好,帕特尔!”.getBytes();
InputStream=新的ByteArrayInputStream(内容);
ContentStream ContentStream=new ContentStreamImpl(名称,biginger.valueOf(content.length),“text/plain”,stream);
//创建主要版本
Document newDoc=parent.createDocument(properties2,contentStream,VersioningState.MAJOR);
System.out.println(“完成”);
}
}

您好,请不要介意我的英语,在web服务库的帮助下,我们可以用java连接到alfresco。如果可能,您可以找到示例代码,避免使用这种方法,而是使用我发布的CMIS方法。我们不再积极开发旧的web服务API,这就是您在本例中使用的。如果您想使用web服务s而不是AtomPub,您可以将Java示例中的BindingType常量更改为“parameter.put(SessionParameter.BINDING_TYPE,BindingType.WEBSERVICES.value())
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();
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.");

  }

}