This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

jsp with POI FILE System concept.  RSS feed

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Marshal
Posts: 60083
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use a more informative thread title than "JSP."
 
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!