Guest User

Untitled

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