sandeepmur
Programmer
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
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