Advertisement
Guest User

Untitled

a guest
Dec 20th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. try
  2. {
  3. String fileDir = "D:\"; // upload directory
  4. String fileName = "config.xml";
  5. URL url = new URL("http://myjenkins/job/test/config.xml"); // Jenkins URL localhost:8080, job named 'test'
  6.  
  7. String user = "username"; // username
  8. String pass = "password"; // password or API token
  9. String authStr = user +":"+ pass;
  10. String encoding = DatatypeConverter.printBase64Binary(authStr.getBytes("utf-8"));
  11.  
  12. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  13.  
  14. connection.setReadTimeout(10000);
  15. connection.setConnectTimeout(15000);
  16. connection.setRequestMethod("POST");
  17. connection.setUseCaches(false);
  18. connection.setDoInput(true);
  19. connection.setDoOutput(true);
  20. connection.setRequestProperty ("Authorization", "Basic " + encoding);
  21.  
  22. try
  23. {
  24.  
  25. String filePath = "D:\config.xml";
  26. FileInputStream inputStream = new FileInputStream(new File(filePath));
  27. FileReader fr = new FileReader(new File(filePath));
  28. BufferedReader br = new BufferedReader(fr);
  29.  
  30. String sCurrentLine;
  31. System.out.println(sCurrentLine = br.readLine());
  32. OutputStream os = connection.getOutputStream();
  33.  
  34. BufferedWriter writer = new BufferedWriter(
  35. new OutputStreamWriter(os, "UTF-8"));
  36. while ((sCurrentLine = br.readLine()) != null) {
  37. //System.out.println("not going inside!!!!");
  38. writer.write(sCurrentLine);
  39. System.out.println(sCurrentLine);
  40. }
  41. writer.flush();
  42. writer.close();
  43. os.close();
  44. int responseCode=connection.getResponseCode();
  45. System.out.println(responseCode);
  46. }
  47. catch(Exception e)
  48. {
  49. e.printStackTrace();
  50. }
  51.  
  52. }
  53. catch(Exception e)
  54. {
  55. e.printStackTrace();
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement