/src/test/java/com/eastrobot/doc/web/IndexControllerTests.java

https://github.com/ekoz/kbase-doc · Java · 81 lines · 62 code · 9 blank · 10 comment · 3 complexity · 576a62734bb02a614cac6aa20d82ee20 MD5 · raw file

  1. /*
  2. * Power by www.xiaoi.com
  3. */
  4. package com.eastrobot.doc.web;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import org.apache.http.HttpEntity;
  8. import org.apache.http.client.ClientProtocolException;
  9. import org.apache.http.client.methods.CloseableHttpResponse;
  10. import org.apache.http.client.methods.HttpPost;
  11. import org.apache.http.entity.ContentType;
  12. import org.apache.http.entity.mime.MultipartEntityBuilder;
  13. import org.apache.http.entity.mime.content.FileBody;
  14. import org.apache.http.entity.mime.content.StringBody;
  15. import org.apache.http.impl.client.CloseableHttpClient;
  16. import org.apache.http.impl.client.HttpClients;
  17. import org.apache.http.util.EntityUtils;
  18. /**
  19. * @author <a href="mailto:eko.z@outlook.com">eko.zhan</a>
  20. * @date 2017年8月19日 下午8:47:29
  21. * @version 1.0
  22. */
  23. public class IndexControllerTests implements Runnable{
  24. public static void main(String[] args) throws InterruptedException {
  25. int interval = 1;
  26. IndexControllerTests indexControllerTests = new IndexControllerTests();
  27. for (int i=0; i<20; i++){
  28. indexControllerTests.run();
  29. System.out.println("当前线程[" + Thread.currentThread().getName() + "]停顿" + interval + "秒");
  30. Thread.currentThread().sleep(1000*interval);
  31. }
  32. }
  33. public void run() {
  34. CloseableHttpClient httpclient = HttpClients.createDefault();
  35. try {
  36. HttpPost httppost = new HttpPost("http://localhost:8080" +
  37. "/kbase-doc/index/uploadData");
  38. FileBody bin = new FileBody(new File("E:\\ConvertTester\\myhot\\en-acc.doc"));
  39. StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);
  40. HttpEntity reqEntity = MultipartEntityBuilder.create()
  41. .addPart("uploadFile", bin)
  42. .addPart("comment", comment)
  43. .build();
  44. httppost.setEntity(reqEntity);
  45. System.out.println("executing request " + httppost.getRequestLine());
  46. CloseableHttpResponse response = httpclient.execute(httppost);
  47. try {
  48. System.out.println("----------------------------------------");
  49. System.out.println(response.getStatusLine());
  50. HttpEntity resEntity = response.getEntity();
  51. if (resEntity != null) {
  52. System.out.println("Response content length: " + resEntity.getContentLength());
  53. }
  54. EntityUtils.consume(resEntity);
  55. } finally {
  56. response.close();
  57. }
  58. } catch (ClientProtocolException e) {
  59. // TODO Auto-generated catch block
  60. e.printStackTrace();
  61. } catch (IOException e) {
  62. // TODO Auto-generated catch block
  63. e.printStackTrace();
  64. } finally {
  65. try {
  66. httpclient.close();
  67. } catch (IOException e) {
  68. e.printStackTrace();
  69. }
  70. }
  71. }
  72. }