使用SpringRESTTemplate将jar上载到nexus

使用SpringRESTTemplate将jar上载到nexus,spring,nexus,resttemplate,apache-commons-fileupload,Spring,Nexus,Resttemplate,Apache Commons Fileupload,我想使用Spring的RestTemplate将jar上传到nexus。我找到了关于如何使用curl的说明,它工作得很好。但是,我完全无法将其转换为RestTemplate调用。这是我的密码: String auth = "admin:admin123"; byte[] encodedAuth = Base64.encodeBase64( auth.getBytes(Charset.forName("US-ASCII")) ); String authHeader = "

我想使用Spring的RestTemplate将jar上传到nexus。我找到了关于如何使用curl的说明,它工作得很好。但是,我完全无法将其转换为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);
FileItem.isFormField来自Apache Commons FileUpload有人知道我如何通过传递的“文件”成功完成此操作吗?


在另一个问题()中,答案表明我需要一个name字段,或者在我的例子中,“type”无法通过如果是这种情况,是否可以在发出post请求时指定这些内容?

我认为这在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>

org.apache.httpcomponents
httpclient
4.4.1
org.apache.httpcomponents
httpime
4.4.1
    <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>