【问题标题】:Uploading jar to nexus with Spring RestTemplate使用 Spring RestTemplate 将 jar 上传到 nexus
【发布时间】:2015-07-26 15:30:23
【问题描述】:

我想使用 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。 有谁知道我如何通过我传入的“文件”来成功完成这项工作?

在另一个问题 (FileUpload isFormField() returning true when submitting file) 中,答案表明我需要一个名称字段,或者在我的情况下,“类型”没有通过。 如果是这种情况,是否可以在发出发布请求时指定这些?

【问题讨论】:

    标签: spring nexus resttemplate apache-commons-fileupload


    【解决方案1】:

    我认为目前这对于 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>
    

    【讨论】:

      猜你喜欢
      • 2013-05-31
      • 2012-04-24
      • 2016-11-30
      • 1970-01-01
      • 2021-02-26
      • 1970-01-01
      • 2015-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多