cancel
Showing results for 
Search instead for 
Did you mean: 

Can't download .docx

mangar
Star Contributor
Star Contributor
When I download a docx file, it downloads fine, but when it opens up in Word, it says "file corrupted" I of course click through and Word recovers the document just fine. But I have some VERY upset managers who don't like Alfresco corrupting their documents. It's not of course, because if I go into Alfresco and click on download file, it works fine, no corruption. It only happens with a web service, and It only happens with docx.

I have a simple web service that downloads a document. I give it the uuid, and I get an InputStream like this:

ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService();
Reference contentReference = new Reference(storeRef,uuid,null);
readResult = contentService.read(new Predicate(new Reference[]{contentReference}, storeRef, null), Constants.PROP_CONTENT);
is = ContentUtils.getContentAsInputStream(readResult[0]);
return is;

in my jsp, I simply send it to the response like this:

AlfrescoBridge bridge = new AlfrescoBridge();  // my handy class
InputStream in = bridge.getFileInputStream(uuid);  // method above
response.reset();
response.setHeader ("Content-Disposition", "attachment;filename=\""+fileName+"\"");
ServletOutputStream sosStream = response.getOutputStream();
int ibit = 256;
while ((ibit) >= 0)   {
     ibit = in.read();
     sosStream.write(ibit);
}
sosStream.flush();
sosStream.close();
in.close();

I am using Alfresco 3.3g
I have upgraded my OpenOffice to 3.2.1

I suspect   ContentUtils.getContentAsInputStream(readResult[0]);

Please help, as my butt, and the future of Alfresco here is on the line.
3 REPLIES 3

mrogers
Star Contributor
Star Contributor
Can you save your downloaded file to disk and then use a binary compare utility to analyse if there is a difference between the file downloaded via your web service and the file downloaded by other means.  It may be something like a "magic" byte at the start or a content encoding issue, or different linefeed characters or something else.

mangar
Star Contributor
Star Contributor
Update:

  It turns out that if I take the exact same InputStream and use the ContentUtils.convertToByteArray(is) and alter the jsp like this:


AlfrescoBridge bridge = new AlfrescoBridge(); // my handy class again
response.reset();
response.setHeader ("Content-Disposition", "attachment;filename=\""+fileName+"\"");
ServletOutputStream sosStream = response.getOutputStream();
sosStream.write(bridge.getFileBites(uuid)); // calls  ContentUtils.convertToByteArray(is)

It works for SOME .docx files, but breaks ALL the other filetypes.

So my full jsp looks basically like this:


AlfrescoBridge bridge = new AlfrescoBridge(); // handy class
response.reset();
response.setHeader ("Content-Disposition", "attachment;filename=\""+fileName+"\"");
ServletOutputStream sosStream = response.getOutputStream();
if(fileName.endsWith("docx")) {
     sosStream.write(bridge.getFileBites(fileID));   //  calls ContentUtils.convertToByteArray(is)
}
else {
    InputStream in = bridge.getFileInputStream(fileID);  // calls ContentUtils.getContentAsInputStream(contentReadResult[0]);
    int ibit = 256;
    while ((ibit) >= 0)   {
      ibit = in.read();
      sosStream.write(ibit);
    }
  sosStream.flush();
  in.close();
  }
sosStream.close();

Again, this only works for some of the docx files, not all. I cannot tell any difference between the documents.

What makes this more weird is if I go into the regular Alfresco faces app and click on the download button, it works in ALL situations with no problems.

Im not one to jump on a "bug" bandwagon, but there is clearly a problem with ContentUtils.

I will try the binary comparison, and see if I can gleam anything form that.  Do you have a suggestion for a particular tool?

mangar
Star Contributor
Star Contributor
Yet another update….

  I changed my web service to use Apache commons-ftp and used Alfresco's ftp like this:

//After cwd to the correct directory:
ByteArrayOutputStream output = new ByteArrayOutputStream();
ftp.retrieveFile(fileName, output);
return output.toByteArray();
And all is well for all files.

This does mean I need to know exactly where the file is and defeats the point of lucene and WebServiceFactory.getContentService();

I wish to formally apply for a bug. Where does one do this?