Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
firstname=lastname="";
       String asn = request.getSession().getAttribute("user").toString();

       response.setContentType("text/html;charset=UTF-8");
       java.io.PrintWriter out = response.getWriter( );
       HashMap<String, String> formdata = new HashMap<String, String>();

        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
     response.setContentType("text/html");

     File file;
      String filePath = request.getServletContext().getRealPath("/StudentImages") + "/" ;
     if( !isMultipart ){

     }
     int maxSize=15*1024*1024;
       MultipartParser parser=  new MultipartParser(request, maxSize);
        com.oreilly.servlet.multipart.Part p=null;
         String sem="";
      String dep="";
      String gender="";
      String email="";
      String cast="";
      String address="";
      String mobile="";
      String bdate="";
      String filename="";
        while((p = parser.readNextPart()) != null)
   {
       if(p.isParam())
       {
           String pName=p.getName();
           if(pName.equals("fname"))
           {
               ParamPart pp=(ParamPart)p;
               dep=pp.getStringValue();
           }
           if(pName.equals("lname"))
           {
               ParamPart pp=(ParamPart)p;
               sem=pp.getStringValue();
           }
           if(pName.equals("gender"))
           {
               ParamPart pp=(ParamPart)p;
               gender=pp.getStringValue();
           }
           if(pName.equals("email"))
           {
               ParamPart pp=(ParamPart)p;
               email=pp.getStringValue();
           }
            if(pName.equals("cast"))
           {
               ParamPart pp=(ParamPart)p;
               cast=pp.getStringValue();
           }
             if(pName.equals("address"))
           {
               ParamPart pp=(ParamPart)p;
               address=pp.getStringValue();
           }
              if(pName.equals("mobile"))
           {
               ParamPart pp=(ParamPart)p;
               mobile=pp.getStringValue();
           }
                 if(pName.equals("bdate"))
           {
               ParamPart pp=(ParamPart)p;
               bdate=pp.getStringValue();
           }

             }
   }


     DiskFileItemFactory factory = new DiskFileItemFactory();
     // maximum size that will be stored in memory
     // Location to save data that is larger than maxMemSize.
     factory.setRepository(new File(filePath + "temp" ));

     // Create a new file upload handler
     ServletFileUpload upload = new ServletFileUpload(factory);

     try{
     // Parse the request to get file items.
     List fileItems = upload.parseRequest(request);

     // Process the uploaded file items
     Iterator i = fileItems.iterator();
     while ( i.hasNext () )
     {
        FileItem fi = (FileItem)i.next();
        if ( !fi.isFormField () && (fi.getContentType().equals("image/jpeg") || fi.getContentType().equals("image/png") ) )
        {
            String fieldName = fi.getFieldName();
           String fileName = fi.getName().trim();
           if(!fileName.equals("")) {
           String contentType = fi.getContentType();
           boolean isInMemory = fi.isInMemory();
           long sizeInBytes = fi.getSize();
           // Write the file
           fileName = asn + ".jpg";
           if( fileName.lastIndexOf("\\") >= 0 ){
              file = new File( filePath + fileName.substring( fileName.lastIndexOf("\\"))) ;
           }else{
              file = new File( filePath + fileName.substring(fileName.lastIndexOf("\\")+1)) ;
           }
           fi.write( file ) ;
           out.println("Uploaded Filename: " + fileName + "<br>");
           }
        }
        if (fi.isFormField()) {
            String name = fi.getFieldName();
            String value = fi.getString();
            formdata.put(name,value);
        }
     }
  }catch(Exception ex) {

      System.out.println(ex);
  }


       try {
           Class.forName("com.mysql.jdbc.Driver").newInstance();
           con = DriverManager.getConnection(connectionparams, uname, psword);
           PreparedStatement prep= con.prepareStatement("UPDATE reg1 SET "
                   + "firstname = ?, lastname = ?, gendar = ?, cast = ?, address = ?, bdate = ?, "
                   + "mobile = ?, email = ?  WHERE asn = ?");
           prep.setString(1,dep);
           prep.setString(2,sem);
           prep.setString(3,gender);
           prep.setString(4,cast);
           prep.setString(5,address);
           prep.setString(6,bdate);
           prep.setString(7,mobile);
           prep.setString(8,email);
           prep.setString(9,asn);
           prep.executeUpdate();
           prep.close();
           int random = new Random().nextInt();
           response.sendRedirect("Studenthome.jsp?rand="+random);
       } catch(Exception E) {
           System.out.println("-----------------------------");
           System.out.println(E);
       }

   }


The code is working perfect but image cant store in Give folder location with coustom name .where is the problem in my code.plz give me suggestion to solve this problem.
Posted
Comments
Shubhashish_Mandal 15-May-14 10:38am    
print the file path to get the actual file location.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900