如果需要,你真的需要做一个完整的阅读,这样你才能真正理解我的问题。我会非常感激和感激。 我明白了:
java.io.FileNotFoundException: photo.png (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at com.nationalid.NationalIDModel.getPicture(NationalIDModel.java:150)
at com.nationalid.NationalViewController.goToPrint(NationalViewController.java:560)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
当我运行我的应用时。
这一行:
at com.nationalid.NationalIDModel.getPicture(NationalIDModel.java:150)
对应于此代码的第6行:
public FileInputStream getPicture(PreparedStatement preparedStatement)
{
File file = new File("photo.png");
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(file);
preparedStatement.setBinaryStream(7, (InputStream) fileInputStream, (int) file.length());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return (FileInputStream) Picture.getBean();
}
这一个:
at com.nationalid.NationalViewController.goToPrint(NationalViewController.java:560)
对应于我在索引7处设置binaryStream的行(图像文件):
public void goToPrint(ActionEvent event) {
myConnection = SqliteConnection.connector();
try {
NationalIDModel national = (NationalIDModel)nationalTable.getSelectionModel().getSelectedItem();
String query = "SELECT * FROM nationalid_table " + "WHERE unique_number = ? AND passportID = ? AND firstname = ?" + "AND lastname = ? AND issuedate = ? AND expirydate = ? AND " + "picture = ? AND nationality = ? AND dateofbirth = ? AND " + "sex = ? AND address = ? AND height = ? AND " + "id_number = ? AND number_of_id = ? AND age = ?";
preparedStatement = myConnection.prepareStatement(query);
preparedStatement.setString(1, national.getUniqueNumber());
preparedStatement.setString(2, national.getPassportID());
preparedStatement.setString(3, national.getFirstname());
preparedStatement.setString(4, national.getLastname());
preparedStatement.setString(5, national.getIssueDate());
preparedStatement.setString(6, national.getExpiryDate());
preparedStatement.setBinaryStream(7, national.getPicture(preparedStatement));
preparedStatement.setString(8, national.getNationality());
preparedStatement.setString(9, national.getDateOfBirth());
preparedStatement.setString(10, national.getGender());
preparedStatement.setString(11, national.getAddress());
preparedStatement.setString(12, national.getHeight());
preparedStatement.setString(13, national.getBirthID());
preparedStatement.setString(14, national.getIDNumber());
preparedStatement.setString(15, national.getAge());
myResultSet = preparedStatement.executeQuery();
if (myResultSet.next())
{
//((Node)event.getSource()).getScene().getWindow().hide();
Stage window = new Stage();
FXMLLoader loader = new FXMLLoader();
Pane root = loader.load(getClass().getResource("../nationalid/PersonsDetailsView.fxml").openStream());
PersonsDetailsController personsDetailsController = (PersonsDetailsController) loader.getController();
personsDetailsController.getUsers(
(FileInputStream) national.getPicture(preparedStatement),
national.getFirstname(),
national.getLastname(),
national.getGender(),
national.getHeight(),
national.getDateOfBirth(),
national.getNationality(),
national.getAddress(),
national.getIDNumber(),
national.getIssueDate(),
national.getExpiryDate()
);
Scene scene = new Scene(root);
window.setTitle("Hello " + txtfirstname.getText() + ", this is your menu page");
window.getIcons().add(new Image("file:users.png"));
window.setScene(scene);
window.show();
NotificationType type = NotificationType.SUCCESS;
TrayNotification trayNotification = new TrayNotification();
trayNotification.setTitle("User Details");
trayNotification.setMessage("User Details Displayed!");
trayNotification.setNotificationType(type);
trayNotification.showAndDismiss(Duration.seconds(4));
}
preparedStatement.close();
} catch (SQLException ep) {
//Logger.getLogger(AddUserController.class.getName()).log(SEVERE, null, ep);
System.out.println(ep);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
但首先这是我问题的背景:
我正在使用智能创意中的javafx构建桌面应用程序。我正在使用MVC模式。我已经构建了一个模块:显示所有可能的数据操作,包括统计估计和数据库值的表示。
现在我正在构建第二个模块:National Identification将从Birth表中提取数据,进行可能的操作并将其存储在ID表中。该项目的目的实际上是获得每个人的真实跟踪......
现在在出生模块中,我有一个查看和打印按钮的视图和打印按钮,所以我必须从数据库中提取数据。每个窗口都是一个fxml文件,其中包含与之关联的特定控制器。
在目前的模块(识别)中,我想做同样的事情,但这次我也必须从数据库中提取图像。
所以我能够提取所有信息并在imageview中正确显示个人图像。
现在在我的ID模块包中,我有一个数据库操作模型:
package com.nationalid;
public class NationalIDModel {
private final SimpleStringProperty UniqueNumber;
private final SimpleStringProperty PassportID;
private final SimpleStringProperty Firstname;
private final SimpleStringProperty Lastname;
private final SimpleStringProperty IssueDate;
private final SimpleStringProperty ExpiryDate;
private final SimpleStringProperty Picture;
private final SimpleStringProperty Nationality;
private final SimpleStringProperty DateOfBirth;
private final SimpleStringProperty Gender;
private final SimpleStringProperty Address;
private final SimpleStringProperty Height;
private final SimpleStringProperty BirthID;
private final SimpleStringProperty IDNumber;
private final SimpleStringProperty NumberOfID;
private final SimpleStringProperty Age;
private final SimpleStringProperty Status;
public NationalIDModel(String unique_number,
String passportID,
String firstname,
String lastname,
String issuedate,
String expirydate,
String picture,
String nationality,
String dateofbirth,
String gender,
String address,
String height,
String birthid,
String id_number,
String number_of_id,
String age,
String status
)
{
this.UniqueNumber = new SimpleStringProperty(unique_number);
this.PassportID = new SimpleStringProperty(passportID);
this.Firstname = new SimpleStringProperty(firstname);
this.Lastname = new SimpleStringProperty(lastname);
this.IssueDate = new SimpleStringProperty(issuedate);
this.ExpiryDate = new SimpleStringProperty(expirydate);
this.Picture = new SimpleStringProperty(picture);
this.Nationality = new SimpleStringProperty(nationality);
this.DateOfBirth = new SimpleStringProperty(dateofbirth);
this.Gender = new SimpleStringProperty(gender);
this.Address = new SimpleStringProperty(address);
this.Height = new SimpleStringProperty(height);
this.BirthID = new SimpleStringProperty(birthid);
this.IDNumber = new SimpleStringProperty(id_number);
this.NumberOfID = new SimpleStringProperty(number_of_id);
this.Age = new SimpleStringProperty(age);
this.Status = new SimpleStringProperty(status);
}
public String getUniqueNumber() {
return UniqueNumber.get();
}
public SimpleStringProperty uniqueNumberProperty() {
return UniqueNumber;
}
public void setUniqueNumber(String uniqueNumber) {
this.UniqueNumber.set(uniqueNumber);
}
public String getPassportID() {
return PassportID.get();
}
public SimpleStringProperty passportIDProperty() {
return PassportID;
}
public void setPassportID(String passportID) {
this.PassportID.set(passportID);
}
public String getFirstname() {
return Firstname.get();
}
public SimpleStringProperty firstnameProperty() {
return Firstname;
}
public void setFirstname(String firstname) {
this.Firstname.set(firstname);
}
public String getLastname() {
return Lastname.get();
}
public SimpleStringProperty lastnameProperty() {
return Lastname;
}
public void setLastname(String lastname) {
this.Lastname.set(lastname);
}
public String getIssueDate() {
return IssueDate.get();
}
public SimpleStringProperty issueDateProperty() {
return IssueDate;
}
public void setIssueDate(String issueDate) {
this.IssueDate.set(issueDate);
}
public String getExpiryDate() {
return ExpiryDate.get();
}
public SimpleStringProperty expiryDateProperty() {
return ExpiryDate;
}
public void setExpiryDate(String expiryDate) {
this.ExpiryDate.set(expiryDate);
}
public String getPicture()
{
return Picture.get();
}
public SimpleStringProperty pictureProperty() {
return Picture;
}
public void setPicture(String picture) {
this.Picture.set(picture);
}
public String getNationality() {
return Nationality.get();
}
public SimpleStringProperty nationalityProperty() {
return Nationality;
}
public void setNationality(String nationality) {
this.Nationality.set(nationality);
}
public String getDateOfBirth() {
return DateOfBirth.get();
}
public SimpleStringProperty dateOfBirthProperty() {
return DateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.DateOfBirth.set(dateOfBirth);
}
public String getGender() {
return Gender.get();
}
public SimpleStringProperty genderProperty() {
return Gender;
}
public void setGender(String gender) {
this.Gender.set(gender);
}
public String getAddress() {
return Address.get();
}
public SimpleStringProperty addressProperty() {
return Address;
}
public void setAddress(String address) {this.Address.set(address);}
public String getHeight() {
return Height.get();
}
public SimpleStringProperty heightProperty() {
return Height;
}
public void setHeight(String height) {
this.Height.set(height);
}
public String getBirthID() {
return BirthID.get();
}
public SimpleStringProperty birthIDProperty() {
return BirthID;
}
public void setBirthID(String birthID) {
this.BirthID.set(birthID);
}
public String getIDNumber() {
return IDNumber.get();
}
public SimpleStringProperty IDNumberProperty() {
return IDNumber;
}
public void setIDNumber(String IDNumber) {
this.IDNumber.set(IDNumber);
}
public String getNumberOfID() {
return NumberOfID.get();
}
public SimpleStringProperty numberOfIDProperty() {
return NumberOfID;
}
public void setNumberOfID(String numberOfID) {
this.NumberOfID.set(numberOfID);
}
public String getAge() {
return Age.get();
}
public SimpleStringProperty ageProperty() {
return Age;
}
public void setAge(String age) {
this.Age.set(age);
}
public String getStatus() {
return Status.get();
}
public SimpleStringProperty statusProperty() {
return Status;
}
public void setStatus(String status) {
this.Status.set(status);
}
}
我故意尝试从此修改getPicture()方法:
public String getPicture() {
return Picture.get();
}
到此:
public FileInputStream getPicture(PreparedStatement preparedStatement)
{
File file = new File("photo.png");
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(file);
preparedStatement.setBinaryStream(7, (InputStream) fileInputStream, (int) file.length());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return (FileInputStream) Picture.getBean();
}
以便它可以匹配:
preparedStatement.setBinaryStream(7, national.getPicture(preparedStatement));
方法:goToPrint(上面提到)类NationalViewController:
package com.nationalid;
public class NationalViewController implements Initializable {
@Override
public void initialize(URL location, ResourceBundle resources) {
Image image;
image = new Image(getClass().getResourceAsStream("download(1).png"));
imageViewGoDown.setImage(image);
//Image imageSave = new Image(getClass().getResourceAsStream("plus.png"));
//btnAdd.getGraphic().equals(imageSave);
brithidcolCS.setCellValueFactory(new PropertyValueFactory<>("BirthID"));
firstnamecolCS.setCellValueFactory(new PropertyValueFactory<>("Firstname"));
lasnamecolCS.setCellValueFactory(new PropertyValueFactory<>("Lastname"));
fathersnamecolCS.setCellValueFactory(new PropertyValueFactory<>("FathersName"));
mothersnamecolCS.setCellValueFactory(new PropertyValueFactory<>("MothersName"));
weightcolCS.setCellValueFactory(new PropertyValueFactory<>("Weight"));
heigthcolCS.setCellValueFactory(new PropertyValueFactory<>("Height"));
placeofbirthcolCS.setCellValueFactory(new PropertyValueFactory<>("PlaceOfBirth"));
nationalitycolCS.setCellValueFactory(new PropertyValueFactory<>("Nationality"));
gendercolCS.setCellValueFactory(new PropertyValueFactory<>("Gender"));
birthIDCertcolCS.setCellValueFactory(new PropertyValueFactory<>("BirthCertificateID"));
dateissuedcolCS.setCellValueFactory(new PropertyValueFactory<>("DateIssued"));
dateofbirthCS.setCellValueFactory(new PropertyValueFactory<>("DateOfBirth"));
agecolCS.setCellValueFactory(new PropertyValueFactory<>("Age"));
uniqueIDcolNI.setCellValueFactory(new PropertyValueFactory<>("UniqueNumber"));
firstnamecolNI.setCellValueFactory(new PropertyValueFactory<>("Firstname"));
lastnamecolNI.setCellValueFactory(new PropertyValueFactory<>("Lastname"));
dateofbirthcolNI.setCellValueFactory(new PropertyValueFactory<>("DateOfBirth"));
nationalitycolNI.setCellValueFactory(new PropertyValueFactory<>("Nationality"));
gendercolNI.setCellValueFactory(new PropertyValueFactory<>("Gender"));
heightcolNI.setCellValueFactory(new PropertyValueFactory<>("Height"));
addresscolNI.setCellValueFactory(new PropertyValueFactory<>("Address"));
cardIDcolNI.setCellValueFactory(new PropertyValueFactory<>("IDNumber"));
issuedatecolNI.setCellValueFactory(new PropertyValueFactory<>("IssueDate"));
expirydatecolNI.setCellValueFactory(new PropertyValueFactory<>("ExpiryDate"));
numberofIDcolNI.setCellValueFactory(new PropertyValueFactory<>("NumberOfID"));
statuscolNI.setCellValueFactory(new PropertyValueFactory<>("Status"));
birthIDcolNI.setCellValueFactory(new PropertyValueFactory<>("BirthID"));
passwordIDcolNI.setCellValueFactory(new PropertyValueFactory<>("PassportID"));
picturecolNI.setCellValueFactory(new PropertyValueFactory<>("Picture"));
agecolNI.setCellValueFactory(new PropertyValueFactory<>("Age"));
}
public void goToPrint(ActionEvent event) {
myConnection = SqliteConnection.connector();
try {
NationalIDModel national = (NationalIDModel)nationalTable.getSelectionModel().getSelectedItem();
String query = "SELECT * FROM nationalid_table " + "WHERE unique_number = ? AND passportID = ? AND firstname = ?" + "AND lastname = ? AND issuedate = ? AND expirydate = ? AND " + "picture = ? AND nationality = ? AND dateofbirth = ? AND " + "sex = ? AND address = ? AND height = ? AND " + "id_number = ? AND number_of_id = ? AND age = ?";
preparedStatement = myConnection.prepareStatement(query);
preparedStatement.setString(1, national.getUniqueNumber());
preparedStatement.setString(2, national.getPassportID());
preparedStatement.setString(3, national.getFirstname());
preparedStatement.setString(4, national.getLastname());
preparedStatement.setString(5, national.getIssueDate());
preparedStatement.setString(6, national.getExpiryDate());
preparedStatement.setBinaryStream(7, national.getPicture(preparedStatement));
preparedStatement.setString(8, national.getNationality());
preparedStatement.setString(9, national.getDateOfBirth());
preparedStatement.setString(10, national.getGender());
preparedStatement.setString(11, national.getAddress());
preparedStatement.setString(12, national.getHeight());
preparedStatement.setString(13, national.getBirthID());
preparedStatement.setString(14, national.getIDNumber());
preparedStatement.setString(15, national.getAge());
myResultSet = preparedStatement.executeQuery();
if (myResultSet.next())
{
//((Node)event.getSource()).getScene().getWindow().hide();
Stage window = new Stage();
FXMLLoader loader = new FXMLLoader();
Pane root = loader.load(getClass().getResource("../nationalid/PersonsDetailsView.fxml").openStream());
PersonsDetailsController personsDetailsController = (PersonsDetailsController) loader.getController();
personsDetailsController.getUsers(
(FileInputStream) national.getPicture(preparedStatement),
national.getFirstname(),
national.getLastname(),
national.getGender(),
national.getHeight(),
national.getDateOfBirth(),
national.getNationality(),
national.getAddress(),
national.getIDNumber(),
national.getIssueDate(),
national.getExpiryDate()
);
Scene scene = new Scene(root);
window.setTitle("Hello " + txtfirstname.getText() + ", this is your menu page");
window.getIcons().add(new Image("file:users.png"));
window.setScene(scene);
window.show();
}
preparedStatement.close();
} catch (SQLException ep) {
//Logger.getLogger(AddUserController.class.getName()).log(SEVERE, null, ep);
System.out.println(ep);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@FXML private JFXButton btnbrowse;
public File file;
public FileChooser fileChooser;
private Desktop desktop = Desktop.getDesktop();
public Image image;
private FileInputStream fileInputStream;
public void browseToSelect(ActionEvent event) {
Stage window = new Stage();
fileChooser = new FileChooser();
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif"),
new FileChooser.ExtensionFilter("Text Files", "*txt"),
new FileChooser.ExtensionFilter("Audio Files", "*wav", "*.mp3", "*.aac"),
new FileChooser.ExtensionFilter("All Files", "*.*")
);
file = fileChooser.showOpenDialog(window);
if(file != null)
{
imageView.setVisible(true);
image = new Image(file.toURI().toString(), 247, 232, true, true);
imageView.setImage(image);
//try {desktop.open(file);} catch (IOException e) {e.printStackTrace();}
}
}
public void pullIDDataOnClick() {
clearFields();
imageView.setVisible(true);
try
{
myConnection = SqliteConnection.connector();
NationalIDModel nationalIDModel = (NationalIDModel)nationalTable.getSelectionModel().getSelectedItem();
String query = "SELECT * FROM nationalid_table WHERE birthid = ?";
preparedStatement = myConnection.prepareStatement(query);
preparedStatement.setString(1, String.valueOf(nationalIDModel.getBirthID()));
myResultSet = preparedStatement.executeQuery();
while(myResultSet.next())
{
txtUniqueNumber.setText(myResultSet.getString("unique_number"));
txtPassportID.setText(myResultSet.getString("passportID"));
txtfirstname.setText(myResultSet.getString("firstname"));
txtlastname.setText(myResultSet.getString("lastname"));
(issuedatepicker.getEditor()).setText(myResultSet.getString("issuedate"));
(expirydatepicker.getEditor()).setText(myResultSet.getString("expirydate"));
InputStream inputStream = myResultSet.getBinaryStream("picture");
try (OutputStream outputStream = new FileOutputStream(new File("photo.jpg"))) {
byte[] content = new byte[1024];
int size;
while((size = inputStream.read(content)) != -1)
{
outputStream.write(content, 0, size);
}
outputStream.close();
inputStream.close();
image = new Image("file:photo.jpg", 247, 232, true, true);
imageView.setImage(image);
} catch (IOException e) {
e.printStackTrace();
}
txtnationality.setText(myResultSet.getString("nationality"));
(birthdatepicker.getEditor()).setText(myResultSet.getString("dateofbirth"));
if(null != myResultSet.getString("sex"))
switch (myResultSet.getString("sex")) {
case "Male":
rdmale.setSelected(true);
break;
case "Female":
rdfemale.setSelected(true);
break;
default:
rdmale.setSelected(false);
rdfemale.setSelected(false);
break;
}
else
{
rdmale.setSelected(false);
rdfemale.setSelected(false);
}
txtplaceofbirth.setText(myResultSet.getString("address"));
txtHeight.setText(myResultSet.getString("height"));
txtbirthid.setText(myResultSet.getString("birthid"));
txtIDNumber.setText(myResultSet.getString("id_number"));
txtNumberOfIDCard.setText(myResultSet.getString("number_of_id"));
txtage.setText(myResultSet.getString("age"));
statuslbl.setText(myResultSet.getString("status"));
}
//goToPrint();
preparedStatement.close();
myResultSet.close();
}
catch (SQLException ex)
{
System.out.println(ex);
Logger.getLogger(NationalViewController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
应该带我到这个界面PersonsDetailsController:
package com.nationalid;
public class PersonsDetailsController implements Initializable{
@Override
public void initialize(URL location, ResourceBundle resources) {
}
public void getUsers(FileInputStream inputeStream, String userinfo1, String userinfo2,
String userinfo3, String userinfo4,
String userinfo5, String userinfo6,
String userinfo7, String userinfo8,
String userinfo9, String userinfo10)
{
userlabel.setText("View " + userinfo2 + "'s" + " Details");
try (OutputStream outputStream = new FileOutputStream(new File("photo.jpg"))) {
byte[] content = new byte[1024];
int size;
while((size = inputeStream.read(content)) != -1)
{
outputStream.write(content, 0, size);
}
outputStream.close();
inputeStream.close();
Image image = new Image("file:photo.jpg", 247, 232, true, true);
imageViewCapture.setImage(image);
} catch (IOException e) {
e.printStackTrace();
}
txtfirstname.setText(userinfo1);
txtlastname.setText(userinfo2);
txtgender.setText(userinfo3);
txtheight.setText(userinfo4);
txtdateofbirth.setText(userinfo5);
txtnationality.setText(userinfo6);
txtaddress.setText(userinfo7);
txtidnumber.setText(userinfo8);
txtissuedate.setText(userinfo9);
txtexpirydate.setText(userinfo10);
}
}
我可以打印。
我的目标是将图像从NationalViewController传递到我可以打印的PersonsDetailsController。
我已成功将图像从数据库拉到NationalViewController,方法为pullIDDataOnclick(),并将其显示在imageview中。
但是每当我点击查看按钮转到PersonsDetailsController界面时,我都会提到错误。
请帮忙!感谢。