Alexey
Alexey

Reputation: 301

How to update content of the file in Alfresco?

My java-backed webscript copies file in a repository to a temp folder and edits it for my needs. During its work a new content is generated and it must be written to a created temporary file.

But there is a problem: the first nor the second code below does not updates file's content.

ContentWriter contentWriter = this.contentService.getWriter(tempFile,
                               ContentModel.PROP_CONTENT, true);
contentWriter.putContent(content);

And the second:

`
WritableByteChannel byteChannel = contentWriter.getWritableChannel();
ByteBuffer buffer = ByteBuffer.wrap(content.getBytes());
byteChannel.write(buffer);
byteChannel.close();
`

How to update file's content?

Upvotes: 0

Views: 3145

Answers (1)

Tahir Malik
Tahir Malik

Reputation: 6643

This works for me:

ContentWriter contentWriter = contentService.getWriter(noderef, ContentModel.PROP_CONTENT, true);
        contentWriter.setMimetype("text/csv");
        FileChannel fileChannel = contentWriter.getFileChannel(false);
        ByteBuffer bf = ByteBuffer.wrap(logLine.getBytes());
        try {
            fileChannel.position(contentWriter.getSize());
            fileChannel.write(bf);
            fileChannel.force(false);
            fileChannel.close();
        } catch (IOException e){
            e.printStackTrace();
        }

I'm appending a line to an existing file, so logLine is the appending string.

Upvotes: 3

Related Questions