Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: why i can't upload file to database

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default why i can't upload file to database

    i'm a newbie in java web application. I used to develop simple module to upload file to database.Below is my insertblob.jsp

    <title>Page Insert</title>
    </head>
    <body>
    <form action="datapost_blob.jsp" method="post" enctype="multipart/form-data">
      <table width="499" border="2" align="center">
     
        <tr bgcolor="#99CCCC">
          <th width="174" scope="row"><div align="left">TRANSACTION DATE</div></th>
     
        <td width="307"><input type="text" name="transaction" readonly="true" value="<%=dateFormat.format(date)%>"></td>
        </tr>
        <tr bgcolor="#99CCCC">
          <th scope="row"><div align="left">VALID START DATE </div></th>
          <td><select name="start" size="1" >
            <option value="selected">Select Operator</option>
            <option value="equal">Equal</option>
            <option value="before">Before</option>
            <option value="after">After</option>
            <option value="meet">Meet</option>
            <option value="met_by">Met_by</option>
          </select>
              <input size="10" id="textfield" type="text" name="textfield" title="DD/MM/YYYY" >
    <input type="button" value="Calendar" onclick="displayCalendarFor('textfield');">
             </td>
        </tr>
        <tr bgcolor="#99CCCC">
          <th scope="row"><div align="left">VALID END DATE </div></th>
          <td><select name="end" size="1" id="end">
            <option value="selected">Select Operator</option>
            <option value="equal">Equal</option>
            <option value="before">Before</option>
            <option value="after">After</option>
            <option value="meet">Meet</option>
            <option value="met_by">Met_by</option>
          </select>
              <input size="10" id="textfieldd" type="text" name="textfieldd" title="DD/MM/YYYY" >
    <input type="button" value="Calendar" onclick="displayCalendarFor('textfieldd');">
              </td>
        <tr bgcolor="#99CCCC">
            <th scope="row"><div align="left">UPLOAD FILE </div></th>
            <td><input type="file" name="file"></td></tr>
     
      </table>
    <p align="center">
        <input type="submit" name="Submit" value="Submit">
        <input type="reset" name="Reset" value="Reset">
    </p>
     
     
     
    </form>
    </body>
    </html>

    and this is my datapost_blob.jsp
    <%@ page import="java.sql.*"%>
    <%@ page import="java.io.File"%>
    <%@ page import="java.io.*"%>
    <%@ page import="java.io.FileInputStream"%>
    <%@ page import="org.apache.commons.fileupload.*"%>
     <%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
     <%@ page import="org.apache.commons.io.*"%>
     <%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
    <%@ page import="org.apache.commons.fileupload.FileUploadException"%>
      <%@ page import="java.util.List"%>
     <%@ page import="java.util.Iterator"%>
     
     <%!
       String temp_transaction="";
       String temp_start="";
       String temp_textfield="";
       String temp_end="";
       String temp_textfieldd="";
       %>
     
    <%
    //check that we have a file upload request
     boolean isMultipart = ServletFileUpload.isMultipartContent(request);
     
     //create a factory for disk-based file items
       FileItemFactory filefactory = new DiskFileItemFactory();
     //create a new file upload handler
       ServletFileUpload serFileUpload = new ServletFileUpload(filefactory);
     
    //try{
        //parse the request
       List fileItems = serFileUpload.parseRequest(request);
       //process the upload items
       Iterator iter = fileItems.iterator();
       while (iter.hasNext())
    	   {
       FileItem item = (FileItem) iter.next();
       if (item.isFormField())
           {
    String name=item.getFieldName();
    String value=item.getString();
     
           if(name.equals("transaction"))
               {temp_transaction=value;
                }
           if(name.equals("start"))
               {temp_start=value;
                }
           if(name.equals("textfield"))
               {temp_textfield=value;
                }
           if(name.equals("end"))
               {temp_end=value;
                }
           if(name.equals("textfieldd"))
               {temp_textfieldd=value;
                }
           }else{
     String itemName = item.getName();
    String folder="C:/Users/dr saiful/My Documents/NetBeansProjects/master_temporal/web/data/"+itemName;
     File saveFile=new File(folder);
     FileInputStream fis=new FileInputStream(saveFile);
     
     
      // item.write(savefile);
     
    /*}catch(Exception e1){
        e1.printStackTrace();
        }
        }
       }*/
     //}catch (FileUploadException e){
        //e.printStackTrace();
        //}
    Connection connection = null;
    String connectionURL = "jdbc:mysql://localhost:3306/test";
    PreparedStatement pstatement = null;
     
    //try{
     
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    connection = DriverManager.getConnection(connectionURL, "root","farham");   
    String queryString = "INSERT INTO master_blob(transactionDate,operatorStart,validStart,operatorEnd,validEnd,filename,datafile) VALUES (?,?,?,?,?,?,?)";
     
                  pstatement = connection.prepareStatement(queryString);
                  pstatement.setString(1, temp_transaction);
                  pstatement.setString(2, temp_start);
                  pstatement.setString(3, temp_textfield);
                  pstatement.setString(4, temp_end);
                  pstatement.setString(5, temp_textfieldd);
                  pstatement.setString(6, itemName);
                  pstatement.setBinaryStream(7,fis,(int)saveFile.length());
     
                  int s=pstatement.executeUpdate();
     
    if(s>0) {
    out.println("Uploaded successfully !");
    }
    else{
    out.println("unsucessfull to upload file.");
    }
     
    fis.close();
    connection.close();
     
           }
     
       }
     
     
    //}
    //catch(Exception e){e.printStackTrace();}
     
    %>

    when I try doing upload,error is printed like below
    HTTP Status 500 -
     
    type Exception report
     
    message
     
    description The server encountered an internal error () that prevented it from fulfilling this request.
     
    exception
     
    org.apache.jasper.JasperException: An exception occurred processing JSP page /datapost_blob.jsp at line 62
     
    59:  String itemName = item.getName();
    60: String folder="C:/Users/dr saiful/My Documents/NetBeansProjects/master_temporal/web/data/"+itemName;
    61:  File saveFile=new File(folder);
    62:  FileInputStream fis=new FileInputStream(saveFile);
    63:   
    64:       
    65:   // item.write(savefile);
     
     
    Stacktrace:
    	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
    	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:404)
    	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    	org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
     
    root cause
     
    java.io.FileNotFoundException: C:\Users\dr saiful\My Documents\NetBeansProjects\master_temporal\web\data\bismi4.gif (The system cannot find the file specified)
    	java.io.FileInputStream.open(Native Method)
    	java.io.FileInputStream.<init>(FileInputStream.java:106)
    	org.apache.jsp.datapost_005fblob_jsp._jspService(datapost_005fblob_jsp.java:127)
    	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    	org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
     
    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.20 logs.


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: why i can't upload file to database

    Hi,
    Please check that file path exist or not and second thing you can change this path to other directory as sometime there may be file permission issue .

    bean factory
    Last edited by abani; December 18th, 2011 at 01:07 AM.

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    42
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: why i can't upload file to database

    Look at this section:

     String itemName = item.getName();
    String folder="C:/Users/dr saiful/My Documents/NetBeansProjects/master_temporal/web/data/"+itemName;
     File saveFile=new File(folder);
     FileInputStream fis=new FileInputStream(saveFile);

    Check that the folder exists on the server.
    Be aware that item.getName() returns differently by IE and Firefox. IE returns absolute path whereas FireFox returns only file name.

    java exception
    Last edited by hns1984; January 11th, 2012 at 05:57 PM.

  4. #4
    Member
    Join Date
    Nov 2011
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: why i can't upload file to database

    Hi,
    Is your problem solved ?

Similar Threads

  1. file upload size limit
    By shiv@P in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: December 24th, 2012, 10:50 PM
  2. Upload File - JSP + Servlet + Netbeans
    By bitos2002 in forum Web Frameworks
    Replies: 4
    Last Post: November 16th, 2011, 08:29 AM
  3. How to upload a pdf file to mysql from a directory?
    By A4Andy in forum JDBC & Databases
    Replies: 1
    Last Post: October 19th, 2011, 08:49 AM
  4. File upload problem
    By antonic in forum Web Frameworks
    Replies: 2
    Last Post: September 28th, 2011, 07:25 PM
  5. Want to upload csv file of greter than 200 Mb
    By shreyansh in forum Java Servlet
    Replies: 6
    Last Post: February 8th, 2011, 06:43 PM