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 widget could not be displayed.

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();

}

This widget could not be displayed.

Hi @sridhar,

Did you get the solution, then please share.

Thanks,

Vikash

This widget could not be displayed.

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
Community showcase
Posted Monday in Confluence

Why start from scratch? Introducing four new templates for Confluence Cloud

Hi my Community friends!  For those who don't know me, I'm a product marketer on the Confluence Cloud team - nice to meet you! For those of you who do, you know that I've been all up in your Co...

342 views 2 5
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