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
This may help:
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);
Ask for and offer help to other Alfresco Content Services Users and members of the Alfresco team.
Related links:
By using this site, you are agreeing to allow us to collect and use cookies as outlined in Alfresco’s Cookie Statement and Terms of Use (and you have a legitimate interest in Alfresco and our products, authorizing us to contact you in such methods). If you are not ok with these terms, please do not use this website.