我正在学习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);
答案 0 :(得分:2)
有一种方便的方法可以从文件中创建ContentStream
对象。
另见: https://chemistry.apache.org/docs/cmis-samples/samples/content/index.html#content-streams
答案 1 :(得分: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中阅读更多内容(简单地说,获取会话方法)。
另外不要忘记在这个方法中,每件事都是静态的(文件名是路径......)。
希望能帮到你。