SHARE
TWEET

Untitled

a guest Dec 6th, 2016 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. response.setContentType("text/html;charset=UTF-8");
  2.     PrintWriter out = response.getWriter();
  3.  
  4.     String path = getServletContext().getRealPath(" ");
  5.  
  6.         String savePath = path + File.separator + SAVE_DIR;
  7.             File fileSaveDir=new File(savePath);
  8.             if(!fileSaveDir.exists()){
  9.                 fileSaveDir.mkdir();
  10.             }
  11.         String firstName=request.getParameter("firstname");
  12.         String lastName=request.getParameter("lastname");
  13.         Part part=request.getPart("file");
  14.         String fileName=extractFileName(part);
  15.         /*if you may have more than one files with same name then you can calculate some random characters and append that characters in fileName so that it will  make your each image name identical.*/
  16.         part.write(savePath + File.separator + fileName);
  17.        /*
  18.         //You need this loop if you submitted more than one file
  19.         for (Part part : request.getParts()) {
  20.         String fileName = extractFileName(part);
  21.         part.write(savePath + File.separator + fileName);
  22.     }*/
  23.         String url = "jdbc:mysql://localhost:3306/";
  24.     String dbName = "uploadfile";
  25.     String driver = "com.mysql.jdbc.Driver";
  26.     String userName = "root";
  27.     String password = "kanchan";
  28.  
  29.     try {
  30.         Class.forName(driver).newInstance();
  31.  
  32.         Connection con = DriverManager
  33.                 .getConnection(url + dbName, userName, password);
  34.  
  35.  
  36.         String query="INSERT INTO custormerdetail values (null,?, ?, ?)";
  37.  
  38.             PreparedStatement pst;
  39.             pst=con.prepareStatement(query);
  40.             pst.setString(1, firstName);
  41.             pst.setString(2,lastName);
  42.             String filePath= savePath + File.separator + fileName ;
  43.             pst.setString(3,filePath);
  44.             pst.executeUpdate();
  45.  
  46.             out.print("uploading successful");
  47.  
  48. }catch (Exception e2) {e2.printStackTrace();}
  49. // file name of the upload file is included in content-disposition header like this:
  50. //form-data; name="dataFile"; filename="PHOTO.JPG"
  51.  }
  52. private String extractFileName(Part part) {
  53.     String contentDisp = part.getHeader("content-disposition");
  54.     String[] items = contentDisp.split(";");
  55.     for (String s : items) {
  56.         if (s.trim().startsWith("filename")) {
  57.             return s.substring(s.indexOf("=") + 2, s.length()-1);
  58.         }
  59.     }
  60.  
  61.  
  62.     out.close();
  63.     return null;
  64. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top