使用Spring RestTemplate将jar上传到nexus

时间:2015-05-14 20:50:16

标签: spring nexus resttemplate apache-commons-fileupload

我想使用Spring的RestTemplate将jar上传到nexus。我找到了如何使用curl here执行此操作的说明,该工作正常。但是我将它转换为RestTemplate调用完全失败了。这是我的代码:

    String auth = "admin:admin123";
    byte[] encodedAuth = Base64.encodeBase64( auth.getBytes(Charset.forName("US-ASCII")) );
    String authHeader = "Basic " + new String( encodedAuth );

    RestTemplate restTemplate = new RestTemplate();

    LinkedMultiValueMap<String, Object> files = new LinkedMultiValueMap<>();
    files.add("r", "releases");
    files.add("hasPom", "false");
    files.add("e", "jar");
    files.add("g", "com.test.proj");
    files.add("a", "my-artifact");
    files.add("v", "1.0.0");
    files.add("p", "jar");
    files.add("file", new ByteArrayResource(jarBytes));

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    headers.set("Authorization", authHeader);
    HttpEntity<LinkedMultiValueMap<String, Object>> entity = new HttpEntity<>(files, headers);
    String response = restTemplate.postForObject("http://localhost:8081/nexus/service/local/artifact/maven/content", entity, String.class);

这失败了400 Bad Request。查看the source后,看起来我的文件未通过此密钥检查:

 for (FileItem fi : files) {
    if (fi.isFormField()) {
      // parameters are first in "nibble"
      processFormField(request, uploadContext, fi);
    }
    else {
      // a file, this means NO parameters will income anymore
      // we either received all the GAVs as params, or we have a POM to work with (file1)
      ...

FileItem.isFormField来自Apache Commons FileUpload。 有谁知道如何通过我的&#34;文件&#34;我正在传递?

在另一个问题(FileUpload isFormField() returning true when submitting file)中,答案表明我需要一个名字字段,或者在我的情况下,我需要&#34;类型&#34;没有经历过。 如果是这种情况,是否可以在发布帖子请求时指定这些内容?

1 个答案:

答案 0 :(得分:0)

我认为RestTemplate目前无法做到这一点。我选择使用Apache的HttpComponents,它运行得非常好。以下是生成的代码:

public void uploadToNexus(String version, File myJar, String artifactId)
{
    try(CloseableHttpClient httpClient = HttpClients.createDefault())
    {
        HttpPost httpPost = new HttpPost("http://admin:admin123@mydomain:8081/nexus/service/local/artifact/maven/content");

        FileBody jarFileBody = new FileBody(myJar);

        HttpEntity requestEntity = MultipartEntityBuilder.create()
                .addPart("r", new StringBody("releases", ContentType.TEXT_PLAIN))
                .addPart("hasPom", new StringBody("false", ContentType.TEXT_PLAIN))
                .addPart("e", new StringBody("jar", ContentType.TEXT_PLAIN))
                .addPart("g", new StringBody("com.domain.my", ContentType.TEXT_PLAIN))
                .addPart("a", new StringBody("my-artifactId", ContentType.TEXT_PLAIN))
                .addPart("v", new StringBody(version, ContentType.TEXT_PLAIN))
                .addPart("p", new StringBody("jar", ContentType.TEXT_PLAIN))
                .addPart("file", jarFileBody)
                .build();

        httpPost.setEntity(requestEntity);

        try(CloseableHttpResponse response = httpClient.execute(httpPost))
        {
            logger.info("response from nexus: {}", response.toString());
        }
    }
    catch (IOException e)
    {
        throw new RuntimeException("Unable to close the httpClient", e);
    }
}

我需要以下maven依赖项:

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.4.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.4.1</version>
    </dependency>