Download file from Mysql database

Discussion in 'Java JSP/Servlet' started by puk284, Apr 25, 2010.

  1. puk284

    puk284 New Member

    Hi,

    I have written the following code to download a file from a MySql database using a Servlet.The problem i have is that i havnt done it right. Can someone please point me in the right direction. Any help will be greatly appreciated. Thank you for your help.

    Code:
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.sql.Blob;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    import javax.servlet.ServletException;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /
    Servlet implementation class datadownload
    /
    public class datadownload extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    /
    @see HttpServlet#HttpServlet()
    /
    public datadownload() {
    super();
    // TODO Auto-generated constructor stub
    }
    
    /
    @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    /
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    
    }
    
    /
    @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    /
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String connectionURL = "jdbc:mysql://localhost/filestore";
    Connection con = null;
    Statement stmt= null;
    ResultSet res = null;
    response.setContentType("application/octet-stream");
    
    try{
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection(connectionURL, "root", "");
    stmt=con.createStatement();
    res =stmt.executeQuery("select * from save_file where id='15'");
    if (res .next()) {
    Blob File=res .getBlob("file");
    InputStream x=File.getBinaryStream();
    int size=x.available();
    ServletOutputStream Fileout = response.getOutputStream();
    FileInputStream f = new FileInputStream("");
    int nextByte = f.read();
    
    while ( nextByte != -1 ) {
    out.write(nextByte);
    nextByte = f.read();
    }
    out.flush();
    }
    }
    
    
    
    
    catch(Exception e){
    System.out.println("Exception :"+e);
    }
    finally{
    try{
    stmt.close();
    con.close();
    }
    catch(Exception e){
    System.out.println(e);
    }
    }
    }
    } 
    
    
  2. IreneAdler

    IreneAdler New Member

    Are you getting any error when you compile because it
  3. RoseAngel

    RoseAngel New Member

    What is the error message you are getting?

Share This Page