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)答案表明,我需要一个名称字段,或者在我的情况下,“类型”不通过。 如果是这种情况,是否可以在发出帖子请求时指定这些内容?