Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Not able to save versions of attachments using confluence API

Sridhar Nimmagadda
Contributor
August 1, 2012

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

1 vote
Sridhar Nimmagadda
Contributor
December 30, 2013

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

}

0 votes
AndreasS
Contributor
January 20, 2014

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 ...

0 votes
Vikash Kumar
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 3, 2013

Hi @sridhar,

Did you get the solution, then please share.

Thanks,

Vikash

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events