cancel
Showing results for 
Search instead for 
Did you mean: 

MS word upload using Java problem

hamada_basiouny
Champ in-the-making
Champ in-the-making
Hi
I am trying to  upload ms word and excel to alfresco using Java, I made a web application using jsf with Trinidad upload every thing is fine for images and PDF but when uploading word file it uploaded to alfresco, The problem is when I try to downland it the file is corrupted and office couldn't open it.


ParentReference parent = connecttoAlfUtils.ReferenceToParent(parentref);

      parent.setChildName(Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, connecttoAlfUtils.normilizeNodeName(documentProperties.getFileName())));

      NamedValue[] properties = new NamedValue[] { Utils.createNamedValue(Constants.PROP_NAME, documentProperties.getFileName()),
// I put content-type static her to test  if it works but it does not               
Utils.createNamedValue("{http://www.alfresco.org/model/content/1.0}content-type", "application/msword"),
                Utils.createNamedValue("{http://www.alfresco.org/model/content/1.0}creator", documentProperties.getUserName()),
                Utils.createNamedValue("{http://www.alfresco.org/model/content/1.0}description", documentProperties.getFileDesc())};

                CMLCreate create = new CMLCreate("1", parent, null, null, null, Constants.TYPE_CONTENT, properties);
      CML cml = new CML();
      cml.setCreate(new CMLCreate[] { create });

      // Execute the CML create statement

      UpdateResult[] results = null;

      try {
         System.out.println("Creating the document " + documentProperties.getFileName());
         results = connecttoAlfUtils.getRepositoryService().update(cml);
         document = results[0].getDestination();
      } catch (Exception e) {
         System.err.println("Can not create the document.");
         throw e;
      }

      // Set the content

      ContentFormat format = new ContentFormat(Constants.MIMETYPE_TEXT_PLAIN, "UTF-8");
         
                try {
                    System.out.println("Setting the content of the document");
                    connecttoAlfUtils.getContentService().write(document, Constants.PROP_CONTENT, documentProperties.getContent(), format);
                    isSavedInAlfresco = true;
                } catch (Exception e2) {
                    System.err.println("Can not set the content of the document.");
                    throw e2;
                }
2 REPLIES 2

jcustovic
Champ in-the-making
Champ in-the-making
You don't have to set "content-type" or "creator". When i create content I use this command:
CMLCreate create = new CMLCreate("1", parent, null, Constants.ASSOC_CONTAINS, null, Constants.TYPE_CONTENT, properties);

And try to set null for format when you upload document like this:
getContentService().write(reference, Constants.PROP_CONTENT, content, null);

You should also make sure that documentProperties.getContent() contains all the bytes of your document (when you load the document maybe you didn't read all your bytes, especially if you download document and didn't use buffered input).

hamada_basiouny
Champ in-the-making
Champ in-the-making
Dear jcustovic

Thanks for replay, I actually solve this problem your second suggestion was right because documentProperties.getContent() for some files (doc , xls , png and others) return with wrong array of byte and for other files (pdf , jpg ) it returns with true array of byte, I don't know why but now it works fine.

I use Trinidad upload, I set "creator" for a security issue.

public void fileUpload(ValueChangeEvent event) throws IOException {
        UploadedFile file = (UploadedFile) event.getNewValue();
        FacesContext context = FacesContext.getCurrentInstance();
        InputStream stream = file.getInputStream();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int next = stream.read();
        while (next > -1) {
            bos.write(next);
            next = stream.read();
        }
        bos.flush();
        byte[] buffer = bos.toByteArray();
       documentProperties.setContent() = buffer ;

the previous code works fine.