package Servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * A servlet that retrieves a file from MySQL database and lets the client
 * downloads the file.
 * @author www.codejava.net
 */
@WebServlet("/downloadServlet")
public class FileDownloadDBServlet extends HttpServlet {
 
    // size of byte buffer to send file
    private static final int BUFFER_SIZE = 4096;   
     
    // database connection settings
    private String dbURL = "jdbc:mysql://localhost:3306/FileDB";
    private String dbUser = "root";
    private String dbPass = "";
     
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // get upload id from URL's parameters
        int contactid = Integer.parseInt(request.getParameter("contact_id"));
         
        Connection conn = null; // connection to the database
         
        try {
            // connects to the database
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
 
            // queries the database
            String sql = "Select * from `contacts` where `contact_id`  = ?";
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setInt(1, contactid);
 
            ResultSet result = statement.executeQuery();
            if (result.next()) {
                // gets file name and file blob data
                String fileName = result.getString("file_name");
                Blob blob = result.getBlob("file");
                InputStream inputStream = blob.getBinaryStream();
                int fileLength = inputStream.available();
                 
                System.out.println("fileLength = " + fileLength);
 
                ServletContext context = getServletContext();
 
                // sets MIME type for the file download
                String mimeType = context.getMimeType(fileName);
                if (mimeType == null) {        
                    mimeType = "application/octet-stream";
                }              
                 
                // set content properties and header attributes for the response
                response.setContentType(mimeType);
                response.setContentLength(fileLength);
                String headerKey = "Content-Disposition";
                String headerValue = String.format("attachment; filename=\"%s\"", fileName);
                response.setHeader(headerKey, headerValue);
 
                // writes the file to the client
                OutputStream outStream = response.getOutputStream();
                 
                byte[] buffer = new byte[BUFFER_SIZE];
                int bytesRead = -1;
                 
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outStream.write(buffer, 0, bytesRead);
                }
                 
                inputStream.close();
                outStream.close();             
            } else {
                // no file found
                response.getWriter().print("File not found for the id: " + contactid);  
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            response.getWriter().print("SQL Error: " + ex.getMessage());
        } catch (IOException ex) {
            ex.printStackTrace();
            response.getWriter().print("IO Error: " + ex.getMessage());
        } finally {
            if (conn != null) {
                // closes the database connection
                try {
                    conn.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }          
        }
    }
}
@@ -945,17 +945,17 @@
 06/FileD
-B
+W
 %22;%0A    p
@@ -1014,16 +1014,26 @@
 Pass = %22
+Warstar123
 %22;%0A     
@@ -1226,23 +1226,22 @@
     int 
-contact
+upload
 id = Int
@@ -1276,23 +1276,22 @@
 ameter(%22
-contact
+upload
 _id%22));%0A
@@ -1650,37 +1650,35 @@
 rom 
-%60contacts%60 where %60contact
+file_upload where upload
 _id
-%60
   = 
@@ -1783,23 +1783,22 @@
 tInt(1, 
-contact
+upload
 id);%0A %0A 
@@ -2052,16 +2052,21 @@
 ob(%22file
+_data
 %22);%0A    
@@ -3642,15 +3642,14 @@
 %22 + 
-contact
+upload
 id);
@@ -4251,9 +4251,10 @@
 %7D%0A    %7D%0A
-
 %7D
+%0A

Update FileDownloadDBServlet.java