Deleted.
You can find the nodeRef for the document by searching only in target folder (assuming that the same name can be stored in different folders). After that, if you use ContentWriter with the right nodeRef, Alfresco will version the node automatically.
Some similar code available at:
https://github.com/keensoft/alfresco-esign-cert/blob/master/esign-cert-repo/src/main/java/es/keensoft/alfresco/sign/webscript/SaveSign.java#L91