cancel
Showing results for 
Search instead for 
Did you mean: 

upload and download files to alfresco using Java Class

hariprasad
Champ in-the-making
Champ in-the-making
——————————————————–
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";
WebServiceFactory.setEndpointAddress("http://localhost:8080/alfresco/soapapi");
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.setAspect("{mandate}mandateImage");
addAspect.setProperty(aspectProps);
addAspect.setWhere(new Predicate(new Reference[] { parentReference }, null, null));
addAspect.setWhere_id("1");

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");
}
5 REPLIES 5

romschn
Star Collaborator
Star Collaborator
You can simply use the out-of-the-box webscripts available to upload and get content of the files from the repository and invoke them through your java code.

Webscript to upload a content - tomcat\webapps\alfresco\WEB-INF\classes\alfresco\templates\webscripts\org\alfresco\repository\upload\upload.post.desc.xml
Webscript to get the content - tomcat\webapps\alfresco\WEB-INF\classes\alfresco\templates\webscripts\org\alfresco\cmis\content.get.desc.xml

Hope this helps.

andy2_0
Champ in-the-making
Champ in-the-making
HI Ramesh

Would you please give the sample code on how to call the webscript from Java code? Thanks!

romschn
Star Collaborator
Star Collaborator
Refer to this link - https://forums.alfresco.com/comment/146735#comment-146735 it has the sample code to make a call to alfresco webscript from Java code.

Hope this helps.

jainmcs03
Champ in-the-making
Champ in-the-making
Hi Ramesh,

I have install alfresco and share WAR's manually on tomcat server, its working fine. But i couldnt find the below files in my WAR.I have to access alfresco through RESTful please advise. Im using alfresco-community-5.0.c.zip.

Webscript to upload a content - tomcat\webapps\alfresco\WEB-INF\classes\alfresco\templates\webscripts\org\alfresco\repository\upload\upload.post.desc.xml
Webscript to get the content - tomcat\webapps\alfresco\WEB-INF\classes\alfresco\templates\webscripts\org\alfresco\cmis\content.get.desc.xml

Regards
Jayendran

mrogers
Star Contributor
Star Contributor
The file is still there its just not "exploded".   There's no change required.
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.