Using Java, how can I duplicate an attachment within a page (changing the filename)

I feel like I have tried almost every combination of Attachment, AttachmentManager, and related calls in order to try to get a second copy of my attachment on the same page (although with a new file name). I keep running into unhelpful error messages, or worse, none at all. Is there a sample around that can show me how to do this? I spent all afternoon on this, and it doesn't seem like it should be that difficult.

Thank you!

1 answer

I finally figured out the combination to get this done. You have to do things in a very particular order.

public void copyAttachmentToSamePage(final long pageId, final String sourceAttachmentName, final String sourceAttachmentName) {
  ContentEntityObject page = (ContentEntityObject)contentEntityManager.getById(pageId).getLatestVersion()
  final Attachment sourceAttachment = attachmentManager.getAttachment(page, sourceAttachmentName);
  Attachment targetAttachment = new Attachment();
  targetAttachment.setMediaType(sourceAttachment.getMediaType());
  targetAttachment.setFileSize(sourceAttachment.getFileSize());
  targetAttachment.setFileName(targetAttachmentName);
  targetAttachment.setTitle(targetAttachmentName);
  targetPage.addAttachment(targetAttachment);
  attachmentManager.saveAttachment(targetAttachment, null, attachmentManager.getAttachmentData(sourceAttachment));
}

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

296 views 11 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