我正在一个JavaFX项目(应用程序)上工作,我有我所有的方法设置,它所有的工作包括一个客户上传图像方法(他可以上传他的照片),我还有一个可序列化的文件,包括我所有的应用程序信息,但它不允许我保存"客户上传"的图像。.我正在寻找一种方法,允许我在改变场景或退出项目后保存这张照片
这是我的序列化代码:
public static void saveFile() {
System.out.println("Saving");
// Serialization
try {
// Saving of object in a file
FileOutputStream file = new FileOutputStream("Rest.ser");
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(restaurant);
out.close();
file.close();
}
catch (IOException ex) {
System.out.println(ex);
}
}
public static boolean readingFile() {
try {
File filec = new File("Rest.ser");
if (filec.isFile() == false || filec.canRead() == false)
return false;
FileInputStream file = new FileInputStream(filec);
ObjectInputStream in = new ObjectInputStream(file);
restaurant = (Restaurant) in.readObject();
in.close();
file.close();
System.out.println("Object has been deserialized ");
return true;
}
catch (IOException ex) {
System.out.println("No ser file");
}
catch (ClassNotFoundException ex) {
System.out.println("ClassNotFoundException is caught");
}
return false;
}
这是我的照片上传代码:
@FXML
private void UploadImageActionPerformed(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilterJPG
= new FileChooser.ExtensionFilter("JPG files (*.JPG)", "*.JPG");
FileChooser.ExtensionFilter extFilterjpg
= new FileChooser.ExtensionFilter("jpg files (*.jpg)", "*.jpg");
FileChooser.ExtensionFilter extFilterPNG
= new FileChooser.ExtensionFilter("PNG files (*.PNG)", "*.PNG");
FileChooser.ExtensionFilter extFilterpng
= new FileChooser.ExtensionFilter("png files (*.png)", "*.png");
fileChooser.getExtensionFilters()
.addAll(extFilterJPG, extFilterjpg, extFilterPNG, extFilterpng);
//Show open file dialog
File file = fileChooser.showOpenDialog(null);
try {
BufferedImage bufferedImage = ImageIO.read(file);
WritableImage image = SwingFXUtils.toFXImage(bufferedImage, null);
Customerimage.setImage(image);
Customerimage.setFitWidth(200);
Customerimage.setFitHeight(200);
Customerimage.scaleXProperty();
Customerimage.scaleYProperty();
Customerimage.setSmooth(true);
Customerimage.setCache(true);
FileInputStream fin = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
for (int readNum; (readNum = fin.read(buf)) != -1;) {
bos.write(buf, 0, readNum);
}
byte[] person_image = bos.toByteArray();
} catch (IOException ex) {
Logger.getLogger("ss");
}
}
有谁能帮我吗?谢谢你。您可以直接从URL加载图像,URL是指向位置的字符串路径。类似于ImageView。您可以根据用户在FileChooser中选择的文件构造此位置。您可能不需要加载和保存图像数据所需的所有代码。如果你想记住图像的位置,你可以把它作为字符串存储在某个地方。