cancel
Showing results for 
Search instead for 
Did you mean: 

retrieve content file

yassine_036
Champ in-the-making
Champ in-the-making
hello
i have a problem and i don't know how can i resolve it really, i want to create a java function who allow me to retrieve a content of text file stored on alfresco, i don't know if is that possible with java
i hope that you can help me
thank you regards
4 REPLIES 4

madhurao
Champ in-the-making
Champ in-the-making
I guess you are not using Alfresco SDK for your development. If u are not using SDK, first download SDK then you will get some examples. After that ur development becomes easy.
Any way, I hope the following code will help you.

public static void uploadFile(){
      String msg="";
      try {
         String fileName="";
         String title="testfile";
         String description="xml file";
         //String path = "C:/LL Data/ATAD/Alfresco-Labs-3Stable-OOo-Setup.exe";
         String path = "C:/application-context.xml";
         path = path.replaceAll("\\\\", "/");
         fileName = path.substring(path.lastIndexOf('/')+1,path.length());
         String ext= fileName.substring(fileName.lastIndexOf('.')+1,fileName.length());
         System.out.println("FileName    ==== >> "+fileName);
         System.out.println("Path       ==== >> "+path);
         String contType="";
         
         if(ext.equals("txt"))
         contType = "text/plain";
         else if (ext.equals("xls"))
         contType = "application/vnd.ms-excel";
         else if (ext.equals("doc"))
         contType = "application/msword";
         else if (ext.equals("html") || ext.equals("htm"))
         contType = "text/html";
         else if (ext.equals("jpg") || ext.equals("jpeg"))
         contType = "image/jpeg";
         else if (ext.equals("bmp"))
         contType = "image/bmp";
         else if (ext.equals("pdf"))
         contType = "application/pdf";
         else if (ext.equals("ppt"))
         contType = "application/vnd.ms-powerpoint";
         else if(ext.equals("xml"))
         contType = "text/xml";
         else if (ext.equals("zip"))
         contType = "application/vnd.ms-zip";
         else if (ext.equalsIgnoreCase("wm"))
            contType = "video/x-ms-wm";
         
         Store storeRef = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
         RepositoryServiceSoapBindingStub repositoryService = null;
         Reference content = null;
         if(contType.equals(""))
         {
            contType = getMimeType(fileName);
         }
         if(contType.equals(""))
         {
            msg="Unknown file format. File not uploded.";
         }
         else
         {
            AuthenticationUtils.startSession("admin", "admin");
      
      //       Create a reference to the parent where we want to create content
            ParentReference companyHomeParent = new ParentReference(storeRef, null, "/app:company_home/app:user_homes", Constants.ASSOC_CONTAINS, null);
      
            repositoryService = WebServiceFactory.getRepositoryService();
            ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService();
            NamedValue[] contentProps = new NamedValue[1];
            NamedValue[] titledProps = new NamedValue[2];
      //       Assign name
            //companyHomeParent.setChildName("cm:" + fileName);
            
            companyHomeParent.setChildName(fileName);
            
            contentProps[0] = Utils.createNamedValue(Constants.PROP_NAME, fileName);
      //      Construct CML statement to add titled aspect
            titledProps[0] = Utils.createNamedValue(Constants.PROP_TITLE, title);
            titledProps[1] = Utils.createNamedValue(Constants.PROP_DESCRIPTION, description);
      
            CMLAddAspect addAspect = new CMLAddAspect(Constants.ASPECT_VERSIONABLE, titledProps, null, "2");
      //      create content
      //       Construct CML statement to create content node
      //       Note: Assign "1" as a local id, so we can refer to it in subsequent
      //       CML statements within the same CML block
            CMLCreate create = new CMLCreate("2", companyHomeParent, companyHomeParent.getUuid(), Constants.ASSOC_CONTAINS, null, Constants.PROP_CONTENT, contentProps);
      //       Construct CML Block
            CML cml = new CML();
            cml.setCreate(new CMLCreate[] {create});
            cml.setAddAspect(new CMLAddAspect[] {addAspect});
            
      //       Issue CML statement via Repository Web Service and retrieve result
      //       Note: Batching of multiple statements into a single web call
            try{
               UpdateResult[] result = repositoryService.update(cml);
               content = result[0].getDestination();
            //       Write some content
            }catch(Exception ex){
               System.out.println(" === IN SIDE EXCEPTION === ");
               String refstr = "/app:company_home/app:user_homes/"+StringEncription.encode(fileName);
               content = new Reference(storeRef, null, refstr);
               /*ResultSetRow[] rows = searchFile(fileName,reference,spaces,repositoryService);
               String id ="";
               if(rows==null){
                  System.out.println("File was not found in repository!");
               }else{
                  ResultSetRow row = rows[0];
                  ResultSetRowNode node = row.getNode();
                  id = node.getId();
                  System.out.println("id : "+ node.getType() + " , File was found in repository"+rows[0].getNode());
               }*/
            }
            if(content != null){
               FileInputStream is = new FileInputStream(path);
               byte[] bytes = ContentUtils.convertToByteArray(is);
               
               ContentFormat format = new ContentFormat(contType, "UTF-8");
               System.out.println("Reference path =>" + content.getPath());
               //Write the content
               contentService.write(content, Constants.PROP_CONTENT, bytes, format);
               msg=" *** Successfully File Uploaded *** ";   
            }
         }
      }
      catch(Exception e)
      {
         msg="Error uploading file";
         System.out.println("Error uploading file : "+e);
         System.out.println(e.toString());
         e.printStackTrace();
      }
      finally
      {
//         End the session
         AuthenticationUtils.endSession();
      }
      System.out.println(msg);
   }
   public static String getMimeType(String filename) {
       System.out.println("Mime Type of " + filename + " is " +
                            new MimetypesFileTypeMap().getContentType(filename));
       // expected output :
       // "Mime Type of gumby.gif is image/gif"
       return new MimetypesFileTypeMap().getContentType(filename);
     }

Regards,
Madhu

yassine_036
Champ in-the-making
Champ in-the-making
thank you for your help but it's not that i want, i think that i don't explain my problem well , i have a file stored in my alfresco and i want his content, i want a java fonction who allow me to have his content (String)
regards

dzumbu
Champ in-the-making
Champ in-the-making
I want to  do the same thing as the initial writer in this thread, but i want to implement it in C#, does anyone have any idea  how?

Thanks in advance.

shamabbas
Champ in-the-making
Champ in-the-making
Hi all,
I want same thing to implement. I'm gonna try the above code because im also new in java. I hope it'll work fine + i also want to upload my files using alfresco services.
If further anyone can help please do so.

Thanks

sham