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
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();
}
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 ...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @sridhar,
Did you get the solution, then please share.
Thanks,
Vikash
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.