简体   繁体   中英

Upload the image path to database using Struts2

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;

}

The local variable

File file;

is never initialized with data, hence the exception. Make FileBean a member variable in your Action class, and create a setter for it.

Eg

private File file;

public void setFile(String fileName) {
    this.file = new File(fileName);
}

After this, you can load your fileName from a HTML form. This will solve your NullPointerException .

PS: You said that you only want to store the path into the database. In that case, you should just save file.getPath() as a String, into the database:

ps.setString(1, file.getPath());

Change Your code like that

 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,in.available());
      // Returns true if there is a database
      if (ps.executeUpdate ()> 0) {
          return "view";
      }           
      return "err";                   
  }
}

and Image Bean file as

public class ImageBean {

File file;
public File getFile() {
    return file;
}

public void setFile(File file) {
    this.file = file;
 }
}

And Where you store Image is columns data type must be a blob type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM