File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes response.setContentType not working in case of Excel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "response.setContentType not working in case of Excel" Watch "response.setContentType not working in case of Excel" New topic
Author

response.setContentType not working in case of Excel

Swapnil Gaurshettiwar
Greenhorn

Joined: Dec 16, 2008
Posts: 10
I have requirment in which a document to be open depends on the document type,

My code is as follows,

String FileName = request.getParameter("FileName");
String FileExt = request.getParameter("FileExt");

String p = FileName+"."+FileExt;

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.

response.setContentLength(output.length+1000);
response.getOutputStream().write(output);

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: 70
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
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.

Here's a generic FileServlet example which may be of use: http://balusc.blogspot.com/2007/07/fileservlet.html


response.setContentLength(output.length+1000);

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 ]
Swapnil Gaurshettiwar
Greenhorn

Joined: Dec 16, 2008
Posts: 10
Hey Guys,

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
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
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
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
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 ).

Can you share code snippet if you have any.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Then your code logic may be bogus.

You at least can find here an example: http://balusc.blogspot.com/2007/07/fileservlet.html

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
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

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 .


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
galne vaibhav
Greenhorn

Joined: Nov 30, 2009
Posts: 9
Hi SWAPNIL please try this simple example you need ony one html one jsp and jxl jar








 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: response.setContentType not working in case of Excel