Downloading xlsx or png from Jira via API creates corrupted file

David Whitlock May 10, 2023
I am using the Jira api to pull down attachments from our Jira Server instance. I am finding the files I pull down and save are corrupted if they are anything but txt files.  
Below is my code fragment that works for pulling down txt files:
String url = "https://" + serverName + "/rest/api/2/attachment/" + attId;
String response = executeGet(url, false);
if (response != null){
JsonUtil ju = new JsonUtil(response, false);
org.json.simple.JSONObject attachmentInfo = ju.getJsonMessage();
String mimeType = (String)attachmentInfo.get("mimeType");
String contentUrl = (String)attachmentInfo.get("content");
String filename = (String)attachmentInfo.get("filename");
try {
URL fileUrl = new URL(contentUrl);
try (InputStream in = fileUrl.openStream()) {
Files.copy(in, Paths.get(filename));
}
} catch (Exception e) {
logger.error("Unable to download file from " + contentUrl);
}
}
I have used the File.copy before to pull down pngs from the other sites with no problem. Any advice would be great.

2 answers

1 accepted

0 votes
Answer accepted
David Whitlock May 11, 2023

I was able to resolve the issue, I changed to code to use URLConnection instead of HTTPConnection and that worked

0 votes
Dan Breyen
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.
May 10, 2023

 Hi David, I looked in Atlassian's JIRA site and didn't find anything specific to this issue (either open or closed).  In the future, you can search their site for issues at jira.atlassian.com.  If you can replicate it(and it sounds like you can), I would enter a support ticket with atlassian at support.atlassian.com and see what they say.  I know it's server, so I'm not sure if they'll fix it or not.  They also might know of a version that there is a fix in, as I see you're not on the latest code.

David Whitlock May 11, 2023

Thanks for the advice, I have done so, I typically double up my requests bc the Community is usually quicker than support.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.20.10
TAGS
AUG Leaders

Atlassian Community Events