How to retrieve video from mySQL database?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kim Charlene
    New Member
    • Jan 2011
    • 2

    How to retrieve video from mySQL database?

    Hi, guys! I've created a java servlet where I can upload video with limited size. The video uploaded is being saved in the mySQL database as BLOB. How can I retrieve that BLOB and display it as the video in my webpage? I'm new to this so I'll need all the help and guidelines you guys can give me. Thank you very much in advance. Below is my servlet code:

    Code:
    package Projects;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.sql.*;
    import javax.servlet.http.HttpSession;
    import java.util.*;
    import org.apache.commons.fileupload.disk.*;
    import org.apache.commons.fileupload.servlet.*;
    import org.apache.commons.fileupload.*;
    import java.io.File;
    
    public class processAddProjects extends HttpServlet {
    
        /** 
         * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
         */
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            try {
    
                HttpSession session = request.getSession();
    
                String Title = request.getParameter("Title");
                String Description = request.getParameter("Description");
                String extension = "";
                String userName = "root";
                String password = "fyp2010";
                //Create a factory for disk-based file items
                DiskFileItemFactory factory = new DiskFileItemFactory();
                // Create a new file upload handler
                ServletFileUpload upload = new ServletFileUpload(factory);
                upload.setSizeMax(1000000000); /* the unit is bytes */
    
                // parse this request by the handler
                // this gives us a list of items from the request
                List items = upload.parseRequest(request);
                // Process the uploaded items
                Iterator iter = items.iterator();
    
                while (iter.hasNext()) {
                    System.out.println("3");
                    FileItem item = (FileItem) iter.next();
                    // Process a regular form field
    
                    // check if the current item is a form field or an uploaded file
                    //not a form field
                    if (!item.isFormField()) {
                        // the item must be an uploaded file save it to disk. Note that there
                        // seems to be a bug in item.getName() as it returns the full path on
                        // the client's machine for the uploaded file name, instead of the file
                        // name only. To overcome that, I have used a workaround using
                        // fullFile.getName().
                        // save it to disk
                        File fullFile = new File(item.getName());
                        String filename = fullFile.getName();
                        if (!filename.isEmpty()) {
                            //Make sure path contains dot (".") - separator of file from properties
                            int indexofdot = filename.indexOf('.');
                            extension = filename.substring(indexofdot);
                            File uploadedFile = new File(getServletContext().getRealPath("/Images/"), Title + extension);
                            item.write(uploadedFile);
                        }
                        //if the current item is a form field
                    } else {
                        String fieldname = item.getFieldName();
                        String value = item.getString();
    
    
                        if (fieldname.equals("Title")) {
                            Title = value;
                        }
                        if (fieldname.equals("Description")) {
                            Description = value;
                        }
    
    
                    }
                }
    
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                String connURL = "jdbc:mysql://localhost:3306/db1";
                Connection conn = DriverManager.getConnection(connURL, userName, password);
                Statement stmt = conn.createStatement();
    
                String SQL = "Insert into projects_tbl(Title,Description,Project)VALUES('" + Title + "','" + Description + "','" +  Title + extension + "')";
    
                System.out.println("2");
    
                int numofrowsaffected = stmt.executeUpdate(SQL);
                System.out.println("3");
                if (numofrowsaffected > 0) {
                    out.println("Your Post had been updated successfully");
                } else {
                    out.println("Unsuccessful updated of Post");
    
                }
            } catch (Exception e) {
                System.out.println(e);
    
            } finally {
                out.close();
            }
        }
    
        // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
        /** 
         * Handles the HTTP <code>GET</code> method.
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
         */
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }
    
        /** 
         * Handles the HTTP <code>POST</code> method.
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
         */
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }
    
        /** 
         * Returns a short description of the servlet.
         * @return a String containing servlet description
         */
        @Override
        public String getServletInfo() {
            return "Short description";
        }// </editor-fold>
    }
Working...