我正在尝试在Jira上传附件。我使用基本身份验证成功完成了此操作,但当我尝试使用OAuth上传时,我得到了一个200状态的空响应。请帮帮我。
public String uploadFile(List list) throws IOException {
String name="admin"+":"+"xyz@123";
String encodeValue=Base64.getEncoder().encodeToString(name.getBytes());
System.out.println("base64 code :"+encodeValue);
InputStream stream = (org.glassfish.grizzly.utils.BufferInputStream) list.get(0);
System.out.println(stream);
byte [] bytes=IOUtils.toByteArray(stream);
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost postRequest = new HttpPost("https://jirademotest.atlassian.net/rest/api/2/issue/JIR-1/attachments?oauth_nonce=****&oauth_token=*****&oauth_timestamp=***&oauth_signature_method=RSA-SHA1&oauth_consumer_key=hardcoded-consumer&oauth_version=1.0&oauth_signature=***");
BASE64Encoder base=new BASE64Encoder();
String encoding = base.encode ("admin:Sarasu@10".getBytes());
//postRequest.setHeader("Authorization", "Basic " + encoding);
postRequest.setHeader("X-Atlassian-Token","nocheck");
MultipartEntityBuilder entity=MultipartEntityBuilder.create();
entity.addPart("file", new InputStreamBody(stream, (String) list.get(1)));
postRequest.setEntity( entity.build());
HttpResponse response = httpClient.execute(postRequest);
HttpEntity entity1 = response.getEntity();
String responseString = EntityUtils.toString(entity1, "UTF-8");
//System.out.println(responseString);
return responseString;
答案 0 :(得分:0)
以下代码有效:
import java.io.File;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
public class AddAttachmentToIssue {
public static void main (String args[]) throws IOException
{
AddAttachmentToIssue u = new AddAttachmentToIssue();
u.addAttachmentToIssue("<issue-id>", "<path>");
}
@SuppressWarnings("deprecation")
public void addAttachmentToIssue(String issueKey, String path){
String auth = new String(org.apache.commons.codec.binary.Base64.encodeBase64(("username"+":"+"password").getBytes()));
String baseURL="https://<jira url>/rest/api/latest/";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(baseURL+"issue/"+issueKey+"/attachments");
httppost.setHeader("X-Atlassian-Token", "nocheck");
httppost.setHeader("Authorization", "Basic "+auth);
System.out.println(httppost);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
File fileToUpload = new File(path);
FileBody fileBody = new FileBody(fileToUpload);
System.out.print(fileBody);
entity.addPart("file", fileBody);
httppost.setEntity(entity);
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
System.out.print(response.toString());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
HttpEntity result = response.getEntity();
if(response.getStatusLine().getStatusCode() == 200)
System.out.println(response);
else
System.out.println("false");
}
}
它给出以下响应:
HTTP/1.1 200 [Date: Mon, 07 Jan 2019 07:01:51 GMT, Server: Apache/2.4.12 (Unix) OpenSSL/1.0.2a, X-AREQUESTID: 121x26602402x1, X-ANODEID: <node -name>, X-XSS-Protection: 1; mode=block, X-Content-Type-Options: nosniff, X-Frame-Options: SAMEORIGIN, Content-Security-Policy: frame-ancestors 'self', X-ASEN: SEN-5310406, X-Seraph-LoginReason: OK, X-ASESSIONID: 5l5m0z, X-AUSERNAME: <username>, Cache-Control: no-cache, no-store, no-transform, Content-Type: application/json;charset=UTF-8, Vary: Accept-Encoding, Set-Cookie: JSESSIONID=<session-id>;path=/jira;Secure;HttpOnly, Set-Cookie: atlassian.xsrf.token=BYHJ-QYVA-BYSV-X6QN|7d0612099e3bb2647d98316c60127e0c9bd9fdee|lin;path=/jira;Secure, Keep-Alive: timeout=5, max=100, Connection: Keep-Alive, Transfer-Encoding: chunked] org.apache.http.conn.BasicManagedEntity@1b485ba