Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Part filePart = request.getPart("photo");
- /**
- *
- * @author bnbih
- */
- @WebServlet(urlPatterns = {"/uploadServlet"})
- @MultipartConfig(maxFileSize = 16177215) // upload file's size up to 16MB
- public class FileUploadDBServlet extends HttpServlet {
- // database connection settings
- private String dbURL = "jdbc:mysql://server_IP:3306/db_name";
- private String dbUser = "root";
- private String dbPass = "";
- /**
- * 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 doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- InputStream inputstream = null;//input stream of the uploaded photo
- Part filePart = request.getPart("photo");
- if(filePart != null){
- //print out file info for debugging
- System.out.println(filePart.getName());
- System.out.println(filePart.getSize());
- System.out.println(filePart.getContentType());
- //get the file
- inputstream = filePart.getInputStream();
- }
- Connection con = null;
- String message = null;
- try{
- //connect to the database
- DriverManager.registerDriver(new com.mysql.jdbc.Driver());
- con = DriverManager.getConnection(dbURL, dbUser, dbPass);
- //construct sql statement
- String sql = "INSERT INTO StudentInfo (img, student) values (?,?)";
- PreparedStatement statement = con.prepareStatement(sql);
- if(inputstream != null){
- //fetches input stream of the upload file for the blob column
- statement.setBlob(1, inputstream);
- statement.setString(2, filePart.getSubmittedFileName());
- }
- //sends the statement to the database server
- int row = statement.executeUpdate();
- if(row > 0){
- message = "Student image uploaded successfully";
- }
- }catch(SQLException ex){
- message = "Something went wrong!! see below n" + ex.getMessage() + filePart.getSubmittedFileName();
- }finally{
- if(con != null){
- //close db connection
- try{
- con.close();
- }catch(SQLException ex){
- ex.printStackTrace();
- }
- }
- //sets the message in request scope
- request.setAttribute("Message", message);
- // forwards to the message page
- getServletContext().getRequestDispatcher("/Message.jsp").forward(request, response);
- }
- response.setContentType("text/html;charset=UTF-8");
- try (PrintWriter out = response.getWriter()) {
- /* TODO output your page here. You may use following sample code. */
- out.println("<!DOCTYPE html>");
- out.println("<html>");
- out.println("<head>");
- out.println("<title>Servlet FileUploadDBServlet</title>");
- out.println("</head>");
- out.println("<body>");
- out.println("<h1>Servlet FileUploadDBServlet at " + request.getContextPath() + "</h1>");
- out.println("</body>");
- out.println("</html>");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement