cancel
Showing results for 
Search instead for 
Did you mean: 

downloading a file through webservices [solved]

thierensbart
Champ in-the-making
Champ in-the-making
I'm trying to download a file thorugh webservices but I can't get it to work.

I can get hold of this:
contentUrl=store://2009/10/13/10/41/fb64d4a3-f652-4a45-9e3e-42cefbb585bf.bin|mimetype=text/plain|size=0|encoding=utf-8|locale=en_US_
but getting the file itself is a mystery to me…

I browsed around these forums and I saw something about NodeRef and ContentReader.
But I can't figure out how it works exactly.

I haven't found any example in the standard examples or via Google.

Anyone care to post a little example?
All I need to know is how to get from QueryResult, to getting my file…
3 REPLIES 3

rliu
Champ in-the-making
Champ in-the-making
I presume that you are developing a Java-backed web script controller.

You can use DownloadContentServlet.generateDownloadURL(...) to get the URL you need.

Hope that helps.

Rich

thierensbart
Champ in-the-making
Champ in-the-making
I'm not using web script.

I'm just accessing Alfresco through WebServices and I want to download the file linked with contentUrl=store://2009/10/13/10/41/fb64d4a3-f652-4a45-9e3e-42cefbb585bf.bin|mimetype=text/plain|size=0|encoding=utf-8|locale=en_US_.


So basicly I just need someone to enlighten me on how to get the effective file from the contentUrl.

thierensbart
Champ in-the-making
Champ in-the-making
i solved it myself.  it was easier then it seemed.

here's the solution for future reference:

this is my function:


private static void makeFile(MigrateDocument doc) throws Exception {
   //start session
   AuthenticationDetails details = startSession(getUser(), getPass());

   //get contentService
   ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService();
   
   //create reference to document with the uuid encapsulated in my MigrateDocument
   Reference ref = new Reference(getStore(), doc.getUuid(), null);
   
   //read the content
   Content[] readResult = contentService.read(new Predicate(new Reference[]{ref}, getStore(), null), Constants.PROP_CONTENT);

   for (Content content : readResult) {
         String[] splittedUrl = content.getUrl().split("/");
         
         if(splittedUrl[splittedUrl.length-1].equals(doc.getName())){
         //this if-clause makes sure it's the right thing to download BUT be sure to have replaced all whitespace in the filename by a '+' or the equals will return false!
            InputStream in = ContentUtils.getContentAsInputStream(content);
            BufferedInputStream bis = new BufferedInputStream(in);
            
            //find location to place the downloaded file
            String path = getPhysicalPaths().get(doc.getParent().getUuid());
            
            //write
            FileOutputStream fos = new FileOutputStream(path + doc.getName());
            BufferedOutputStream bos = new BufferedOutputStream(fos, 1024);
            byte data[] = new byte[1024];
            int count;
            while((count = bis.read(data, 0, 1024)) != -1){
               bos.write(data, 0, count);
            }
            bos.close();
            bis.close();
         }
      }   
   //end session
   endSession(details);

i hope someone will find this useful…