2017-08-02 60 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的獲取會話方法)。

另外不要忘記,在這種方法中,每件事情都是靜態的(文件名稱爲路徑...)。

希望能幫到你。

相關問題