This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

jsp with POI FILE System concept.

 
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: 67375
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use a more informative thread title than "JSP."
 
Rancher
Posts: 43011
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: 43011
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.
 
Crusading Chameleon likes the size of this ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!