Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to get file object by attachment using java/groovy?

Hi all! 
I have a problem with getting file object by attachment of an issue.
I am using Jira 8.6.1.
I have looked similar question and tried all that was written there, but these approaches don't work.
Else I have found AttachmentUtils.class. It is deprecated, but it has getAttachmentFile() method that directly returns File object by attachment. It also hasn't worked. All of my test breaks by exists() method of the File.class (returns 'false' all the time). 
Could you help me with resolve this problem?

2 answers

1 accepted

0 votes
Answer accepted

I have found in what was a mistake. I tried to get file by wrong path. The path in the link of my question isn't universal so didn't work for me.

0 votes

Hi @Dmitrii 

What exactly are you trying to do with the file? You can try this to get the attachments.

def attachments = ComponentAccessor.attachmentManager.getAttachments(issue)
attachments*.getFilesize()
attachments*.getFilename()

Let me know if this helps.

Ravi 

I need file to will write in my zip archive. But I still can't extract file from the attachment.
Zip archive will contain all attachments of the issue.

@Ravi Sagar _Adaptavist_ 

Approach 1


String filePath = PathUtils.joinPaths(ComponentAccessor.getAttachmentPathManager().getDefaultAttachmentPath(), issue.getProjectObject().getKey(), issue.getKey(), attachment.getId().toString());
File file = new File(filePath);

Approach 2


File file = AttachmentUtils.getAttachmentFile(attachment)

All of these approaches return that the file doesn't exist

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you