使用 Struts2 将图像路径上传到数据库
Upload the image path to database using Struts2
我是 Struts2 的新手,想了解更多。我只想将图像的路径上传到数据库中,而不是整个图像。我想把它存储在我的服务器上,以后可以检索它。
嗯,这是我的主意。现在我的问题是,该怎么做?我试过了,但现在,我卡住了并收到错误消息。
错误:
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;
}
局部变量
File file;
从未用数据初始化,因此异常。在你的 Action class 中使 FileBean
成为一个成员变量,并为其创建一个 setter。
例如
private File file;
public void setFile(String fileName) {
this.file = new File(fileName);
}
在此之后,您可以从 HTML 表单加载 fileName
。这将解决您的 NullPointerException
.
PS: 你说你只想把路径存入数据库。在这种情况下,您应该将 file.getPath()
作为字符串保存到数据库中:
ps.setString(1, file.getPath());
像那样更改您的代码
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";
}
}
和 Image Bean 文件为
public class ImageBean {
File file;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
}
并且您存储图像的位置是列数据类型必须是 blob 类型。
我是 Struts2 的新手,想了解更多。我只想将图像的路径上传到数据库中,而不是整个图像。我想把它存储在我的服务器上,以后可以检索它。
嗯,这是我的主意。现在我的问题是,该怎么做?我试过了,但现在,我卡住了并收到错误消息。
错误:
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;
}
局部变量
File file;
从未用数据初始化,因此异常。在你的 Action class 中使 FileBean
成为一个成员变量,并为其创建一个 setter。
例如
private File file;
public void setFile(String fileName) {
this.file = new File(fileName);
}
在此之后,您可以从 HTML 表单加载 fileName
。这将解决您的 NullPointerException
.
PS: 你说你只想把路径存入数据库。在这种情况下,您应该将 file.getPath()
作为字符串保存到数据库中:
ps.setString(1, file.getPath());
像那样更改您的代码
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";
}
}
和 Image Bean 文件为
public class ImageBean {
File file;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
}
并且您存储图像的位置是列数据类型必须是 blob 类型。