1

I am trying to upload a file to the DocLibrary folder using the below method:

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());
    }

The documentation suggest this:

curl -utest:test -X POST host:port/alfresco/api/-default-/public/alfresco/versions/1/nodes/12341234-2344-2344-43243242334/children -F filedata=@test.txt

Using the method postTheDocument() creates the empty document. But when I try below request:

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\"}}";

I get the following Exception:

 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)]
    

What I am doing wrong here or what I am missing?

Ajay Kumar
  • 2,906
  • 3
  • 23
  • 46
  • Did you check that the data payload you're passing in your Java code will actually work with curl? I often use Python Requests module to send HTTP client commands and there is a module that will print out the exact code one would use with curl. There probably is something like that for Java but I'm not sure. – Harlin Oct 07 '20 at 02:03

2 Answers2

1

I believe the common approach is to create the node in Alfresco, then send the file to the node in a multipart upload to "http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-root-/children" (for an example of multipart upload see: https://www.baeldung.com/httpclient-multipart-upload).

To get the node id from the created node:

    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();
        }

    }        
Curtis
  • 548
  • 3
  • 13
  • I tried em all but nothing worked out. I contacted Alfresco support and they provided the Apache Http Client approach and that worked fine. I.e. using REST API I was successfully able to upload File as well as CUSTOM Metadata both in one shot rather than creating Node first and then loading the File (content) thereafter. Thanks for help though @Curtis. Will try it again and will update you on this. – Ajay Kumar Oct 15 '20 at 01:40
  • Hi can you post your solution here, Even my requirement is same – kavyansh Oct 20 '22 at 18:10
1

Posting the solution that worked for me. Its not RestTemplate based but it works. This is the solution I got from Alfresco Support folks and it works perfectly well (refer the inline comments):

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
}
Ajay Kumar
  • 2,906
  • 3
  • 23
  • 46