Advertisement
Guest User

Untitled

a guest
May 1st, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. <html>
  2. <!--// This the client html page that receive data and uploads it.>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>JSP Page</title>
  6. </head>
  7. <body>
  8. <body>
  9. <center>
  10. <h1>Task It User profile page</h1><br></br>
  11.  
  12. <form action="Upload" method="post" enctype="multipart/form-data">
  13. <table border="0">
  14. <tr>
  15. <td>First Name: </td>
  16. <td><input type="text" name="firstName" size="50"/></td>
  17. </tr>
  18. <tr>
  19. <td>Last Name: </td>
  20. <td><input type="text" name="lastName" size="50"/></td>
  21. </tr>
  22. <tr>
  23. <td>Portrait Photo: </td>
  24. <td><input type="file" name="photo" size="50"/></td>
  25. </tr>
  26. <tr>
  27. <td colspan="2">
  28. <input type="submit" value="Save">
  29. </td>
  30. </tr>
  31. </table>
  32. </form>
  33. </center>
  34. </body>
  35. </html>
  36.  
  37.  
  38. package net.codejava.upload;
  39.  
  40. import java.io.IOException;
  41. import java.io.InputStream;
  42. import java.sql.Connection;
  43. import java.sql.DriverManager;
  44. import java.sql.PreparedStatement;
  45. import java.sql.SQLException;
  46.  
  47. import javax.servlet.ServletException;
  48. import javax.servlet.annotation.MultipartConfig;
  49. import javax.servlet.annotation.WebServlet;
  50. import javax.servlet.http.HttpServlet;
  51. import javax.servlet.http.HttpServletRequest;
  52. import javax.servlet.http.HttpServletResponse;
  53. import javax.servlet.http.Part;
  54.  
  55. @WebServlet("/Upload")
  56. @MultipartConfig(maxFileSize = 16177215) // upload file's size up to 16MB
  57. public class FileUpload extends HttpServlet {
  58.  
  59. // database connection settings
  60. private String dbURL = "jdbc:mysql://localhost:3306/appdb";
  61. private String dbUser = "root";
  62. private String dbPass = "asdf";
  63.  
  64. @Override
  65. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  66. throws ServletException, IOException {
  67. doPost(request, response);
  68. }
  69.  
  70. @Override
  71. protected void doPost(HttpServletRequest request,
  72. HttpServletResponse response) throws ServletException, IOException {
  73. // gets values of text fields
  74. String firstName = request.getParameter("firstName");
  75. String lastName = request.getParameter("lastName");
  76.  
  77. InputStream inputStream = null; // input stream of the upload file
  78.  
  79. // obtains the upload file part in this multipart request
  80. Part filePart = request.getPart("photo");
  81. if (filePart != null) {
  82. // prints out some information for debugging
  83. System.out.println(filePart.getName());
  84. System.out.println(filePart.getSize());
  85. System.out.println(filePart.getContentType());
  86.  
  87. // obtains input stream of the upload file
  88. inputStream = filePart.getInputStream();
  89. }
  90.  
  91. Connection conn = null; // connection to the database
  92. String message = null; // message will be sent back to client
  93.  
  94. try {
  95. // connects to the database
  96. DriverManager.registerDriver(new com.mysql.jdbc.Driver());
  97. conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
  98.  
  99. // constructs SQL statement
  100. String sql = "INSERT INTO contacts (first_name, last_name, photo) values (?, ?, ?)";
  101. PreparedStatement statement = conn.prepareStatement(sql);
  102. statement.setString(1, firstName);
  103. statement.setString(2, lastName);
  104.  
  105. if (inputStream != null) {
  106. // fetches input stream of the upload file for the blob column
  107. statement.setBlob(3, inputStream);
  108. }
  109.  
  110. // sends the statement to the database server
  111. int row = statement.executeUpdate();
  112. if (row > 0) {
  113. message = "File uploaded and saved into database";
  114. }
  115. } catch (SQLException ex) {
  116. message = "ERROR: " + ex.getMessage();
  117. // ex.printStackTrace();
  118. } finally {
  119. if (conn != null) {
  120. // closes the database connection
  121. try {
  122. conn.close();
  123. } catch (SQLException ex) {
  124. ex.printStackTrace();
  125. }
  126. }
  127. // sets the message in request scope
  128. request.setAttribute("Message", message);
  129.  
  130. // forwards to the message page
  131. getServletContext().getRequestDispatcher("/message.jsp").forward(request, response);
  132. }
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement