Not able to save versions of attachments using confluence API

I am trying to add versions of attachment with the below code and doesn't seem to find success

Space sp = spaceManager.getSpace(AP3);

Page p = pageManager.getPage(AP3, Level1);

Attachment attachment = new Attachment(Test123.txt,text/plain, 62 , adding first document - version+1);

attachment.setContent(p);

Attachment oldVersion = attachmentManager.getAttachment(p, Test123.txt);

attachmentManager.saveAttachment(attachment, oldVersion, new FileInputStream(new File(c://Test123.txt)));

Test123.txt was already saved previously

3 answers

This worked for me !!

Attachment previousVersion=null;

try {

previousVersion = (Attachment) newVersion.clone();

} catch (CloneNotSupportedException e) {

// TODO Auto-generated catch block

log.error("cannot clone", e);

}catch (Exception ex){

log.error("unknown exception while attaching versions", ex);

}

newVersion.setFileSize(latestFileSize); //get the new versions filesize and set

try {

attachmentManager.saveAttachment(newVersion, previousVersion, new FileInputStream(new File(latestFilepath)));

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}catch (Exception ex){

ex.printStackTrace();

}

Hi @sridhar,

Did you get the solution, then please share.

Thanks,

Vikash

Instead of clone(), one can also use copy() which throws a RuntimeException instead of a checked exception.

InputStream contentAsStream = attachment.getContentsAsStream();
            BufferedImage image = ImageIO.read(contentAsStream);
            String imageExtension = attachment.getFileExtension();

            // do something with the image here

            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(image, imageExtension, os);

            Attachment oldAttachment = attachment.copy();
            attachment.setFileSize(os.size());

            attachmentManager.saveAttachment(attachment, oldAttachment, new ByteArrayInputStream(os.toByteArray()));

I intentionally skipped the closing of all the streams ...

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Jul 10, 2018 in Confluence

We want to see the templates you've created in Confluence!

Hi Community, Jessica here from the Confluence Product Marketing team!  July’s community challenge is all about sharing pictures  — and as an extension of our first post on what ...

681 views 21 12
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you