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.