Posted by: sggoyal | April 2, 2009

JSP/Java Code to Download csv file

<!–JSP Code of Download File

By Saurabh Goyal <sggoyal@gmail.com> –>

<%@ page contentType=”text/html; charset=iso-8859-1″ language=”java” import=”java.sql.*,javax.servlet.*,java.io.*” errorPage=”" %>

 

<%

try { 

 

String ROOT_PATH;

// Path of WebApplication Directy

ROOT_PATH = getServletContext().getRealPath(“/”);

FileInputStream in = new FileInputStream(ROOT_PATH+”syssum.csv”);// Here syssum.csv is source file to download

response.setContentType(“application/csv”);//Sets the MIME header 

response.setHeader(“Content-Disposition”, “attachment; filename=syssum.csv”);// Here syssum.csv is name of file to download

 

int i; 

while ((i=in.read()) != -1) { 

out.write(i); 

in.close(); 

out.close(); 

 

}

catch(Exception e) 

System.out.println(“…error while loading: “+e.toString());

%><html><head></head><body>Error: can’t open file syssum.csv</body></html><%

}

%>


Leave a response

Your response:

Categories