Advertisement
Guest User

Untitled

a guest
Aug 10th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. Part filePart = request.getPart("photo");
  2.  
  3. /**
  4. *
  5. * @author bnbih
  6. */
  7. @WebServlet(urlPatterns = {"/uploadServlet"})
  8. @MultipartConfig(maxFileSize = 16177215) // upload file's size up to 16MB
  9. public class FileUploadDBServlet extends HttpServlet {
  10.  
  11. // database connection settings
  12. private String dbURL = "jdbc:mysql://server_IP:3306/db_name";
  13. private String dbUser = "root";
  14. private String dbPass = "";
  15. /**
  16. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  17. * methods.
  18. *
  19. * @param request servlet request
  20. * @param response servlet response
  21. * @throws ServletException if a servlet-specific error occurs
  22. * @throws IOException if an I/O error occurs
  23. */
  24. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  25. throws ServletException, IOException {
  26.  
  27. InputStream inputstream = null;//input stream of the uploaded photo
  28.  
  29. Part filePart = request.getPart("photo");
  30. if(filePart != null){
  31.  
  32. //print out file info for debugging
  33. System.out.println(filePart.getName());
  34. System.out.println(filePart.getSize());
  35. System.out.println(filePart.getContentType());
  36.  
  37. //get the file
  38. inputstream = filePart.getInputStream();
  39.  
  40. }
  41. Connection con = null;
  42. String message = null;
  43.  
  44. try{
  45. //connect to the database
  46. DriverManager.registerDriver(new com.mysql.jdbc.Driver());
  47. con = DriverManager.getConnection(dbURL, dbUser, dbPass);
  48.  
  49. //construct sql statement
  50. String sql = "INSERT INTO StudentInfo (img, student) values (?,?)";
  51. PreparedStatement statement = con.prepareStatement(sql);
  52.  
  53. if(inputstream != null){
  54. //fetches input stream of the upload file for the blob column
  55. statement.setBlob(1, inputstream);
  56. statement.setString(2, filePart.getSubmittedFileName());
  57. }
  58.  
  59. //sends the statement to the database server
  60. int row = statement.executeUpdate();
  61.  
  62. if(row > 0){
  63. message = "Student image uploaded successfully";
  64. }
  65.  
  66.  
  67. }catch(SQLException ex){
  68. message = "Something went wrong!! see below n" + ex.getMessage() + filePart.getSubmittedFileName();
  69. }finally{
  70. if(con != null){
  71. //close db connection
  72. try{
  73. con.close();
  74. }catch(SQLException ex){
  75. ex.printStackTrace();
  76. }
  77. }
  78.  
  79. //sets the message in request scope
  80.  
  81. request.setAttribute("Message", message);
  82.  
  83. // forwards to the message page
  84. getServletContext().getRequestDispatcher("/Message.jsp").forward(request, response);
  85.  
  86. }
  87.  
  88.  
  89.  
  90.  
  91. response.setContentType("text/html;charset=UTF-8");
  92. try (PrintWriter out = response.getWriter()) {
  93. /* TODO output your page here. You may use following sample code. */
  94. out.println("<!DOCTYPE html>");
  95. out.println("<html>");
  96. out.println("<head>");
  97. out.println("<title>Servlet FileUploadDBServlet</title>");
  98. out.println("</head>");
  99. out.println("<body>");
  100. out.println("<h1>Servlet FileUploadDBServlet at " + request.getContextPath() + "</h1>");
  101. out.println("</body>");
  102. out.println("</html>");
  103. }
  104. }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement