Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

download a csv file from jsp

 
krussi rong
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krussi:
You might be interested in this thread....
Seems like the download works this way.
- satya
 
mahadevan raja
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Les Dsouza
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic