We have a database full of old resumes that are now necessary to access. They are saved in different formats, html, txt, doc, docx. I am using this code to open them.
Every format opens successfully, except docx. In Firefox, I get errors that the xml is corrupted, but if I just keep clicking through the errors, the resume will open in Word. In IE, I get a window that says do you want to open or save this file-it knows the file type is MSWord. I click OK and it doesn't do anything. If I save it to my computer, then click open, I have to find Word in the list of applications to open it. Then I get the same errors that Firefox gives, errors with the contents, click through the errors, it opens. I have asked at least 10 people to try this on their machines and everyone of us gets the same result. I have a newer XP with MS Office 2007. Others with Windows7 also gets the same result. This happens across the board on docx - there are hundreds of them in the database, so it's not just one corrupted file.
Did you try the following content type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
Edit: oops... seems like you did... is this content type set correctly for the docx files?
How about application/octet-stream?
Joined: Jan 10, 2008
Yes, Bear, Thank you!
Koen, that is exactly the code that I used and it does not work.
Jan Piper wrote: I am using this code to open them.
But that code doesn't "open" anything. I realize that people use the word "open" for all kinds of vaguely-defined processes, but that code just sets the content-type of the HTTP response and does absolutely nothing with any documents at all.
You already seem to be considering the idea that the documents are being corrupted by something in the process which sends them from the browser to the database. You should continue looking at that idea, since that is most likely the problem.
You've already confirmed that it makes no difference what MIME type you use, so that's just a red herring now. It's time to look at the code which transfers the data from the database to the browser. The other possibility is that the data was corrupted by the process of putting it into the database.
Joined: Jan 10, 2008
I am converting the data to a string and then opening by PrintStream.
I just find it very strange that the only data corrupted was docx. I spent the evening looking for other solutions and all I can find are other people with the same problem, but no solutions.
Are you actually saying you've turned binary data into a String? It then surprises me that you haven't run into problems before. Binary data can contain many bytes that have no valid char representation. Turning that data into a String can cause implicit conversions to take place, which is what will corrupt your file.
To write binary data:
1) Never use JSPs; any line break between directives will be included and can (will) cause problems.
2) Never use response.getWriter() but use response.getOutputStream()
3) Write the data as bytes (byte) and never as chars or Strings.