Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Opening documents in JSP

Posted on 2004-10-02
5
Medium Priority
?
189 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 400 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

885 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