12-07-2022 10:58 AM
Hi, I'm using the acs-deployment 7.2.0 (the docker deployment) and I would like to upload a file using the REST APIs with Java HttpURLConnection, so by explicitly doing the POST (at the moment I cannot use the ReST API Java Wrapper Extension Point). From https://docs.alfresco.com/content-services/latest/develop/rest-api-guide/folders-files/#uploadfile I see the curl example, but I could not "translate" it in Java (I had no problem with the POST using the alfresco/api/-default-/public/authentication/versions/1/tickets call). I do not know how to deal with the various "-F" parameters and on how to pass the content of the file itself.
Has anyone use the direct REST APIs to upload a file in ACS? Could you please provide me a simple example on how to do it?
Thank you
Andrea
12-12-2022 04:27 AM
This may help:
12-12-2022 06:03 AM
angelborroy thank you for the response.
Following the link you provided, I was able to make it work with CloseableHttpClient + HttpPost + MultipartEntityBuilder classes. I'm still not able to make it work with the HttpURLConnection (I get a 400 response), but at least I know it can work with plain Java calls using the REST APIs.
The working code (so it could be useful for anyone who has the same issue) is:
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost uploadFile = new HttpPost("http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-shared-/children");
uploadFile.addHeader("Authorization", bacisAutString);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
FileBody fileBody = new FileBody(new File(filePath));
builder.addPart("filedata", fileBody);
HttpEntity multipart = builder.build();
uploadFile.setEntity(multipart);
HttpResponse response = httpClient.execute(uploadFile);
Explore our Alfresco products with the links below. Use labels to filter content by product module.