Results 1 to 1 of 1
  1. #1
    mhcmd is offline Member
    Join Date
    Feb 2014
    Posts
    4
    Rep Power
    0

    Default Upload file sync with adding data in DB

    Hi,

    I'm trying to upload a file and store it in my project webcontent/images.

    I know that "enctype=multipart/form-data" is important to your form attribute when uploading file. Yes it works! event though I got some error/bugs

    "I'm using try catch here"
    error in sql File null uploaded successfully.
    File null uploaded successfully.
    File abc.jpg uploaded successfully.
    File null uploaded successfully.
    File null uploaded successfully.
    File null uploaded successfully.
    File null uploaded successfully.

    My problem is I cannot sync it with adding data, if I remove the "enctype=multipart/form-data" in my form. My sql works! but the uploading of file failed :(

    Added
    Exception in uploading file-1.

    I'm using java servlet. Here's my codes if you wanted to look on it.

    AddIngr.java

    Java Code:
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		PrintWriter out = response.getWriter();
    		out.println("<html><hr> ");
    	    out.println("<h1> Add Ingredients </h1> "); //enctype=multipart/form-data
    		out.println("<form method=post action=GetAddIngr >");
    	    //out.println("<form method=post action=MySQLConTest>");
    		out.println("<br>Name: <input type=text input name=name ><br>");
    		out.println("<br>Description: <input type=text input name=desc><br>");
    		out.println("<br>Picture: <input type=file  name=pic ><br>");
    		out.println("<br>Calories Content: <input type=text input name=cal><br>");
    		out.println("<br>Advantage: <input type=text input name=adv><br>");
    		out.println("<br>Disadvantage: <input type=text input name=disadv><br>");
    
    		out.println("<br><br><input type=submit name=Submit value=Add ><br><br>");
    		out.println("</form>");
    		out.println("<hr> ");		
    		out.println("<br><br></html>");
    		//out.println("</body></html>");
    	
    	}
    GetAddIngr.java

    Java Code:
     public void init() throws ServletException{
    		DiskFileItemFactory fileFactory = new DiskFileItemFactory();
    		File filesDir = (File) getServletContext().getAttribute("FILES_DIR_FILE");
    		fileFactory.setRepository(filesDir);
    		this.uploader = new ServletFileUpload(fileFactory);
    	}
    
    	/**
    	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    	 */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        	
        	response.setContentType("text/html");
    	    PrintWriter out = response.getWriter();
    	
       try {
    		List<FileItem> fileItemsList = uploader.parseRequest(request);
    		Iterator<FileItem> fileItemsIterator = fileItemsList.iterator();
    		while(fileItemsIterator.hasNext()){
    			FileItem fileItem = fileItemsIterator.next();
    			File file = new File(request.getServletContext().getAttribute("FILES_DIR")+File.separator+fileItem.getName());
    			
    			fileItem.write(file);
    			out.println("File "+fileItem.getName()+ " uploaded successfully.");
    			out.println("<br>");
    			
    		}
    	} catch (FileUploadException e) {
    		out.println("Exception in uploading file-1.");
    	} catch (Exception e) {
    		out.println("Exception in uploading file.");
    	}
        	
    			  
    	}
    
    	
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		
    	    String url = "jdbc:sqlite:C:/Users/Honey/Documents/cookitDB.sqlite";
    		
    		  response.setContentType("text/html");
    		  PrintWriter out = response.getWriter();
    		 
    		  String name = request.getParameter("name");
    		  String desc = request.getParameter("desc");
    		  String cal = request.getParameter("cal");
    		  String adv = request.getParameter("adv");
    		  String disadv = request.getParameter("disadv");
    		  String fileName = request.getParameter("pic"); 
    		 
    		  try {
    		  Class.forName("org.sqlite.JDBC");
    		  con = DriverManager.getConnection(url);
    		
    		  sql = "insert into ingredients values (?,?,?,?,?,?,?)";
    		 
    		  pt = con.prepareStatement(sql);
    		  pt.setString(2, name);
    		  pt.setString(3, desc);
    		  pt.setString(4, "http://www.java-forums.org/images/"+fileName);
    		  pt.setString(5, cal);
    		  pt.setString(6, adv);
    		  pt.setString(7, disadv);
    		  pt.executeUpdate();
    		  out.println("  Added ");
    		  pt.close();
    
    		 
    		  }
    		  catch(ClassNotFoundException e){
    		  out.println("Couldn't load database driver");
    		  }
    		  catch(SQLException e){
    		  out.println("error in sql");
    		  }
    		  catch (Exception e){
    		  out.println(e);
    		  }
    		  
    		  finally {
    		   try {
    		  if (con != null) con.close();
    		  }
    		  catch (SQLException ignored){
    		  out.println(ignored);
    		  }
    		  }
    		  
    		 
    
    	   doGet(request, response);
    		
    		
    	}
    Thanks :)
    Last edited by mhcmd; 02-12-2014 at 05:53 AM.

Similar Threads

  1. Adding data to an existing binary file
    By NatProg in forum Advanced Java
    Replies: 5
    Last Post: 03-15-2012, 01:51 AM
  2. file sync from FTP to FTP
    By kgk04 in forum Advanced Java
    Replies: 0
    Last Post: 12-18-2010, 11:15 AM
  3. Replies: 0
    Last Post: 11-24-2009, 02:49 PM
  4. :large file upload to server(chunk upload)
    By tommy_725 in forum Networking
    Replies: 0
    Last Post: 10-16-2009, 01:21 AM
  5. how to upload a file along with html form data
    By pranith in forum Java Servlet
    Replies: 3
    Last Post: 07-30-2007, 03:33 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •