File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes download a csv file from jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "download a csv file from jsp" Watch "download a csv file from jsp" New topic

download a csv file from jsp

krussi rong
Ranch Hand

Joined: Jan 30, 2002
Posts: 62
I want user download some datas from database in form of *.csv.
I used <a href = "testdownload.jsp> download
to link testdownload.jsp that code as follow:

<%@ page import="*,javax.servlet.*,java.util.* " %>
String path ="../downfile/";
String filename="transaction.csv";
response.setHeader("Content-Disposition","attachment; filename=" + "transaction.csv");
int iRead;
FileInputStream stream = null;
try {File f = new File(path+filename);
stream = new FileInputStream(f);
while ((iRead = != -1) {
finally {
if (stream != null) {
It didn't work, I want user download trasactions.csv but the save as window
gave me information save testdownload.jsp
and then gave errors information can't save
does anybody know how to do it?
thanks in advance!
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
You might be interested in this thread....
Seems like the download works this way.
- satya

Take a Minute, Donate an Hour, Change a Life
mahadevan raja
Ranch Hand

Joined: Apr 11, 2001
Posts: 96
hi krussi,
just now i saw ur query,i dont know whether i have taken ur query in the right sense or not,but as far as i have understood i shall suggest u one thing,
u click the link which calls the xxx.jsp in which u have written a code to write to a transaction.csv.
hope i am right.
u do one thing,u provide a link in this jsp file which calls the same transaction.csv.
let me be more clear.
suppose u have created a file (csv) in
then give a link in the jsp file,
<a href="/<server-path>/transaction.csv">Open csv file </a>
so now when u click this link u will get a file download message box,i think this is what u needed
In short,
give a link in the jsp file,which calls the transaction.csv.
hope u r not confused.
if confused,reply soon.
Remember in the file download option u have a check box,stating always ask before opening such kind of file,let it be checked.
[ February 04, 2002: Message edited by: mahadevan raja ]

SPECIALIST-WCSV5.1 Implementation.<br />SYSTEMS EXPERT-WAS4.0 Administration
Les Dsouza

Joined: Jan 29, 2002
Posts: 27
I tried the following code, it works...

If you already have a file created then its better to directly give a link to it.
[ February 05, 2002: Message edited by: Les Dsouza ]
krussi rong
Ranch Hand

Joined: Jan 30, 2002
Posts: 62
Thanks everybody for help!
My task is let user download some datas from our
database in form of *.csv. when user click download link,it should be form a transactions.csv
file according to user's query automatically,then the browser give the user a file download window.
I tried many ways, but didn't work well.
First way:
I formed data file transactions.csv first, then
used <a href="transactions.csv> download
the problem is:
but when I click the link download, the browser sometimes give me a save as window, sometimes opened the transactions.csv directively.
Second way:
I used <a href="testdownload.jsp">download
link to testdownload.jsp that form data file
transactions.csv then give a user download window
let user download file. the testdownload.jsp code
I wrote in the top.
the problem is:
when I click the download link,the browser give me
a download testdownload.jsp window instead of trasaction.csv download window,then I click the button 'Ok',then give me error.
so what's the problem,how to do about that.maybe
I have something wrong about how to link a
I agree. Here's the link:
subject: download a csv file from jsp
It's not a secret anymore!