2015-05-14 36 views
0

我想用Spring的RestTemplate将jar上传到nexus。我找到了关于如何使用curl here的方法,这很好。但是,我完全无法将其转换为RestTemplate调用。这里是我的代码:用Spring RestTemplate将jar上传到nexus

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通用FileUpload。 有没有人知道我怎么能得到这个成功与我的“文件”,我通过?

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

回答

0

我在想RestTemplate目前是不可能的。我选择使用Apache的HttpComponents,它的工作非常出色。下面是生成的代码:

public void uploadToNexus(String version, File myJar, String artifactId) 
{ 
    try(CloseableHttpClient httpClient = HttpClients.createDefault()) 
    { 
     HttpPost httpPost = new HttpPost("http://admin:[email protected]: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>