• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to download csv file  RSS feed

Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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="java.io.*"%>
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 = window.open("", 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 = fis.read()) != -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 java.io.*;
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, java.io.IOException
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( ( c=in.read() ) != -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 ]
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Linda,

I'm facing the exact same problem you posted. Please let me if you have found the solution for it. Thank you.
Author and ninkuma
Posts: 66424
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This would be more appropriate in the Servlets fourm. Moved.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!