Solved

Opening documents in JSP

Posted on 2004-10-02
5
182 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
ID: 12215515
You have to set content type of JSP
0
 
LVL 11

Accepted Solution

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

Expert Comment

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java cyclic graphs 15 212
Using Tomcat as raspberry pi java app server 13 192
designing in object programming 12 94
running on tomcat not jboss eap 7.0 3 18
This article describes how to reset your Windows 10 password when you've forgotten it.
The advancement in technology has been a great source of betterment and empowerment for the human race, Nevertheless, this is not to say that technology doesn’t have any problems. We are bombarded with constant distractions, whether as an overload o…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

827 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