Java Alfresco-如何在使用RestTemplate进行post时添加/设置文件内容
我正在尝试使用以下方法将文件上载到DocLibrary文件夹:Java Alfresco-如何在使用RestTemplate进行post时添加/设置文件内容,java,rest,alfresco,alfresco-share,spring-resttemplate,Java,Rest,Alfresco,Alfresco Share,Spring Resttemplate,我正在尝试使用以下方法将文件上载到DocLibrary文件夹: private static void postTheDocument() { final String restENDPoint = "http://servername:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/48eea6b2-fe9b-4cd2-8270-5caa35d7e8dc/children";
private static void postTheDocument() {
final String restENDPoint = "http://servername:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/48eea6b2-fe9b-4cd2-8270-5caa35d7e8dc/children";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setBasicAuth("admin", "admin");
String requestJson = "{\"name\": \"TestName123s.doc\",\"nodeType\": \"hr:HR_Type\",\"properties\":{\"cm:title\":\"New Test title123\",\"hr:emp_no\":\"123456\",\"hr:lname\":\"Last_Name1\",\"hr:fname\":\"First_Name1\"}}";
HttpEntity<String> entity = new HttpEntity<String>(requestJson, headers);
HttpEntity<String> response = restTemplate.exchange(restENDPoint, HttpMethod.POST, entity,
String.class);
System.out.println(response.getBody().toString());
}
使用postTheDocument()方法创建空文档。但当我尝试以下请求时:
String requestJson = "{\"filedata\": \"@Z:/05test/YYYYest11qq890.pdf\",\"name\": \"TestName123s.doc\",\"nodeType\": \"hr:HR_Type\",\"properties\":{\"cm:title\":\"New Test title123\",\"hr:emp_no\":\"123456\",\"hr:lname\":\"Last_Name1\",\"hr:fname\":\"First_Name1\"}}";
我得到以下例外情况:
Exception in thread "main" org.springframework.web.client.HttpClientErrorException$BadRequest: 400 : [{"error":{"errorKey":"Could not read content from HTTP request body: Unrecognized field \"filedata\" (class org.alfresco.rest.api.model.Node), not marked as ignorable (36 known properties: \"modifiedB... (1969 bytes)]
我在这里做错了什么或者我缺少了什么?我认为常用的方法是在露天创建节点,然后将文件以多部分上传的方式发送到节点http://localhost:8080/alfresco/api/-默认-/public/alfresco/versions/1/nodes/-root-/children”(有关多部分上载的示例,请参阅:) 要从创建的节点获取节点id,请执行以下操作:
if (response != null) {
try {
JSONObject result;
result = new JSONObject(response.getBody());
System.out.println(result.toString());
JSONObject oj = result.getJSONObject("entry");
String id = (String) oj.get("id");
System.out.println(id);
fileupload(file_upload,id);
} catch (JSONException e2) {
e2.printStackTrace();
}
}
发布对我有效的解决方案。它不是基于RestTemplate的,但可以工作。这是我从Alfresco支持人员那里得到的解决方案,效果非常好(请参阅内联注释):
您是否检查了您在Java代码中传递的数据负载是否能够与curl一起工作?我经常使用Python请求模块来发送HTTP客户机命令,并且有一个模块将打印出与curl完全相同的代码。Java可能也有类似的功能,但我不确定。我尝试了所有这些功能,但没有任何效果。我联系了Alfresco支持部门,他们提供了ApacheHTTP客户端方法,效果很好。也就是说,使用RESTAPI,我能够一次性成功地上传文件和自定义元数据,而不是先创建节点,然后再加载文件(内容)。谢谢你的帮助,柯蒂斯。将重试并向您更新此信息。
if (response != null) {
try {
JSONObject result;
result = new JSONObject(response.getBody());
System.out.println(result.toString());
JSONObject oj = result.getJSONObject("entry");
String id = (String) oj.get("id");
System.out.println(id);
fileupload(file_upload,id);
} catch (JSONException e2) {
e2.printStackTrace();
}
}
private static void postFileAndMetadataToAlfresco() throws IOException, AuthenticationException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://someservername:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/12341234-1234-1234-1234-123412341234/children");
UsernamePasswordCredentials creds = new UsernamePasswordCredentials ("admin","adminpswd");
httpPost.addHeader (new BasicScheme().authenticate(creds,httpPost, null));
File payload = new File ("/path/to/my/file.pdf");
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); // Entity builder
builder.addPart("filedata", new FileBody(payload)); // this is where I was struggling
builder.addTextBody ("name", "thenamegoeshere");
builder.addTextBody ("foo", "foo");
builder.addTextBody ("bar", "bar");
builder.addTextBody ("description", "descriptiongoeshere");
builder.addTextBody ("overwrite", "true");
HttpEntity entity = builder.build();
httpPost.setHeader("Accept","application/json");
httpPost.setEntity(entity);
CloseableHttpResponse response = httpClient.execute(httpPost); // Post the request and get response
System.out.println(response.toString()); // Response print to console
httpClient.close(); // close the client
}