Home Jsp Download CSV File from Database in JSP
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Download CSV File from Database in JSP

Advertisement
In this section, you will learn how to download CSV file from database in JSP. In this example, we have developed two files "viewcsv.jsp" and "file_download.jsp" , code is given below.

Download CSV File from Database in JSP

     

In this section, you will learn how to download CSV  file from database in JSP.  In this example, we have developed two files "viewcsv.jsp" and "file_download.jsp" , code is given below.

Brief description of the flow of application :

1). Create a webpage "viewcsv.jsp""  to display and download the CSV file  from database. All CSV file will show as hyperlink.

2). Another  "file_download.jsp" is used to retrieve CSV file..

Step:1 To create a "file" table in Database. 

create table `file` (
`id` double ,
`file_data` blob ,
`file_name` varchar (50)
)


Step:2 Create a "viewcsv.jsp"  to display all the CSV file.

<html>

    <body>

    <br><br>

    <table width="200px" style="border:1px solid #ff0000;background-color:f7f7f7" align="center">

    <tr style="font-weight:bold;">

    <td align="center" align="center" colspan=2 style="border-bottom: 2px solid #000000;">Download Csv File</td>

    </tr>

 <tr style="font-weight:bold;">

       <td align="center" style="border-bottom: 2px solid #000000;">Id</td>

       <td align="center" style="border-bottom: 2px solid #000000;">File</td>

    </tr>

    <%@ page import="java.io.*,java.util.*,java.sql.*" %> 

    <%

    

	 String connectionURL = "jdbc:mysql://localhost/application";

	  String url=request.getParameter("WEB_URL");

	  String Content=new String("");

	  Statement stmt=null;

      Connection con=null;

    try

    {

	Class.forName("com.mysql.jdbc.Driver").newInstance();

	con=DriverManager.getConnection(connectionURL,"root","root"); 

	stmt=con.createStatement();

	String qry = "select * from file";

	ResultSet rst= stmt.executeQuery(qry);

	while(rst.next())

	{

    %>

	 <tr>

      <td align="center"><%=rst.getInt(1)%></td>

      <td align="center">

      <a href="file_download.jsp?id=<%=rst.getInt(1)%>"><%=rst.getString(3)%></a>

       </td>

    </tr>

    <%

	}

    

    }

    catch(Exception e){

	e.printStackTrace();

    }

    %>

    </table>

    </body>

    </html>

Step:3 To create a web page  "file_download.jsp" .

<%@ page import="java.io.*,java.util.*,java.sql.*" %> 

    <%

    int id=0;

    if(request.getParameter("id")!=null && request.getParameter("id")!="")

    {

  id = Integer.parseInt(request.getParameter("id").toString());

    }

	 String connectionURL = "jdbc:mysql://localhost/application";

	  String url=request.getParameter("WEB_URL");

	  String Content=new String("");

	  Statement stmt=null;

      Connection con=null;

    try

    {

	String filename="data"+id+".csv";

	 Class.forName("com.mysql.jdbc.Driver").newInstance();

      con=DriverManager.getConnection(connectionURL,"root","root"); 

      stmt=con.createStatement();

	  String qry = "select * from file where id="+id;

	  ResultSet rst= stmt.executeQuery(qry);

			if(rst.next())

			{

    

				Content=rst.getString("file_data");

			}

			out.println(Content);

		byte requestBytes[] = Content.getBytes();

		ByteArrayInputStream bis = new ByteArrayInputStream(requestBytes);

		response.reset();

		response.setContentType("application/text");

		response.setHeader("Content-disposition","attachment; filename=" +filename);

		byte[] buf = new byte[1024];

		  int len;

		  while ((len = bis.read(buf)) > 0){

				  response.getOutputStream().write(buf, 0, len);

				 }

		bis.close();

		response.getOutputStream().flush(); 

    }

    catch(Exception e){

	e.printStackTrace();

    }

    %>

Output:

Display CSV file :



Download the CSV File :

Download the application

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 26, 2008

Related Tutorials

Ask Questions?    Discuss: Download CSV File from Database in JSP   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
weiren
August 16, 2011
help with downloading of pdf files from mysql

i tried your code with downloading of pdf files but after i download the pdf file i open it with adobe but is displays a blank page
ady
August 19, 2011
Download CSV File from Database in JSP

Hi your discussion is very useful! But i have a problem, ive tried to get a zip data from database, followed your code and changed String filename="data"+id+".csv"; to .zip. The problem is, im having a an error and instead of opening the file, it just printing the output file to the page. please advise
cuddlefish
August 24, 2011
Download code

I have an issue with this code....it works fine all the way to actual downloading.However after the actual downloading....all the files am using are of 18.0kb which is the wrong size of the files and when i try opening it they are empty.
Anil
September 9, 2011
want data1.cvs

pls help me to solve my prom....i love your site...
Sukhdev Singh
November 16, 2011
java

nice site but your codes often do not run accurately,particularly that of JSP.....:(
arjun
January 17, 2012
download files from database in jsp

what if we want to download files other than .csv as in .txt,.png,.jpg etc what then?????
haneef
June 20, 2012
jsp

i want download all column from table tel me code
Solomon Fose
November 18, 2012
data csv file download

Download csv data file
vinay
November 23, 2012
REGARDING FILE DOWNLOADING

sir when i run this code i m not able to download its showing null but there is file in database in binary form at so please help me its urgent
DMCA.com