目前我正在寻找一种在Hudson的以下配置中上传文件+字段的工作方法。当前的问题是 Hudson 总是抱怨应该提交的表单..(请参阅本文后面的异常)。但根据我阅读的文档,它应该像下面的 Java 代码片段一样工作......
HttpPost httppost = new HttpPost(triggerJobUrl);
FileBody fileBody = new FileBody(releaseProperties);
StringBody stringBody = new StringBody(svnURL.toString());
MultipartEntity mentity = new MultipartEntity();
mentity.addPart("trunk/release.properties", fileBody);
mentity.addPart("SVNURL", stringBody);
httppost.setEntity(mentity);
HttpResponse response = null;
try {
response = httpClient.execute(httppost);
} catch (ClientProtocolException e) {
throw new HudsonException("http protocol error.", e);
} catch (IOException e) {
throw new HudsonException("connection aborted.", e);
}
if (response.getStatusLine().getStatusCode() != 200) {
throw new HudsonException("Unexpected status code received " + response.getStatusLine().getStatusCode());
}
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
try {
resEntity.consumeContent();
} catch (IOException e) {
throw new HudsonException(
"if an I/O error occurs. This indicates that connection keep-alive is not possible.", e);
}
}
我当前的 Maven 依赖项如下:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.0.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.0.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.0.3</version>
<type>jar</type>
<scope>compile</scope>
例外是:
java.lang.Error: This page expects a form submission
at org.kohsuke.stapler.RequestImpl.getSubmittedForm(RequestImpl.java:769)
at hudson.model.ParametersDefinitionProperty._doBuild(ParametersDefinit