0

I am working with Alfresco Share 5.2 and trying to create new node. I introduced new button, and by clicking on that button new excel file will be created and stored inside MyFiles directory.

Everything works fine, but created node has size 0, and I cannot download it. Below is my java code which I used to create node ref from excel file.

HSSFWorkbook document = new HSSFWorkbook();
HSSFSheet sheet = document.createSheet("search-results");
HSSFRow row = sheet.createRow(0);
HSSFRow rowSecond = sheet.createRow(1);
HSSFCell cell = row.createCell(0);
HSSFCell cellWithValue = rowSecond.createCell(0);
cell.setCellValue("Link");
cellWithValue.setCellValue(urlLink);

                            Map<QName, Serializable> props = new HashMap<QName, Serializable>();
                            props.put(ContentModel.PROP_NAME, nameOfFile);
                            ChildAssociationRef childAssoc = 
                            nodeService.createNode(folderSaveResults,
                                    ContentModel.ASSOC_CONTAINS,
                                    ContentModel.ASSOC_CONTAINS,
                                    ContentModel.TYPE_CONTENT,
                                    props);

                            NodeRef node = childAssoc.getChildRef();

                            ContentReader reader = contentService.getReader(node, 
                            ContentModel.PROP_CONTENT);
                            ContentWriter contentWriter =
                                    contentService.getWriter(node, ContentModel.PROP_CONTENT, 
                            true);
                            contentWriter.setMimetype(mimetypeService
                                    .guessMimetype(nodeService.getProperty(node, 
                                     ContentModel.PROP_NAME).toString()));

                            this.behaviourFilter.disableBehaviour(node);

                            if (reader != null) {
                                contentWriter.putContent(reader.getContentInputStream());
                            }

                            this.behaviourFilter.enableBehaviour(node);

                            System.out.println(node);

For some reason size of created node is still 0 bytes.

Mike1988B
  • 57
  • 5

1 Answers1

0

your code for generating xlsx file look's good. Try to replace your last lines:

this.behaviourFilter.disableBehaviour(node);
if (reader != null) 
{          
 contentWriter.putContent(reader.getContentInputStream());
}
this.behaviourFilter.enableBehaviour(node);

With this code:

        FileChannel fileChannel = contentWriter.getFileChannel(false);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            document.write(bos);
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] bytes = bos.toByteArray();

        ByteBuffer bf = ByteBuffer.wrap(bytes);
        try {
            fileChannel.write(bf);
            fileChannel.force(false);
            fileChannel.close();
        } catch (IOException ex) {
        }

I think you dont need use ContentReader reader. This code work in my Alfresco 5.2.

David Dejmal
  • 359
  • 1
  • 8