cancel
Showing results for 
Search instead for 
Did you mean: 

Descargar fichero desde alfresco

sanxesito
Champ in-the-making
Champ in-the-making
Hola!
Soy nuevo en el foro, aunque ya llevo varias semanas siguiendolo

Me ha surgido una duda, y llevo varios dias intentandolo solucionar
Estoy realizando un programa en JAVA y webservices que comunica con alfresco
Realice la subida de ficheros, pero ahora estoy intentando la bajada de ficheros a mi local.


public Fichero obtenerDocumentoGD(String dir, Fichero fichero) throws Exception
{
   if (fichero.getNombreFichero()==null){
      return null;
   }
   Fichero respuesta = new Fichero();
   File f1 = null;
   String nombreFichero=fichero.getNombreFichero().toLowerCase();
   WebServiceFactory.setEndpointAddress(URL);
   WebServiceFactory.setTimeoutMilliseconds(TIMEOUT);
                AuthenticationUtils.startSession(USERNAME,PASSWORD);
                ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService();
                String space = PATH + "/*[@cm:name=\""+ dir + "\"]" + "/*[@cm:name=\""+nombreFichero+"\"]";
   Reference i=new Reference(STORE,null,space);
   try {
      
      Node[] nodes;
      nodes = WebServiceFactory.getRepositoryService().get(new Predicate(new Reference[] {i}, STORE, null));
   
      if (nodes != null && nodes.length > 0) {
         Content[] readResult = WebServiceFactory.getContentService().read(new Predicate(new Reference[] {nodes[0].getReference()}, STORE, null), Constants.PROP_CONTENT);
         Content content = readResult[0];
      
          ContentUtils.copyContentToFile(content, new File("C:/docs/temp/ejemplo.txt"));

         // Definir la ruta donde se recuperará el fichero
         
         f1 = new File("C:/docs/temp/" + fichero.getNombreFichero());
         FileInputStream fis = new FileInputStream(f1);
         byte[] entryBytes = null;
         
         // Recuperar el fichero definido.
         ByteArrayOutputStream bos = new ByteArrayOutputStream();

         byte[] buffer = new byte[1024];
         int len;

         while ((len = fis.read(buffer, 0, 1024)) > -1) {
            bos.write(buffer, 0, len);
         }
         bos.flush();
         fis.close();

         entryBytes = bos.toByteArray();
         pasar(entryBytes);
         // logger.info("El fichero no se ha recuperado correctamente el
         // fichero de la ruta especificada.");
         
         respuesta.setCodigoFichero(fichero.getCodigoFichero());
         respuesta.setFichero(entryBytes);
         respuesta.setNombreFichero(fichero.getNombreFichero());
         respuesta.setRutaFichero("C:/docs/temp");
         
         
      } else {
      }
   } catch (Exception e)
   {
      e.printStackTrace();
      return null;   
   }
   finally
   {
           AuthenticationUtils.endSession();
           if (f1!=null){
           }
   }
    return respuesta;   
}

PATH es   "/app:company_home/*[@cm:name=\"" + "myFolder" + "\"]"

STORE es Store(Constants.WORKSPACE_STORE, "SpacesStore");


Siempre me da una excepcion en ….     ContentUtils.copyContentToFile(content, new File("C:/docs/temp/ejemplo.txt"));

diciendo …."Unable to get content as inputStream."

Gracias de antemano y gran foro!
10 REPLIES 10

pablo_zapico
Champ in-the-making
Champ in-the-making
Buenas, para leer el byte[] de alfresco yo lo hago de esta forma



   public byte[] search(String id, String path, ContentServiceSoapBindingStub contentRepository) throws Exception{
      
      Reference reference = new Reference(STORE, id, path);   
            
       Content[] readResult = contentRepository.read(new Predicate(new Reference[]{reference}, STORE, null), Constants.PROP_CONTENT);
       byte[] contentBytes = null;
       if(readResult!=null){
          Content content = (Content) readResult[0];         
          contentBytes = UtilsFile.getByteContent(content);
      }
      return contentBytes;
   }


yo retorno un array de bytes, pero si kisieras el fichero podria utilizar


         fileTemp = File.createTempFile(AlfrescoKeys.FILE_TEMP
               , "."
               + ext);

         // Se convierte el contenido en un fichero temporal
         ContentUtils.copyContentToFile(content, fileTemp);

A partir del content obtenido y te genera un fichero.

espero qte sirva.

sanxesito
Champ in-the-making
Champ in-the-making
muchas gracias por la ayuda

el problema que tengo es el ContentUtils, siempre me da excepcion ahi, sea cual sea el procedimiento que utilizo

que libreria contiene Utilsfile?

Un saludo

pablo_zapico
Champ in-the-making
Champ in-the-making
Se estan guardando bien los ficheros dentro de alfresco? igual no te estan retornando bien los byte[]. La clase UtilsFile es mia propia, te la pego aunq no deberia… mejor te la envio por correo.

Otra cosa cuando haces la carga comprueba el numero de byte[] q te inserta.

A mi con ese metodo que te pegue antes me retorna perfecto los ficheros y esto esta en produccion asi que funciona.

sanxesito
Champ in-the-making
Champ in-the-making
Muchas gracias, ya me llego el correo, pero no me sirve dado que utilizas el CopyContentToFile, y me salta la excepcion
Muchas gracias de todas formas
Smiley Very Happy

pablo_zapico
Champ in-the-making
Champ in-the-making
Pero… mmm… jejeje. Has probado con ContentServiceSoapBindingStub, la excepcion tienes el codigo de la excepcion que te salta? me tiene intrigado

pablo_zapico
Champ in-the-making
Champ in-the-making

sanxesito
Champ in-the-making
Champ in-the-making
13:40:40,089 ERROR [descarga.UtilsFile] org.alfresco.webservice.util.WebServiceException: Unable to get content as inputStream.
Exception in thread "main" org.alfresco.webservice.util.WebServiceException: Unable to get content as inputStream.
   at org.alfresco.webservice.util.ContentUtils.getContentAsInputStream(ContentUtils.java:133)
   at org.alfresco.webservice.util.ContentUtils.copyContentToFile(ContentUtils.java:293)
   at descarga.UtilsFile.getByteContent(UtilsFile.java:42)
   at descarga.Descarga.obtenerDocumentoGD(Descarga.java:74)
   at descarga.Programa.main(Programa.java:99)
Caused by: java.io.IOException: Server returned HTTP response code: 500 for URL: http://10.121.0.89:8080/alfresco/download/direct/workspace/SpacesStore/03cb4dbc-56ba-46dd-9fc4-53b54...
   at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
   at org.alfresco.webservice.util.ContentUtils.getContentAsInputStream(ContentUtils.java:129)
   … 4 more

sanxesito
Champ in-the-making
Champ in-the-making
La cosa de la version del Store no creo que afecte, porque la subida de ficheros si que me funciona bien

sanxesito
Champ in-the-making
Champ in-the-making
Y encontre el problema y es que tengo un directorio activo y chocaba contra el
Lo tengo que realizar entero por webservices en vez de devolver la url…asique a ver

Un saludo!!