0

我有这个代码可以在 MySQL 中保存图像,但它不起作用。

FileInputStream fis = null;
PreparedStatement ps = null;
MyDB.con.setAutoCommit(false);
File pic = new File(txtPicPath.getText().trim());
fis = new FileInputStream(pic);
ps = MyDB.con.prepareStatement("insert into `photo`(`Employee ID`, Picture) values (?, ?)");
ps.setString(1, Data.User.getText());
ps.setBinaryStream(2, fis, pic.length());
ps.executeUpdate();
MyDB.con.commit();
JOptionPane.showMessageDialog(rootPane, "Upload Successfully");
btnPicdelete.setEnabled(true);
btnBrowse.setEnabled(false);
btnUpload.setEnabled(false);
txtPicPass.setText("");    
txtPicPath.setText("");

问题是:

java.io.FileNotFoundException: image1.jpg (The system cannot find the file specified)

我上传的图片名称是image1.jpg.

4

2 回答 2

0

您需要在 jTextField 中指定图像的完整路径,或者您可以将getClass().getResource()其用作替代方案!

例子:

URL url = this.getClass().getResource("../Image/pyPic.jpg");  
                InputStream is = url.openStream();  
于 2013-03-20T07:49:00.620 回答
0

我的文件名必须是:

txtPicPath.setText(file.getAbsoluteFile().toString())
于 2013-03-21T02:34:49.993 回答