To upload a new version of a file to the repository use the NodesApi.updateNodeContent method, which will set the new content for the file.
For more information about this ReST API endpoint, see Upload a New Version of File.
For a description of the common parameters, such as majorVersion, see this Common Parameters.
import org.alfresco.core.handler.NodesApi;
import org.alfresco.core.model.Node;
import org.alfresco.core.model.NodeBodyCreate;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class UploadNewFileVersionCmd {
static final Logger LOGGER = LoggerFactory.getLogger(UploadNewFileVersionCmd.class);
private Boolean majorVersion = true;
private String updateComment = null;
private String updatedName = null;
private List<String> include = null;
private List<String> fields = null;
@Autowired
NodesApi nodesApi;
public void execute(String textFileNodeId, String binFileNodeId) throws IOException {
// Update text content for a file
Node newTextFile = updateTextFileContent(textFileNodeId,"Some UPDATED text for the file");
// Upload a file as new content
Node newFile = uploadNewFileVersion(binFileNodeId, "updatedpicture.png");
}
/**
* Upload a file from disk as a new version
*/
private Node uploadNewFileVersion(String fileNodeId, String filePath) {
// Get the file bytes
File someFile = new File(filePath);
byte[] fileData = null;
try {
fileData = FileUtils.readFileToByteArray(someFile);
} catch (IOException e) {
e.printStackTrace();
}
// Update the file node content
Node updatedFileNode = nodesApi.updateNodeContent(fileNodeId,
fileData, majorVersion, updateComment, updatedName, include, fields).getBody().getEntry();
LOGGER.info("Uploaded new content for file: {}", updatedFileNode.toString());
return updatedFileNode;
}
/**
* Update text content for a file
*/
private Node updateTextFileContent(String fileNodeId, String textContent) {
// Update the file node content
Node updatedFileNode = nodesApi.updateNodeContent(fileNodeId,
textContent.getBytes(), majorVersion, updateComment, updatedName, include, fields).getBody().getEntry();
LOGGER.info("Updated text content for file: {}", updatedFileNode.toString());
return updatedFileNode;
}
}
We would execute this command class something like this, passing in the command name, text file Node ID, and the binary file node id:
% java -jar target/rest-api-0.0.1-SNAPSHOT.jar upload-new-version 0492460b-6269-4ca1-9668-0d934d2f3370 48413f7a-066d-4e38-b2e6-c84ede635493
2021-04-28 13:44:51.437 INFO 15466 --- [ main] o.a.tutorial.restapi.RestApiApplication : Started RestApiApplication in 2.782 seconds (JVM running for 3.471)
2021-04-28 13:44:51.439 INFO 15466 --- [ main] o.a.tutorial.restapi.RestApiApplication : args[0]: upload-new-version
2021-04-28 13:44:51.441 INFO 15466 --- [ main] o.a.tutorial.restapi.RestApiApplication : args[1]: 0492460b-6269-4ca1-9668-0d934d2f3370
2021-04-28 13:44:51.441 INFO 15466 --- [ main] o.a.tutorial.restapi.RestApiApplication : args[2]: 48413f7a-066d-4e38-b2e6-c84ede635493
2021-04-28 13:44:51.981 INFO 15466 --- [ main] o.a.t.restapi.UploadNewFileVersionCmd : Updated text content for file: class Node {
id: 0492460b-6269-4ca1-9668-0d934d2f3370
name: somestuff2.txt
nodeType: acme:document
isFolder: false
isFile: true
isLocked: false
modifiedAt: 2021-04-28T12:44:51.578Z
modifiedByUser: class UserInfo {
displayName: Administrator
id: admin
}
createdAt: 2021-04-28T12:02:33.143Z
createdByUser: class UserInfo {
displayName: Administrator
id: admin
}
parentId: 8fa4e27d-35aa-411d-8bbe-831b6ed0c445
isLink: null
isFavorite: null
content: class ContentInfo {
mimeType: text/plain
mimeTypeName: Plain Text
sizeInBytes: 30
encoding: ISO-8859-1
}
aspectNames: [rn:renditioned, cm:versionable, cm:titled, cm:auditable, acme:securityClassified, cm:author, cm:thumbnailModification]
properties: {cm:title=TextfileTitle2, cm:versionType=MAJOR, acme:documentId=DOC-001, cm:versionLabel=3.0, acme:securityClassification=Company Confidential, cm:lastThumbnailModification=[doclib:1619611506701], cm:description=TextfileDesc2}
allowableOperations: null
path: null
permissions: null
definition: null
}
2021-04-28 13:44:56.783 INFO 15466 --- [ main] o.a.t.restapi.UploadNewFileVersionCmd : Uploaded new content for file: class Node {
id: 48413f7a-066d-4e38-b2e6-c84ede635493
name: somepicture2.png
nodeType: acme:document
isFolder: false
isFile: true
isLocked: false
modifiedAt: 2021-04-28T12:44:52.055Z
modifiedByUser: class UserInfo {
displayName: Administrator
id: admin
}
createdAt: 2021-04-28T12:02:33.621Z
createdByUser: class UserInfo {
displayName: Administrator
id: admin
}
parentId: 8fa4e27d-35aa-411d-8bbe-831b6ed0c445
isLink: null
isFavorite: null
content: class ContentInfo {
mimeType: image/png
mimeTypeName: PNG Image
sizeInBytes: 23174
encoding: UTF-8
}
aspectNames: [rn:renditioned, cm:versionable, cm:titled, cm:auditable, acme:securityClassified, cm:author, cm:thumbnailModification, exif:exif]
properties: {cm:title=PicturefileTitle2, cm:versionType=MAJOR, acme:documentId=DOC-001, cm:versionLabel=3.0, exif:pixelYDimension=256, acme:securityClassification=Company Confidential, exif:pixelXDimension=256, cm:lastThumbnailModification=[doclib:1619611506392, imgpreview:1619611515611], cm:description=PicturefileDesc2}
allowableOperations: null
path: null
permissions: null
definition: null
}
Note how the cm:versionLabel has bumped up to 3.0. Version 1.0 and 2.0 were created when the files were created in two steps. For more information, see Uploading a File with Custom Type.