2017-08-02 61 views
0

我正在学习CMIS并遇到类似于以下的代码,它使用CMIS创建文档。我想使用CMIS的createDocument方法来上传存储在本地机器文件夹中的文件。我怎样才能做到这一点?如何使用CMIS上传文档?

Folder parent = .... 

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); 
+0

“FileInputStream”有什么问题? – Gagravarr

回答

0

我测试过这种方法,它的工作对我来说

public static void upload(String serverUrl, String username, String password, String cheminFichierSource, String nomDestination, String cheminFichierDestination, String extentionFichier) { 
try {  
    Session session = getSession(serverUrl, username, password); 
    Folder root = getFolderByPath(serverUrl, username, password, cheminFichierDestination); 

    Map<String, Object> properties = new HashMap<String, Object>(); 
    properties.put(PropertyIds.OBJECT_TYPE_ID, BaseTypeId.CMIS_DOCUMENT.value()); 
    String name = nomDestination + "." + extentionFichier; 

    System.out.println("name:" + name); 
    properties.put(PropertyIds.NAME, name); 
    List<Ace> addAces = new LinkedList<Ace>(); 
    List<Ace> removeAces = new LinkedList<Ace>(); 
    List<Policy> policies = new LinkedList<Policy>(); 
    File file = new File(cheminFichierSource); 
    ContentStream contentStream = new ContentStreamImpl("content." + extentionFichier, BigInteger.valueOf(file.length()), 
new MimetypesFileTypeMap().getContentType(file), new FileInputStream(file)); 
    Document dc = root.createDocument(properties, contentStream, VersioningState.MAJOR, policies, addAces, removeAces, session.getDefaultContext()); 
//idnewdoc=dc.getId(); 

    } catch (FileNotFoundException e) { 
    JOptionPane.showMessageDialog(null, e.getMessage(), "ERREUR Consultation! ", JOptionPane.ERROR_MESSAGE); 
    } 
    } 

您还可以阅读更多How to get connected with Alfresco repository(simplly的获取会话方法)。

另外不要忘记,在这种方法中,每件事情都是静态的(文件名称为路径...)。

希望能帮到你。

相关问题