writing excel file to Outputstream in jsp
Hi,
My requirement is reading an xl sheet file from the server(at specified location) as a binary data(byte stream) and i want to
write that data into the ServletOutputStream in a jsp page. i tried in different ways but am getting illegal state
exception.The reason for this exception, in the jsp page there is the implicit "out" object which is the PrintWriter object
which in turn is a character stream object .Using one "response" we can get only one stream object which the already
available out object but am in need of byte stream object to writer the xl file into the response stream.
The below code is working properly if i write the whole code in a single line but this not our coding standard so i need a
better solution .
Even i tried with the same writer object but the data which am getting from response is a corrupted one.
am giving the code below which i used . In some version of tomcat server it is running properly but i need to run in jboss
server 3.0.28
this is a jsp page:
<%@ page import="java.io.*" %>
<%
try{
OutputStream servletOutputStream = response.getOutputStream();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment;filename=report.xls");
File file = new File(request.getParameter("path"));
InputStream in =new FileInputStream(request.getParameter("path"));
long length = file.length();
byte[] bytes = new byte[(int) length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead=in.read(bytes, offset, bytes.length-offset)) >= 0)
{
offset += numRead;
}
servletOutputStream.write(bytes);
in.close();
servletOutputStream.flush();
servletOutputStream.close();
System.out.println("pathis:"+file.getAbsolutePath());
}catch(Exception e)
{
e.printStackTrace();
}%>
"path" variable is nothing but the xl sheet file location in jboss server.
pls suggest me some other way to achieve this.
To run this download.jsp just create a simple.jsp with one form with one text box to give the file location of the xl sheet , and give action attribute value as download.jsp
_________________
karthikeyan