cancel
Showing results for 
Search instead for 
Did you mean: 

Java code to Download File from site document library.

shibu
Champ in-the-making
Champ in-the-making
Can any body give the java code to download files from a site document library?
7 REPLIES 7

mitpatoliya
Star Collaborator
Star Collaborator
you can do through the download url associated with each content.
Just call that url in the java and in response you will get that file.

shibu
Champ in-the-making
Champ in-the-making
How can I get the download URL?

I tried with the following code :
<code>
public static String webService(String dir, String fileName)
         throws Exception {
      String downloadURL = null;

      try {
         // Let us say you know the directory and filename For Eg:

         // String dir = "app:company_home/sites/my-site/documentLibrary/";
         // String fileName = "Stake";
         System.out.println("dir ::" + dir);
         System.out.println("fileName ::" + fileName);

         // Start the session
         AuthenticationUtils.startSession("admin", "admin");
         String ticket = AuthenticationUtils.getCurrentTicket();

         // Create a reference to the parent where we want to create content
         Store storeRef = new Store(Constants.WORKSPACE_STORE, "SpacesStore");

         // Reference reference = new Reference(storeRef, null,
         // "/app:company_home/cmsmileyCMCOE/cm:COE1/*[@cm:name=\"" +
         // "eProfilerPPT" + "\"]");
         // Create a new Reference to the node you want (variable path here).
         String path = dir + "*[@cm:name=\"" + fileName + "\"]";
         Reference reference = new Reference(storeRef, null, path);
         ContentServiceSoapBindingStub contentService = WebServiceFactory
               .getContentService();

         // Read the content from the respository
         Content[] readResult = contentService.read(new Predicate(
               new Reference[] { reference }, storeRef, null),
               Constants.PROP_CONTENT);
         Content contentRef = readResult[0];

         // Get the ticket URL as above and the downloadURL here points to
         // the url where the content is present.
         System.out.println("contentRef.getUrl() : " + contentRef.getUrl());
         String ticketURL = "?ticket=" + ticket;
         downloadURL = contentRef.getUrl() + ticketURL;
         System.out.println("downloadURL in webService(): " + downloadURL);
      } catch (Throwable e) {
         System.out.println(e.toString());
      } finally {
         // End the session
         // AuthenticationUtils.endSession();
      }
      return downloadURL;
   }
<code>



This is returning null value when calling with a site document library;
String url = webService("app:company_home/sites/my-site/documentLibrary/", "Alfresco Doccument.odt");
While printing path , it displays : app:company_home/sites/my-site/documentLibrary/*[@cm:name="Alfresco Doccument.odt"] .
And exception is throwing when "Content[] readResult" is defined.

But working fine if I called like;
String url = webService("/app:company_home/cm:sample/", "images.jpeg");

So the problem is happening when site document library path is passing.
How to fix it?
Please help me to fix it.

shibu
Champ in-the-making
Champ in-the-making
I got the answer.
Path of the site document library will be "app:company_home/st:sites/cm:my-site/cm:documentLibrary".

mrball
Champ in-the-making
Champ in-the-making
Hi,
I found the content url and it works if I put it in an htlm anchor:

<a href="http://localhost:8080/alfresco/d/a/workspace/SpacesStore/70847d54-dd4d-4660-a557-f788a8f2b94e/test.d...">Download file </a>



I would like to create a "bulk download" in JAVA code to download a Collection of a content from alfresco.
Is it possible?

I used <strong>org.apache.http.client.HttpClient httpClient = new DefaultHttpClient();</strong>


String defaultURL= "http://localhost:8080/alfresco/d/a/workspace/SpacesStore/70847d54-dd4d-4660-a557-f788a8f2b94e/test.d...";
HttpGet httpget = new HttpGet(defaultURL);
org.apache.http.client.HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
long len = entity.getContentLength();
InputStream inputStream = entity.getContent();
…….

// Something here to get the input stream
// and the file object to put into the zip file



Finally I create a Zip file where I have all files.
Now I have this zip file with all files, but all file have zero bytes size.

To help you, I tell you that the value of "len" is "-1"

shailu13
Champ in-the-making
Champ in-the-making
Thank you @shinu your post helped me Smiley Happy

poonam_sangle
Champ in-the-making
Champ in-the-making
above code is not working…I got Error starting session, I have added line
WebServiceFactory.setEndpointAddress("http://192.168.1.90:8484/alfresco/api"); still I have same error

divyank
Champ in-the-making
Champ in-the-making
Iam getting the same error session is not getting started even after including WebServiceFactory.setEndpointAddress("http://192.168.1.90:8484/alfresco/api"); Iam using the same code abpve to download a file


public String webService(String dir, String fileName)   throws Exception

{

         String downloadURL = null;
         try {


            System.out.println("dir ::" + dir);

            System.out.println("fileName ::" + fileName);

   

            // Start the session
         //   WebServiceFactory.setEndpointAddress("http://127.0.0.1:8080/alfresco/api");
            AuthenticationUtils.startSession("admin", "Password123");

            String ticket = AuthenticationUtils.getTicket();

   

            // Create a reference to the parent where we want to create content

            Store storeRef = new Store(Constants.WORKSPACE_STORE, "SpacesStore");

   

            // Reference reference = new Reference(storeRef, null,

            // "/app:company_home/cmsmileyCMCOE/cm:COE1/*[@cm:name=\"" +

            // "eProfilerPPT" + "\"]");

            // Create a new Reference to the node you want (variable path here).

            String path = dir + "*[@cm:name=\"" + fileName + "\"]";

            Reference reference = new Reference(storeRef, null, path);

            ContentServiceSoapBindingStub contentService = WebServiceFactory

                  .getContentService();

   

            // Read the content from the respository

            Content[] readResult = contentService.read(new Predicate(

                  new Reference[] { reference }, storeRef, null),

                  Constants.PROP_CONTENT);

            Content contentRef = readResult[0];

   

            // Get the ticket URL as above and the downloadURL here points to

            // the url where the content is present.

            System.out.println("contentRef.getUrl() : " + contentRef.getUrl());

            String ticketURL = "?ticket=" + ticket;

            downloadURL = contentRef.getUrl() + ticketURL;

            System.out.println("downloadURL in webService(): " + downloadURL);

         } catch (Throwable e) {

            System.out.println(e.toString());

         } finally {

            // End the session

            // AuthenticationUtils.endSession();

         }

         return downloadURL;

      }

What can be my directory if i have a file in a site created by me called "mysite" document library



Iam using Alfresco community edition 5.0.d