我正在尝试将图像上传到我的 db2 数据库中。我已经复制了所需的
db2jcc.jar
库web-inf/lib
<form action="Upload" method="post" enctype="multipart/form-data">
ID : <input type="text" name="id"/><br>
FILE : <input type="file" name="photo"/><br>
<input type="submit" value="upload"/>
</form>
我的上传器 servlet 是
try {
String id=request.getParameter("id");
Part photo = request.getPart("photo");
try
{
Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
Connection con=DriverManager.getConnection("jdbc:db2:SAMPLEDB");
System.out.println("Connection Successful");
PreparedStatement ps=con.prepareStatement("INSERT INTO SAMPLETABLE (ID,PHOTO) VALUES (?,?)");
ps.setString(1, id);
File fBlob = new File ( request.getParameter("photo") ); //exception thrown here
FileInputStream is = new FileInputStream ( fBlob );
ps.setBinaryStream (2, is, (int) fBlob.length() );
ps.execute ();
}
catch(Exception e)
{
System.out.println("exception --> "+e);
}
}
finally
{
out.close();
}
}
我得到的例外是
java.lang.NullPointerException