AnsweredAssumed Answered

Upload file - Java example

Question asked by sergio on Dec 22, 2006
Hoping to do a useful thing, I would like to share with you a method to upload a file using ws.

public static boolean wsUploadFile(   String schema,
                                                         String address,
                                                        String parentNodeId,
                                                        String fileName,
                                                        String fileMimeType,
                                                        String fileType)
   {
     boolean fileCreated = false;
     
     final Store STORE = new Store(schema, address);
     
     try
     {
        File f = new File(fileName);
        
        ParentReference parentRef = new ParentReference();
      parentRef.setStore(STORE);
      parentRef.setUuid(parentNodeId);
      parentRef.setAssociationType(Constants.ASSOC_CONTAINS);
      parentRef.setChildName(Constants.ASSOC_CONTAINS);
     
      NamedValue[] nv = new NamedValue[1];
      NamedValue n = new NamedValue(Constants.PROP_NAME, false, f.getName(), null);
      nv[0] = n;
     
      CMLCreate create = new CMLCreate();
      create.setId("id1");
      create.setParent(parentRef);
      create.setType(fileType);
      create.setProperty(nv);
     
      CML cml = new CML();
      cml.setCreate(new CMLCreate[]{create});
     
      ContentFormat cf = new ContentFormat();
      cf.setMimetype(fileMimeType);
      cf.setEncoding("UTF-8");
     
      CMLWriteContent write = new CMLWriteContent();
      write.setProperty(Constants.PROP_CONTENT);
      write.setContent(getBytesFromFile(f));
      write.setFormat(cf);
      write.setWhere_id("id1");
     
      cml.setWriteContent(new CMLWriteContent[]{write});
     
      UpdateResult[] result = repositoryService.update(cml);
         
      fileCreated = true;
      }
     catch (Exception e1)
     {
         // TODO Auto-generated catch block
         e1.printStackTrace();
      }
     
     return (fileCreated);
   }
Merry christmas and happy new 2007 to everyone.

All the best,

Sergio

Outcomes