Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to download attachments for issue via jira-rest-java-client-core?

AVsapelkin
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 30, 2018
I need to download all attachments for specified issue in jira. 
This code downloads file, but it's corrupted.
Can anyone help, how to correctly work with attachments?


import com.atlassian.jira.rest.client.api.JiraRestClient
import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory
import java.io.FileOutputStream

import java.net.URI
import java.nio.channels.Channels
import java.io.BufferedInputStream
import java.io.IOException
import java.net.URL


@Throws(IOException::class)
private fun downloadUsingStream(urlStr: String, file: String) {
val url = URL(urlStr)
val bis = BufferedInputStream(url.openStream())
val fis = FileOutputStream(file)
val buffer = ByteArray(1024)
var count = 0

while (bis.read(buffer, 0, 1024) != -1) {
fis.write(buffer, 0, buffer.size)
}
fis.close()
bis.close()
}

fun main(args: Array<String>) {
val jiraURI = URI("http://jira/")

val factory = AsynchronousJiraRestClientFactory()

val client: JiraRestClient = factory.createWithBasicHttpAuthentication(jiraURI,"login","password")
val issueClient = client.issueClient

client.use {
issueClient.getIssue("attachment").claim().attachments.forEach {attachment->


val website = attachment.contentUri.toURL()
downloadUsingStream(website.toString(),attachment.filename)

}
}




}

1 answer

0 votes
蒋雨辰
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 7, 2018

Thank god, finally I find a solution in Java.

 

JiraRestClient client = factory.createWithBasicHttpAuthentication(jiraURI,"login","password")

//cast JiraRestClient to AsynchronousIssueRestClient
AsynchronousIssueRestClient issueClient=(AsynchronousIssueRestClient) client.getIssueClient();

URL url=new URL("your attachment url");
Promise<InputStream> attachment=asynchronousIssueRestClient.getAttachment(url);
InputStream inputStream=attachment.get();

//then you can do whatever you want with that inputStream
Chirag Taraviya
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 6, 2018

@蒋雨辰Can you please suggest anything in php for this issue? Or just basic steps with jira url.

Suggest an answer

Log in or Sign up to answer