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

Dmitrii January 27, 2021

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
Dmitrii February 16, 2021

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
Ravi Sagar _Sparxsys_
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.
January 27, 2021

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 

Dmitrii January 27, 2021

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.

Dmitrii January 27, 2021

@Ravi Sagar _Sparxsys_ 

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