if (p.indexOf(".txt")>-1) { response.setContentType("text/plain"); } else if (p.indexOf(".xls")>-1) { response.setContentType("application/vnd.ms-excel"); } else if (p.indexOf(".pdf")>-1) { response.setContentType("application/pdf"); } else if(p.indexOf(".doc")>-1) { response.setContentType("application/msword"); } else if (p.indexOf(".xlsx")>-1) { response.setContentType("application/vnd.ms-excel"); }else if(p.indexOf(".docx")>-1) { response.setContentType("application/msword"); } else { }
response.setHeader("Content-Disposition","Attachment;filename= "+p); byte[] output = ATT1[0].getData(); //ATT1[] is output of an Webservice call which contain the actual data of a document in byte[] array form.
The code is working fine for all document type(*.doc,*.pdf,*.txt) , I dont understand why its not working for *.xls or *.xlsx (excel application).The excel document get corrupted.
Any help on this issue will be appreciable. Thanks.
Nitin Pathak
Ranch Hand
Joined: Sep 25, 2006
Posts: 68
posted
0
To me, there does not seem to be an obvious problem with your code. Try out the following options:
changing the setContentType("application/x-ms-excel"); for xls and try to flush the output stream after writing to it:
Bauke Scholtz
Ranch Hand
Joined: Oct 08, 2006
Posts: 2458
posted
0
Originally posted by Swapnil Gaurshettiwar: I have requirment in which a document to be open depends on the document type
Rather use ServletContext#getMimeType() and add missing mime types to the web.xml, for example:
For an overview of all valid mime types, consult the documentation at microsoft.com. Google may help a lot in this.
Why +1000? That would most likely corrupt the response. The content length must be exactly the same length as the amount of bytes you write. [ December 16, 2008: Message edited by: Bauke Scholtz ]
thanks for your suggestions, I tried by making above changes,but same problem persist. m I missing anything mandatory ?
can anyone suggest me some other approach ? It will be really helpful.
thanks, Swapnil.
Bauke Scholtz
Ranch Hand
Joined: Oct 08, 2006
Posts: 2458
posted
0
Originally posted by Swapnil Gaurshettiwar: thanks for your suggestions, I tried by making above changes,but same problem persist. m I missing anything mandatory ?
Apparently. You must be doing it wrong. To exclude things, is the code been placed in a servlet or a JSP? Don't you see anything in the appserver's logs? Don't you suppress exceptions?
can anyone suggest me some other approach ? It will be really helpful.
Why? There is no other approach. The suggested approach is the right way and it should work.
Swapnil Gaurshettiwar
Greenhorn
Joined: Dec 16, 2008
Posts: 10
posted
0
I have written this code on JSP and I am using BEA Weblogic Workshop 8.3
writting the code on servelet will make a difference ?
Bauke Scholtz
Ranch Hand
Joined: Oct 08, 2006
Posts: 2458
posted
0
JSP is there for presentation only. You shouldn't write raw Java code in a JSP file. It's only asking for trouble. Write Java code in Java classes. The JSP may add unwanted whitespace or linefeeds to the binary output which would break it completely.
Swapnil Gaurshettiwar
Greenhorn
Joined: Dec 16, 2008
Posts: 10
posted
0
I tried to write the same code with the help of Servelet and done all necessary mappings,but no use the same problem persist only for excel files(.xls or .xlsx ).
You only need to add xlsx mime type to the web.xml, if not done yet.
Swapnil Gaurshettiwar
Greenhorn
Joined: Dec 16, 2008
Posts: 10
posted
0
the code is there in the first post,could yoy please tell what you find bugus there ??? Atleast I cant find anything and if it is then why its working for other file formats and not only for .xls ???
I alrady added entries in web.xml and explicitly specified response.setContentType.
Originally posted by Swapnil Gaurshettiwar: the code is there in the first post
Please be sure to use UBB code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.
You can go back and change your post to add code tags by clicking the .