栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用Java将文件传递给Jenkins

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用Java将文件传递给Jenkins

这是一个可运行的类。使用apache-httpclient(4.5.1)和相关的jar。关键是将/ build / URL与MultiPart
Form提交一起使用。此处描述了远程API

package my;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.ContentType;import org.apache.http.entity.mime.FormBodyPartBuilder;import org.apache.http.entity.mime.HttpMultipartMode;import org.apache.http.entity.mime.MultipartEntityBuilder;import org.apache.http.entity.mime.content.FileBody;import org.apache.http.entity.mime.content.StringBody;import org.apache.http.impl.client.HttpClientBuilder;import java.io.File;import java.io.IOException;class JenkinsClientExample {    void helloJenkins() throws IOException {        String server = "localhost";        String jenkinsHost = "http://" + server + ":8080";        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();        HttpClient httpClient = httpClientBuilder.build();        String payLoad="{ "parameter": [{"name":"FILE1_PARAM","file":"file0"}, {"name":"FILE2_PARAM","file":"file1"},{"name":"STRING_PARAM", "value":"2014"}, " +     "{"name":"BOOLEAN_PARAM", "value":"TRUE"}  ] }";        File file = new File("c:/dummy.txt");        File file2 = new File("c:/another.txt");        FormBodyPartBuilder formBodyPartBuilder3 = FormBodyPartBuilder.create("file0", new FileBody(file, ContentType.TEXT_PLAIN));        FormBodyPartBuilder formBodyPartBuilder4 = FormBodyPartBuilder.create("file1", new FileBody(file2, ContentType.TEXT_PLAIN));        FormBodyPartBuilder formBodyPartBuilder1 = FormBodyPartBuilder.create("json", new StringBody(payLoad, ContentType.TEXT_PLAIN));        HttpEntity entity = MultipartEntityBuilder     .create()     .addPart(formBodyPartBuilder3.build())     .addPart(formBodyPartBuilder4.build())     .addPart(formBodyPartBuilder1.build())     .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)     .build();        //must be the build URL not buildWithParameters        HttpPost httpPost = new HttpPost(jenkinsHost + "/job/fake.UpdateCQ_VersionFixed/build");         httpPost.setEntity(entity);        HttpResponse response = httpClient.execute(httpPost);        HttpEntity result = response.getEntity();        System.out.println(result);        System.out.println(response.toString());    }    public static void main(String[] args) {        try { new JenkinsClientExample().helloJenkins();        } catch (IOException e) { e.printStackTrace();        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/406617.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号