2 Replies Latest reply: Nov 7, 2012 12:40 AM by user1175496 RSS

    Blank FIle getting Downloaded Using the GET _FILE method.

    user1175496
      Hi,
      I am using the following piece of code download a file from UCM after using the
      GET_FILE service,but the file which is getting downloaded is a blank file.I have
      turned full verbose tracing on in the logs ,but there s no error in the log
      file.Can anyone provide any suggestions on the same:


      public InputStream getFileStream (String dID,String documentName,String
      extension,String contentID) throws Exception {
      InputStream stream = null;

      String myFileToEdit = "C:\\testFolder\\"+contentID+"_"+documentName;
      InputStream myInputStream = null;
      InputStreamReader myInputStreamReader = null;


      IdcClientManager IdcClientManager = new IdcClientManager();
      IdcClient client =
      IdcClientManager.createClient("idc://localhost:4444");
      IdcContext IdcContext = new IdcContext("weblogic");
      //Get the client (from the base class) and create a new binder
      // IdcClient client = getClient();
      DataBinder dataBinder = client.createBinder ();
      SystemUtils.trace("system", "dID in getFile>>>>>>>>>>" +dID);

      //retrieve the file, check the file size
      dataBinder.putLocal ("IdcService", "GET_FILE");
      dataBinder.putLocal ("dID", dID);
      ServiceResponse response = client.sendRequest (IdcContext, dataBinder);
      int reportedSize = Integer.parseInt (response.getHeader
      ("Content-Length"));
      int retrievedSize = 0;

      //The file is streamed back to us in the response
      stream = response.getResponseStream ();
      SystemUtils.trace("system", "stream>>>>>>>>>>" +stream.toString());
      SystemUtils.trace("system", "Conetnt in getFile>>>>>>>>>>" +contentID);
      SystemUtils.trace("system", "DocNAme in getFile>>>>>>>>>>"
      +documentName);
      File f=new File("C:\\testFolder\\"+contentID+"_"+documentName +"."
      +extension);
      OutputStream out=new FileOutputStream(f);
      byte buf[]=new byte[1024];
      int len;
      SystemUtils.trace("system", "File>>>>>>>>>>" +stream.read(buf));
      while((len=stream.read(buf))>0)
      SystemUtils.trace("system", "len>>>>>>>>>" +len);

      out.write(buf,0,len);
      out.close();
      stream.close();
      }
        • 1. Re: Blank FIle getting Downloaded Using the GET _FILE method.
          Fabian Scherpenzeel
          >
          while((len=stream.read(buf))>0)
          SystemUtils.trace("system", "len>>>>>>>>>" +len);

          out.write(buf,0,len);
          out.close();
          stream.close();
          only the trace message is within the while loop... only when the while loop exits the content of the buffer is written to the output stream.
          • 2. Re: Blank FIle getting Downloaded Using the GET _FILE method.
            user1175496
            Hi
            I tried with the modified code(below) but still its not working :
            public void getFileStream (String dID,String documentName,String extension,String contentID) throws Exception {
                 InputStream stream = null;

                 String myFileToEdit = "C:\\testFolder\\"+contentID+"_"+documentName+"." +extension;

                 InputStreamReader InputStreamReader = null;

                      
                      IdcClientManager IdcClientManager = new IdcClientManager();
                 IdcClient client = IdcClientManager.createClient("idc://localhost:4444");
                 IdcContext IdcContext = new IdcContext("weblogic");
                      //Get the client (from the base class) and create a new binder
                 // IdcClient client = getClient();
                 DataBinder dataBinder = client.createBinder ();
                 SystemUtils.trace("system", "dID in getFile>>>>>>>>>>" +dID);            
                 
                 //retrieve the file, check the file size
                 dataBinder.putLocal ("IdcService", "GET_FILE");
                 dataBinder.putLocal ("dID", dID);
                 ServiceResponse response = client.sendRequest (IdcContext, dataBinder);
                 int reportedSize = Integer.parseInt (response.getHeader ("Content-Length"));
                 int intRetrievedFileSize = 0;
                 
                 //The file is streamed back to us in the response
                 stream = response.getResponseStream ();
                 InputStreamReader =new InputStreamReader(stream, "UTF-8");
                 SystemUtils.trace("system", "stream>>>>>>>>>>" +stream.toString());            
                 SystemUtils.trace("system", "Conetnt in getFile>>>>>>>>>>" +contentID);            
                 SystemUtils.trace("system", "DocNAme in getFile>>>>>>>>>>" +documentName);            
                 int intRead = 0;
            /* char[] myCharArray = new char[4096];
            StringBuffer myStringBuffer = new StringBuffer();
            while ((intRead = InputStreamReader.read(myCharArray)) != -1) {
                 myStringBuffer.append(myCharArray, 0, intRead);
            intRetrievedFileSize += intRead;
            }
            myFileToEdit = myStringBuffer.toString();*/

                 File f=new File(myFileToEdit);
                 OutputStream out=new FileOutputStream(f);      
                 byte buf[]=new byte[4096];
                      int len;
                      SystemUtils.trace("system", "File>>>>>>>>>>" +stream.read(buf));
                      len=stream.read(buf);
                      while(len>0){
                      //SystemUtils.trace("system", "len>>>>>>>>>" +len);            
                 
                      out.write(buf,0,len);
                      
                      }
            InputStreamReader.close();
            out.close();
                      stream.close();
            // return stream;
            }