Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

response.setContentType not working in case of Excel

 
Swapnil Gaurshettiwar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
galne vaibhav
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi SWAPNIL please try this simple example you need ony one html one jsp and jxl jar








 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic