File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "download a csv file from jsp" Watch "download a csv file from jsp" New topic
Author

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="java.io.*,javax.servlet.*,java.util.* " %>
<%
String path ="../downfile/";
String filename="transaction.csv";
response.setContentType("application/octet-stream");
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 = stream.read()) != -1) {
out.write(iRead);
}out.flush();
}
finally {
if (stream != null) {
stream.close();
}
}
%>
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
testdownload.jsp
does anybody know how to do it?
thanks in advance!
Krussi
Madhav Lakkapragada
Ranch Hand

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


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
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
/<server-path>/transaction.csv
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
Greenhorn

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
testdownload.jsp?
thanks
Krussi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: download a csv file from jsp