I am new to Struts2 and trying to learn more about. I just want to upload the path of an image into the database, not the whole image. I want to store it on my server, which can be retrieved later.
Well this was my idea. Now my question is, how to do that? I've tried, but right now, i am stuck and getting an error.
Error:
java.lang.NullPointerException
UploadImage.java
public class UploadImageAction extends ActionSupport{
public String execute() throws FileNotFoundException, ClassNotFoundException, SQLException {
ImageBean ib= new ImageBean();
FileInputStream in = new FileInputStream (ib.getFile());
Class.forName("com.mysql.jdbc.Driver");
String sql = "insert into filetable (image) value (?)";
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/erp","root","ali$");
PreparedStatement ps = conn.prepareStatement (sql);
// Specified in the settings for the specified input stream inputStream.available () used to determine the length of the stream
ps.setBinaryStream (1, in);
// Returns true if there is a database
if (ps.executeUpdate ()> 0) {
return "view";
}
return "err";
}
}
ImageBean.java
public class ImageBean {
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
File file;
}