我正在尝试将图像上传到我的db2 database.i已经复制了所需的库
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
答案 0 :(得分:1)
request.getParameter()及其相关方法不适用于多部分请求,并且在处理多部分表单数据时将始终返回null。
请参阅以下有关Java中的文件上载: https://www.coderanch.com/how-to/java/FileUpload
这是一个更好的解决方案:http://www.roseindia.net/jsp/file_upload/Sinle_upload.xhtml.shtml