the simple thing i want to achieve is to be able to browse a file on my system an then use a servlet to finally store the file as a byte stream to my DB2 database. Initially i started by using coding the servlet part as a java application by explicitly mention the path of the file to be uploaded as the value of a String object. that worked fine. trouble started when i used an html form (which eventually has to be a jsp page) to search the file in the sysytem and then upload it.
here's my servlet "Up1.java" code (this works fine standalone)
//all the required imports go here
@WebServlet("/Up1")
public class Up1 extends HttpServlet {
private static final long serialVersionUID = 1L;
public Up1() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String filename=request.getParameter("filename");
PrintWriter pw=response.getWriter();
pw.print(filename);
try{
//InputStream sImage;
Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
Connection con= DriverManager.getConnection("jdbc:db2://localhost:50000/PHOTOTRY","db2admin","db2admin");
File imgfile = new File(filename);
String id="1111";
String name="myphoto";
FileInputStream fin = new FileInputStream(imgfile);
PreparedStatement pre = con.prepareStatement("INSERT INTO EMP VALUES(?,?,?)");
pre.setString(1,id);
pre.setBinaryStream(2,fin,(int)imgfile.length());
pre.setString(3,name);
int success=pre.executeUpdate();
if(success>=1){
pw.println("New Employee detail added");
}
pre.close();
}
catch(Exception e)
{pw.print(e);}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
and the supportin html file "page1.html"-
<html>
<head></head>
<body>
<form action="Up1" name="upform" enctype="multipart/form-data">
<table width="60%" border="0" cellspacing="1" cellpadding="1" align="center" class="style1">
<tr>
<td align="left"><b>Select a file to upload :</b></td>
</tr>
<tr>
<td align="left">
<input type="file" name="filename" size="50">
</td>
</tr>
<tr>
<td align="left">
<input type="hidden" name="todo" value="upload">
<input type="submit" name="Submit" value="Upload">
<input type="reset" name="Reset" value="Cancel">
</td>
</tr>
</table>
</form>
</body>
</html>
the problem faced is that it throws a java.io.FileNotFound Exception as it couldn't find the file specified. and i jsut can't figure out WHY?
IDE- Eclipse Indigo
Backend- DB2
OS- Windows 7