aspose file tools*
The moose likes Servlets and the fly likes docx won't open Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "docx won Watch "docx won New topic
Author

docx won't open

Jan Piper
Greenhorn

Joined: Jan 10, 2008
Posts: 13
I need help.

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.

Any ideas?

Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

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?
Jan Piper
Greenhorn

Joined: Jan 10, 2008
Posts: 13
Yes, Bear, Thank you!
Koen, that is exactly the code that I used and it does not work.
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Jan Piper wrote:Yes, Bear, Thank you!
Koen, that is exactly the code that I used and it does not work.

What about the more generic application/octet-stream?
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Also, are the file extensions all lower case? Because .DOCX won't match your condition, but .docx would.
Jan Piper
Greenhorn

Joined: Jan 10, 2008
Posts: 13
Using octet-stream, I have to save it. Then choose Word, then the same errors. Yes, the ext is .docx, not .DOCX.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18675
    
    8

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.
Jan Piper
Greenhorn

Joined: Jan 10, 2008
Posts: 13
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Christophe Maillard
Greenhorn

Joined: May 03, 2012
Posts: 2
I had the same problem. I found a solution: specifying the "Content-Length" header as I explain here. So, in JAVA, I just needed to add this line to make it work:



Hope it could help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: docx won't open