Guest User

Untitled

a guest
Dec 19th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. <form class="form-horizontal" enctype="multipart/form-data" method="post" action="uploadFile.jsp">
  2. <fieldset>
  3. <div class="control-group">
  4. <label class="control-label" for="typeahead">Product Name:</label>
  5. <div class="controls">
  6. <input type="text" class="span6 typeahead" id="typeahead" name="ck_product_name" data-provide="typeahead" data-items="4" reqired=""/>
  7. </div>
  8. </div>
  9.  
  10. <div class="control-group">
  11. <label class="control-label" for="typeahead">Gallery Image</label>
  12. <div class="controls">
  13. <input type="file" class="form-control" name="uProperty" id="txtimage" required="required">
  14. <span>Image Size (698 X 352)</span>
  15. </div>
  16. </div>
  17.  
  18. <div class="form-actions">
  19. <button type="submit" class="btn btn-primary">Submit</button>
  20. <button type="reset" class="btn">Reset</button>
  21. </div>
  22. </fieldset>
  23. </form>
  24.  
  25. <%
  26. int no1 = 1;
  27.  
  28.  
  29. String saveFile = "";
  30. String contentType = request.getContentType();
  31. if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
  32. DataInputStream in = new DataInputStream(request.getInputStream());
  33. int formDataLength = request.getContentLength();
  34. byte dataBytes[] = new byte[formDataLength];
  35. int byteRead = 0;
  36. int totalBytesRead = 0;
  37. while (totalBytesRead < formDataLength) {
  38. byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
  39. totalBytesRead += byteRead;
  40. }
  41. String file = new String(dataBytes);
  42. saveFile = file.substring(file.indexOf("filename="") + 10);
  43. saveFile = saveFile.substring(0, saveFile.indexOf("n"));
  44. saveFile = saveFile.substring(saveFile.lastIndexOf("\") + 1, saveFile.indexOf("""));
  45. int lastIndex = contentType.lastIndexOf("=");
  46. String boundary = contentType.substring(lastIndex + 1, contentType.length());
  47. int pos;
  48. pos = file.indexOf("filename="");
  49. pos = file.indexOf("n", pos) + 1;
  50. pos = file.indexOf("n", pos) + 1;
  51. pos = file.indexOf("n", pos) + 1;
  52. int boundaryLocation = file.indexOf(boundary, pos) - 4;
  53. int startPos = ((file.substring(0, pos)).getBytes()).length;
  54. int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
  55. File ff = new File("D:/Admin_Panel/web/img/gallery/" + saveFile);
  56. FileOutputStream fileOut = new FileOutputStream(ff);
  57. fileOut.write(dataBytes, startPos, (endPos - startPos));
  58. fileOut.flush();
  59. fileOut.close();
  60. %><br>
  61. <table border="2">
  62. <tr>
  63. <td><b>You have successfully upload the file:</b>
  64. <%out.println(saveFile);%></td>
  65. </tr>
  66. </table>
  67. <%
  68. Connection connection = null;
  69. String connectionURL = "jdbc:mysql://localhost:3306/vehiclehire?zeroDateTimeBehavior=convertToNull";
  70. PreparedStatement psmnt = null;
  71. String sv = request.getParameter("ck_product_name");
  72. try {
  73. Class.forName("com.mysql.jdbc.Driver").newInstance();
  74. connection = DriverManager.getConnection(connectionURL, "root", "");
  75. psmnt = connection.prepareStatement("insert into gallery(file_path,ck_product_name) values(?,?)");
  76. psmnt.setString(1, ff.getPath());
  77. psmnt.setString(2, sv);
  78. out.println(psmnt);
  79. int s = psmnt.executeUpdate();
  80. if (s > 0) {
  81. System.out.println("Uploaded successfully !");
  82. } else {
  83. System.out.println("Error!");
  84. }
  85. } catch (Exception e) {
  86. e.printStackTrace();
  87. }
  88. }
  89. %>
Add Comment
Please, Sign In to add comment