Hi All,
I get a java.lang.NullPointerException at the following code. I am trying to insert a blob object in a mysql database.
The java.lang.NullPointerException is in the FileInputStream fis = new FileInputStream(file);.
Can anyone tell me what is wrong.
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String file = request.getParameter("file");
FileInputStream fis = new FileInputStream(file);
try {
Class.forName( "org.gjt.mm.mysql.Driver" );
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/osso", "root", "root");
PreparedStatement pstmt = con.prepareStatement("INSERT INTO bestanden (Data) VALUES (?)");
// provide values for prepared statement and execute update
pstmt.setString (1, "theName");
// pump it in with a file input stream
pstmt.setBinaryStream (2, fis, file.length());
pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) { e.printStackTrace(); }
}
My JSP is as follows:
<html>
<head>
<title>
upload
</title>
</head>
<body bgcolor="#ffffff">
<form action="/upload" method="POST" name="upload" enctype="multipart/form-data" target="main">
<input type="file" name="file" value="">
<input type="hidden" name="actie" value="opslaan"/>
<input type="submit" name="uploaden" value="Uploaden"/>
</form>
<form action="/upload" method="post" target="main">
<input type="hidden" name="actie" value="ophalen"/>
<input type="submit" name="ophalen" value="Bestand ophalen"/>
</form>
</body>
</html>
Any suggestions what I am doing wrong. Or have somebody another idea to load files up to mysql database.
Regards,
Henk