Solved

Opening documents in JSP

Posted on 2004-10-02
5
179 Views
Last Modified: 2010-04-01
Hi,

I need to open documents (MS Word, powerpoint, excel etc..) in my webpage (jsp). The code below works but the all the documents open mixed with junk chars..

   String p = "C:\\tomcat5\\webapps\\TMN\\Despesas.xls";
//     String p = "C:\\tomcat5\\webapps\\TMN\\motivacao.doc";
   
   boolean ok = true;
   ok = p!=null;
   if (ok) {
      if (p.indexOf(".html")>-1) {
         response.setContentType("text/html");
      } else if (p.indexOf(".gif")>-1) {
         response.setContentType("image/gif");
      } else if (p.indexOf(".pdf")>-1) {
         response.setContentType("application/pdf");
      } else if (p.indexOf(".doc")>-1) {
         response.setContentType("application/msword");
      } else if (p.indexOf(".xls")>-1) {
         response.setContentType("application/excel");            
      } else {
         ok = false;
      }
   }
   if (ok) {
      try {
         int l = (int) new File(p).length();
         response.setContentLength(l);
         byte[] b = new byte[l];
         FileInputStream f = new FileInputStream(p);
         f.read(b);
         ServletOutputStream o = response.getOutputStream();
         o.write(b,0,l);
         o.flush();
         o.close();
         f.close();
      } catch (Exception e) {
         ok = false;
      }
   }
   if (!ok) {
      response.sendError(HttpServletResponse.SC_BAD_REQUEST);
   }

How can I resolve this ?

TIA,
0
Comment
Question by:sgaucho
  • 3
5 Comments
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
Comment Utility
You have to set content type of JSP
0
 
LVL 11

Accepted Solution

by:
sompol_kiatkamolchai earned 100 total points
Comment Utility
response.setContentType("String of mime type");
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
Comment Utility
response.setContentType("application/vnd.ms-excel");
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Marketing can be an uncomfortable undertaking, especially if your material is technology based. Luckily, we’ve compiled some simple and (relatively) painless tips to put an end to your trepidation and start your path to success.
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now