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

How to download csv file

Linda Thomas
Ranch Hand

Joined: Jun 21, 2004
Posts: 36
I have the code in my class that creates a .csv file on the server and now I'm trying to provide a link that will open up a save window where the user can download it to their own computer.

My link <a href="<%=URL_BASE%>/student_services/counsellors/statistics.csv">click here</a> where URL_BASE is a constant variable but it opens up an browser window with the contents of statistics.csv. If I right click and save target as it wants to save as a .html file and even though I can change the extension to .csv I'm not sure that it will save correctly.

Besides my supervisor wants a pop-up box that lets the user save to the destination they want when you click the link.

I've found some code on another site but its not clear enough to me on how to use it.

I got:
<%@ page language="java" import="*"%>
function openErr(file){
//alert("file "+file);

var windowParams="toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=20,height=20,left=0,top=0";
function toOpen(file) {
win1 ="", winName, windowParams);
window.setTimeout('toCloseWin()', 5000);

function toCloseWin(){


Then in jsp
// download
String filePath="";
String fileNameNew="statistics.csv";
// response.setContentType("application/x-msdownload");
// or
// response.setContentType("application/unknown");
response.addHeader("Content-Disposition", "attachment; filename="+fileNameNew);
File uFile= new File(filePath);
int fSize=(int)uFile.length();
FileInputStream fis = new FileInputStream(uFile);
PrintWriter pw = response.getWriter();
int c=-1;
// Loop to read and write bytes.

while ((c = != -1){
// Close output and input resources.
}catch(Exception e){

NOTE: I have no idea why there is code there that is commented out. I found response.setContentType("text/csv"); on my own and have replaced their options of
// response.setContentType("application/x-msdownload");
// or
// response.setContentType("application/unknown");
with it instead.

The link was
<a target="_blank" href="<%=URL_BASE%>/student_services/counsellors/statistics.csv">
and for some reason they added
<form name="downloadFrm" action="" method="post">
<INPUT TYPE="hidden" name="fileName">
under it.

Also included was a DownloadServlet class
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public final class DownloadServlet extends HttpServlet {
private static final String basePath = "/doc";

public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException,
String filePath = request.getPathInfo();
String filename = request.getParameter( "filename" );
if( filePath == null && filename != null ) filePath = "/" + filename;
if( filename == null ) filename = filePath;
if( filename != null ) filename = (new File(filename)).getName();

if( filePath != null ) {
InputStream in = null;
OutputStream out = null;
try {
in = getServletContext().getResourceAsStream(basePath + filePath);
if( in != null ) {
out = new BufferedOutputStream( response.getOutputStream() );
in = new BufferedInputStream( in );
String contentType = "application/unknow";
System.out.println( "contentType: " + contentType );
response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
int c; while( ( ) != -1 ) out.write( c );
} finally {
if( in != null ) try { in.close(); } catch( Exception e ) {}
if( out != null ) try { out.close(); } catch( Exception e ) {}
response.sendError( HttpServletResponse.SC_NOT_FOUND );

The code is very confusing and I think filename should be statistics.csv so I have replaced it but not sure if I have in the right places.

Can anyone help?
[ October 07, 2005: Message edited by: Linda Thomas ]
Lewis Lam

Joined: Jan 31, 2006
Posts: 1
Dear Linda,

I'm facing the exact same problem you posted. Please let me if you have found the solution for it. Thank you.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 62275

This would be more appropriate in the Servlets fourm. Moved.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: How to download csv file