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 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) {


} catch (IOException e) {


}catch (Exception ex){



Hi @sridhar,

Did you get the solution, then please share.



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

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

            // do something with the image here

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

            Attachment oldAttachment = attachment.copy();

            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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Kesha Thillainayagam
Posted Apr 13, 2018 in Confluence

We want to hear how your non-technical teams are using Confluence!

Hi Community! Kesha (kay-sha) from the Confluence marketing team here! Can you share stories with us on how your non-technical (think Marketing, Sales, HR, legal, etc.) teams are using Confluen...

374 views 20 10
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