我正在学习CMIS,并遇到了类似于以下内容的代码,该代码使用CMIS创建文档。我想使用 CMIS 的 createDocument 方法上传存储在本地计算机文件夹中的文件。我怎样才能做到这一点?
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);
有一种方便的方法可以从文件创建ContentStream
对象。
请参阅: https://chemistry.apache.org/java/javadoc/org/apache/chemistry/opencmis/client/util/ContentStreamUtils.html#createFileContentStream-java.io.File-
另请参阅: https://chemistry.apache.org/docs/cmis-samples/samples/content/index.html#content-streams
我已经测试了这种方法,它对我有用
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);
}
}
你也可以在如何与Alfresco仓库连接(简单地说是get会话方法(中阅读更多内容。
另外不要忘记,在这种方法中,每件事都是静态的(文件名路径......
希望对您有所帮助。