cancel
Showing results for 
Search instead for 
Did you mean: 

Getting error while downloading content from alfresco repo

zhy628588
Champ in-the-making
Champ in-the-making
Hey All:
  Recently,I need to write a program to download file form the alfresco.The following is my code.
  public class download_test {
   //Url(include port)
   protected static final String Url = "http://localhost:8080/alfresco/api";
   //UserName Key
   protected static final String UserName = "admin";
  //PassWord key
   protected static final String PassWord = "admin";
  //Upload SerVice Folder
   protected static final String ServiceFolderName = "GDV/Open";
  /** The store used throughout the samples */
   protected static final Store STORE = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
/**  * @param args */
public static void main(String[] args) throws Exception
{
  // TODO Auto-generated method stub 
  /**Set repository location*/
   WebServiceFactory.setEndpointAddress(Url);
/**Start the session*/
   AuthenticationUtils.startSession(UserName, PassWord);
/** Make sure Service Folder has been created,or download files will be failure */ 
  boolean Success = true;
  String[] ServiceFolderList = ServiceFolderName.split("/");
  String ParentFolder = "/app:company_home";
  String ServiceFolder;
  Reference SAMPLE_FOLDER;
  for(String mServiceFolder Smiley FrustratederviceFolderList) {
     ServiceFolder = ParentFolder+"/cm:"+mServiceFolder;
    SAMPLE_FOLDER = new Reference(STORE, null, ServiceFolder);
   try  {
    WebServiceFactory.getRepositoryService().get(new Predicate(new Reference[]{SAMPLE_FOLDER}, STORE, null));
    } catch (Exception exception) {
    //jmp to /** End the session*/ 
   Success=false;
    break;
    }
    ParentFolder = ServiceFolder; }
   System.out.println(ParentFolder);
    /**Download files,if exception then "Success = False" */ 
   if(Success) {
   // Get the content service 
   ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService();
  //Search the content int the ServicrFolder
   Content mcontent = SearchContent(contentService,ParentFolder,"1kkk.jpg");  DownLoadFileFromAlfresco(mcontent,"d:/1kkk.jpg"); '
   }
   /** End the session*/ 
   AuthenticationUtils.endSession(); }

    public static void DownLoadFileFromAlfresco(Content content,String destFolder) throws Exception {
       InputStream in=ContentUtils.getContentAsInputStream(content);
      //Output file
     File file=new File(destFolder);
         if(!file.exists()) file.createNewFile();
         FileOutputStream out=new FileOutputStream(file);
         int c;
          byte buffer[]=new byte[1024];
        while((c=in.read(buffer))!=-1)
       {
             for(int i=0;i<c;i++)
           out.write(buffer);
          }
         in.close();
         out.close();
        System.out.println("download ok!");
     }
    public static String modiflyRefenceName(String FileName) {
           if(FileName.charAt(0)>='1'&&FileName.charAt(0)<='9') {
                           String ModifliedName ="_x003"+FileName.substring(0,1)+"_"+FileName.substring(1);
                           System.out.println(ModifliedName); return ModifliedName;
                   }
                           return FileName;
}
public static Content SearchContent(ContentServiceSoapBindingStub contentService, String srcFolder,String FileName) throws Exception {
  String FileRefenceName = modiflyRefenceName(FileName);
  Reference contentReference = new Reference(STORE,null,srcFolder+"/cm:"+FileRefenceName);
  Content[] readResult = contentService.read( new Predicate(new Reference[]{contentReference}, STORE, null), Constants.PROP_CONTENT);
  System.out.println(readResult[0].getUrl());
   for(Content content: readResult) {
       String[] SplitedUrl = content.getUrl().split("/");
        if(SplitedUrl[SplitedUrl.length-1].equals(FileName))
             { return content; }
       }
        return null; }
}
I meet a problem,when the fileName which we will download from alfresco like this "Iberdrola SA (2009-03-24).xls".I got thefollowing exception:
AxisFault
faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.generalException
faultSubcode:
faultString:
faultActor:
faultNode:
faultDetail:
   {http://www.alfresco.org/ws/service/content/1.0}ContentFault:<ns1:errorCode>0</ns1:errorCode><ns1:message>Error executing xpath:
   xpath: /app:company_home/cm:GDV/cmSmiley Surprisedpen/cm:IberdrolaSA(2009-03-24).xls</ns1:message>
   {http://xml.apache.org/axis/}exceptionNameSmiley Surprisedrg.alfresco.repo.webservice.content.ContentFault
   {http://xml.apache.org/axis/}stackTrace:
   at org.alfresco.repo.webservice.content.ContentWebService.read(ContentWebService.java:113)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at org.apache.axis.providers.java.RPCProvider.invokeMethod(RPCProvider.java:397)
   at org.apache.axis.providers.java.RPCProvider.processMessage(RPCProvider.java:186)
   at org.apache.axis.providers.java.JavaProvider.invoke(JavaProvider.java:323)
   at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)
   at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
   at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
   at org.apache.axis.handlers.soap.SOAPService.invoke(SOAPService.java:454)
   at org.apache.axis.server.AxisServer.invoke(AxisServer.java:281)
   at org.apache.axis.transport.http.AxisServlet.doPost(AxisServlet.java:699)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
   at org.apache.axis.transport.http.AxisServletBase.service(AxisServletBase.java:327)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
   at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
   at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
   at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
   at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
   at java.lang.Thread.run(Thread.java:595)

   {http://xml.apache.org/axis/}hostnameSmiley Tongueinesoft-9bf1a1


   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
   at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
   at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
   at java.lang.reflect.Constructor.newInstance(Unknown Source)
   at java.lang.Class.newInstance0(Unknown Source)
   at java.lang.Class.newInstance(Unknown Source)
   at org.apache.axis.encoding.ser.BeanDeserializer.<init>(BeanDeserializer.java:104)
   at org.apache.axis.encoding.ser.BeanDeserializer.<init>(BeanDeserializer.java:90)
   at org.alfresco.webservice.content.ContentFault.getDeserializer(ContentFault.java:146)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.apache.axis.encoding.ser.BaseDeserializerFactory.getSpecialized(BaseDeserializerFactory.java:154)
   at org.apache.axis.encoding.ser.BaseDeserializerFactory.getDeserializerAs(BaseDeserializerFactory.java:84)
   at org.apache.axis.encoding.DeserializationContext.getDeserializer(DeserializationContext.java:464)
   at org.apache.axis.encoding.DeserializationContext.getDeserializerForType(DeserializationContext.java:547)
   at org.apache.axis.message.SOAPFaultDetailsBuilder.onStartChild(SOAPFaultDetailsBuilder.java:157)
   at org.apache.axis.encoding.DeserializationContext.startElement(DeserializationContext.java:1035)
   at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
   at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
   at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
   at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
   at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
   at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
   at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
   at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
   at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
   at javax.xml.parsers.SAXParser.parse(Unknown Source)
   at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
   at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
   at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)
   at org.apache.axis.handlers.soap.MustUnderstandChecker.invoke(MustUnderstandChecker.java:62)
   at org.apache.axis.client.AxisClient.invoke(AxisClient.java:206)
   at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
   at org.apache.axis.client.Call.invoke(Call.java:2767)
   at org.apache.axis.client.Call.invoke(Call.java:2443)
   at org.apache.axis.client.Call.invoke(Call.java:2366)
   at org.apache.axis.client.Call.invoke(Call.java:1812)
   at org.alfresco.webservice.content.ContentServiceSoapBindingStub.read(ContentServiceSoapBindingStub.java:421)
I tried use ISO9075.encode(" Iberdrola SA (2009-03-24).xls") to get the fileName,it seems thatI can not get the content ,the content is null
Could somebody to help me to solve this problem.
Thanks a lot !
2 REPLIES 2

zhy628588
Champ in-the-making
Champ in-the-making
Hey all:
  I debug the program.And I found that if I use the ISO9705.encode() method to modify the filName.using the following code can get the content:
   Content[] readResult = contentService.read(new Predicate(new Reference[] { contentReference }, STORE, null), Constants.PROP_CONTENT);
but when I run the following code,I can not get the value of the content:
  for (Content content : readResult) {
   System.out.println("the url of the content :"+content.getUrl().toString());
   String[] SplitedUrl = content.getUrl().split("/");
   System.out.println("the value of the SplitedUrl :"+SplitedUrl[SplitedUrl.length - 1]);
   if (SplitedUrl[SplitedUrl.length - 1].equals(FileRefenceName)) {
               return content;
      }
}
I debug the code,the value of  SplitedUrl[SplitedUrl.length - 1] is changed to IberdrolaSA%282009-03-24%29.xls.SO we can not found that IberdrolaSA(2009-03-24).xls.
Hope somebody help me!

sisao
Champ in-the-making
Champ in-the-making
Hi zhy628588,

names are ISO9075 encoded when nodes are stored by alfresco….you can use org.alfresco.util.ISO9075.* in order to retrieve the correct metadata property values for queries.

Again %28 = "(" and %29 = ")"

Regards.