AnsweredAssumed Answered

upload and download files to alfresco using Java Class

Question asked by hariprasad on Jun 26, 2014
Latest reply on Jul 28, 2014 by romschn
How to upload and download files to alfresco using Java Class,
I need a java class for the below items with different methods, could you please provide

1.   Upload File along with Meta Data
public void uploadFile( ) {


2.   Download File along with Meta Data
public void downloadFile( ) {


3.   Search File By Name
public void SearchFileByName( ) {


4.   Search File with Meta Data
public void SearchFileWithMetaData( ) {


5.   List GUIDs for Files matching the criteria
public void listOfGuids( ) {

6.   Search File by GUID

public void searchGuid( ) {

I have implemented in the below class for uploading to Alfresco,
i need similarly in different methods for the other items mentioned above,
could you please help on this,
public class AlfrescoUtil {

public static void main(String args[]) throws Exception {
String fileName = "upload.jpg";
AuthenticationUtils.startSession("admin", "admin");
ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService();
RepositoryServiceSoapBindingStub repositoryService =WebServiceFactory.getRepositoryService();

Store storeRef = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
String parentSpace = "/app:company_home/cm:ACH_Space";
ParentReference parentReference = new ParentReference (storeRef, null, parentSpace, Constants.ASSOC_CONTAINS, null);
parentReference.setChildName("cm:"+ fileName);

NamedValue[] contentProps = new NamedValue[3];
contentProps[0] = Utils.createNamedValue(Constants.PROP_TITLE,fileName);
contentProps[1] = Utils.createNamedValue(Constants.PROP_DESCRIPTION, fileName);
contentProps[2] = Utils.createNamedValue(Constants.PROP_NAME, fileName);

NamedValue[] aspectProps = new NamedValue[3];
aspectProps[0] = Utils.createNamedValue("{mandate}UMRN","18062014_01");
aspectProps[1] = Utils.createNamedValue("{mandate}IRN", "18062014_01");
aspectProps[2] = Utils.createNamedValue("{mandate}status", "TDS");

CMLAddAspect addAspect = new CMLAddAspect();
addAspect.setWhere(new Predicate(new Reference[] { parentReference }, null, null));

CMLCreate create = new CMLCreate("1", parentReference, parentReference.getUuid(), Constants.ASSOC_CONTAINS,null, Constants.PROP_CONTENT, contentProps);

CML cml = new CML();
cml.setCreate(new CMLCreate[] { create });
cml.setAddAspect(new CMLAddAspect[] { addAspect });
UpdateResult[] result1 = repositoryService.update(cml);
Reference content = result1[0].getDestination();
FileInputStream is = new FileInputStream("C:\\Users\\0021083\\Desktop\\PostGoLive4.jpg");

byte[] bytes = ContentUtils.convertToByteArray(is);

ContentFormat format = new ContentFormat("image/jpeg", "UTF-8");
contentService.write(content, Constants.PROP_CONTENT, bytes,format);
System.out.println("File Uploaded Successfully");