Reputation: 277
I am learning CMIS and have come across code similar to following which creates a document using CMIS. I want to use createDocument method of CMIS to upload a file stored in a folder in my local machine. How can I achieve that?
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);
Upvotes: 0
Views: 3747
Reputation: 1796
I've tested this method and it's worked for me
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);
}
}
You can also read more in How to get connected with Alfresco repository (simplly the get session method).
Also don't forget that in this method every thing is static (the file name the path ...).
Hope that helped you.
Upvotes: 0
Reputation: 3235
There is a convenience method to create a ContentStream
object from a file.
See also: https://chemistry.apache.org/docs/cmis-samples/samples/content/index.html#content-streams
Upvotes: 2