Reputation: 14435
I'm trying to upload a file into my Alfresco repository using Apache Chemistry OpenCMIS. The file is created and the properties are correct but there is no content, the file is 0 bytes. I've double checked and there is nothing wrong with the source file. Here's my Java code:
File content = new File(somepath);
try{
String mimeType = new MimetypesFileTypeMap().getContentType(content);
logger.debug("mimetype: " + mimeType);
logger.debug("file: " + content.getAbsolutePath());
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyIds.OBJECT_TYPE_ID, BaseTypeId.CMIS_DOCUMENT.value());
properties.put(PropertyIds.NAME, content.getName());
FileInputStream fis = new FileInputStream(content);
DataInputStream dis = new DataInputStream(fis);
byte[] bytes = new byte[(int) content.length()];
dis.readFully(bytes);
ContentStream cs = new ContentStreamImpl(content.getName(), BigInteger.valueOf(bytes.length), mimeType, dis);
Folder folder = (Folder) session.getObjectByPath("/myfolder");
Document doc = folder.createDocument(properties, cs, VersioningState.MAJOR);
return doc.getId();
}catch(CmisBaseException e){
logger.error("error uploading file: "+ e.getMessage(), e);
}
There is no exception catched.
Upvotes: 2
Views: 2370
Reputation: 358
I was looking for an example about this, and found your question, the problem with the original if I am not mistaken is that you are reading the buffer beforehand so the pointer is at the end of it when you pass it to the content stream, I am making a little class just to test some functionality I want to implement in a program later and this block works with your initial approach.
File content = new File("C:\\\\asdf.asdf");
try{
String mimeType = new MimetypesFileTypeMap().getContentType(content);
System.out.println("mimetype: " + mimeType);
System.out.println("file: " + content.getAbsolutePath());
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyIds.OBJECT_TYPE_ID,BaseTypeId.CMIS_DOCUMENT.value()+",P:cm:titled");
properties.put("cm:description", "upload desde código");
properties.put(PropertyIds.NAME, content.getName());
FileInputStream fis = new FileInputStream(content);
DataInputStream dis = new DataInputStream(fis);
ContentStream cs = new ContentStreamImpl(content.getName(),BigInteger.valueOf(content.length()), mimeType, dis);
Document doc = newFolder.createDocument(properties, cs, VersioningState.MAJOR);
}catch(CmisBaseException ex){
Logger.getLogger(CMISTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (FileNotFoundException ex) {
Logger.getLogger(CMISTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(CMISTest.class.getName()).log(Level.SEVERE, null, ex);
}
Upvotes: 1
Reputation: 2037
I think there is problem with the content stream you are passing.
Try to replace you code with this
String docText = "This is a sample document";
byte[] content = docText.getBytes();
InputStream stream = new ByteArrayInputStream(content);
ContentStream contentStream = getSession().getObjectFactory().createContentStream(filename, Long.valueOf(content.length), "text/plain", stream);
You can gradually change this simple text with content from your new file in next step.
Upvotes: 4