aspose file tools*
The moose likes JSP and the fly likes jsp with POI FILE System concept. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "jsp with POI FILE System concept." Watch "jsp with POI FILE System concept." New topic
Author

jsp with POI FILE System concept.

muthu bharathi
Ranch Hand

Joined: Dec 10, 2008
Posts: 97
Hi,

I have one program that makes excel reports
from an MYSQL using POI. This program saves the reports to
a Tomcat server on my system. I have a JSP called DisplayReports ,
When i clicked on DisplayReports.jsp, it is suppose to run Downloads.jsp, the
Open/Save dialog box opens and then it down loads to the user
specified directory. The relevant portion of the code in Download.jsp
looks like this:

String filepath = dbBean.getDataPath();
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition",
"attachment; filename=\"" + filename + "\"");

POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filepath
+ filename));
HSSFWorkbook wb = new HSSFWorkbook(fs);
FileOutputStream fileOut = new FileOutputStream(filename);

wb.write(fileOut);
fileOut.close();

It downloads the excel workbook with the correct name but their is no
data. What do I need to add?


--
with thanks
M. Bharathi

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39814
    
  28
Please use a more informative thread title than "JSP."
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42601
    
  65
For starters, you can't stream binary content from a JSP. You need to use a servlet for this.

Secondly, what's the purpose of using POI for this? You can read the file into a byte[] (using FileInputStream), and then stream the contents of that array to the servlet output stream. No need to use the POI API.

I don't understand what the FileOutputStream is supposed to accomplish. It would write to a file on the server, which doesn't help if you want to stream the file contents to the client.


Ping & DNS - my free Android networking tools app
muthu bharathi
Ranch Hand

Joined: Dec 10, 2008
Posts: 97
ulf,

ok.. thanks now im understand my mistake. i was changed my code ,it retrieves the data

But the data contains machine code with inner text..

Here with i've included my code. please rectify my error

response.setHeader("Content-Type", "text/");
response.setHeader("Content-Disposition","attachment; filename=\"" + f.getName() + "\";");



FileInputStream fis = null;
try {
fis = new FileInputStream(downpath);
byte[] buf = new byte[4 * 1024]; // 4K buffer
int bytesRead;
while ((bytesRead = fis.read()) != -1) {
((javax.servlet.jsp.JspWriter)out).print((char)bytesRead);
}
out.flush();
response.flushBuffer();
}
My data is like this :

ÐÏࡱá;þÿ  þÿÿÿþÿÿÿ

How can i convert it into my format..

-- with regards
M. Bharathi
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42601
    
  65
You're using the wrong content type; see this.

You should read big chunks of data in one go, not every single byte one at a time (you're not using the buffer you allocated); see this for some inspiration.

Also, it will not work reliably from within a JSP (which are for textual contents, not binary data). You're asking for trouble by doing that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jsp with POI FILE System concept.