File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "jsp with POI FILE System concept." Watch "jsp with POI FILE System concept." New topic

jsp with POI FILE System concept.

muthu bharathi
Ranch Hand

Joined: Dec 10, 2008
Posts: 97

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();
"attachment; filename=\"" + filename + "\"");

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


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

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

Joined: Mar 22, 2005
Posts: 42959
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.
muthu bharathi
Ranch Hand

Joined: Dec 10, 2008
Posts: 97

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 = != -1) {
My data is like this :

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

How can i convert it into my format..

-- with regards
M. Bharathi
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42959
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:
subject: jsp with POI FILE System concept.
It's not a secret anymore!