Guest User

Untitled

a guest
Jan 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. import java.io.File;
  2.  
  3. import org.apache.commons.httpclient.HttpClient;
  4. import org.apache.commons.httpclient.HttpStatus;
  5. import org.apache.commons.httpclient.methods.PostMethod;
  6. import org.apache.commons.httpclient.methods.multipart.FilePart;
  7. import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
  8. import org.apache.commons.httpclient.methods.multipart.Part;
  9. import org.apache.commons.httpclient.methods.multipart.StringPart;
  10.  
  11. public class RtCommentSample {
  12.  
  13. public static void main(String[] args) {
  14.  
  15. String targetURL = args[0]+"/REST/1.0/ticket/"+args[1]+"/comment?user=root&pass=password";
  16.  
  17. PostMethod filePost = new PostMethod(targetURL);
  18.  
  19. try {
  20.  
  21. String content = "id: "+args[1]+"\nAction: comment\nText: "+args[2];
  22.  
  23.  
  24. Part[] parts = null;
  25.  
  26. if(args.length > 3){
  27. parts = new Part[2];
  28.  
  29. File targetFile = new File(args[3]);
  30. parts[0] = new FilePart("attachment_1",targetFile.getName(), targetFile);
  31.  
  32. content += "\nAttachment: "+targetFile.getName();
  33.  
  34. }else{
  35. parts = new Part[1];
  36. }
  37.  
  38. System.out.println(content);
  39.  
  40. parts[parts.length -1] = new StringPart("content",content);
  41.  
  42. filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
  43.  
  44. HttpClient client = new HttpClient();
  45. client.getHttpConnectionManager(). getParams().setConnectionTimeout(5000);
  46.  
  47. int status = client.executeMethod(filePost);
  48. if (status == HttpStatus.SC_OK) {
  49. System.out.println("Upload complete, response=" + filePost.getResponseBodyAsString());
  50. } else {
  51. System.out.println("Upload failed, response=" + HttpStatus.getStatusText(status));
  52. }
  53. } catch (Exception ex) {
  54. ex.printStackTrace();
  55. } finally {
  56. filePost.releaseConnection();
  57. }
  58. }
  59. }
Add Comment
Please, Sign In to add comment