JAVA upload attachment to JIRA Issue via REST API by HttpURLConnection method? Edited

Hi everyone,

can help me anyone?

I need upload image or other files to JIRA by REST API, but i cant use HttpPost method, because with this method i have problem with company proxy. I tryed this:

public void AddAttachment(String DefectId) throws IOException, JSONException {

String CHARSET = "UTF-8";
BufferedReader br = null;

Authorization();

String path = "C:\\somefile.txt";
File file = new File(path);

URL url = new URL(myServer + "/rest/api/2/issue/" + DefectId + "/attachments");
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy", 8080));
HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Cookie", cookieValue);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("X-Atlassian-Token", "nocheck");

System.out.println(conn.getResponseCode());
System.out.println(conn.getResponseMessage());

OutputStream outputStream = conn.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, CHARSET), true);
try ( FileInputStream inputStream = new FileInputStream(file);) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
}
writer.flush();


if (200 <= conn.getResponseCode() && conn.getResponseCode() <= 299) {
br = new BufferedReader(new InputStreamReader(conn.getInputStream(),
"UTF-8"));
} else {
br = new BufferedReader(new InputStreamReader(conn.getErrorStream(),
"UTF-8"));
}

String lineOfResponse = "";
StringBuilder strBuild = new StringBuilder();

while ((lineOfResponse = br.readLine()) != null) {
strBuild.append(lineOfResponse);
}

String response = strBuild.toString();

System.out.println(response);

conn.disconnect();
br.close();


}

 I have problem how a can id file(image or document) to output stream.

Authorization(by Cookies) i have in other methods and working fine.

0 answers

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
Julia Dillon
Posted Apr 17, 2018 in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

799 views 2 19
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