gpt4 book ai didi

java - 我可以在项目的资源文件夹中看到图像,但仍然收到无效网址错误

转载 作者:行者123 更新时间:2023-12-02 05:43:56 25 4
gpt4 key购买 nike

所以我有一个程序,用户可以上传图像,然后将其和产品保存到在线商店。但我遇到了一个非常奇怪的问题,没有一个文件名被识别。基本上,用户使用文件选择器在本地上传文件。我已经尝试过将其保存到我的资源文件夹中,我已经尝试过将其保存到资源/图像中,并且尝试将其保存到主/图像中,并且每次发生同样的事情时。用户选择他们想要上传的照片,我看着它转到正确的文件夹,但是当用户单击上传并创建新产品时,会出现错误,指出无效的网址或资源不存在。事情是这样的。该资源确实存在。我看着它出现在我的 Intellij 项目窗口中。我什至将文件的路径打印到控制台,以防它去其他地方,但不,这是正确的文件路径。我想也许有一些奇怪的拼写错误,所以我将文件拖到终端,但不,它是确切的文件路径。我要疯狂地想弄清楚这一点。

上传发生的位置

public class UploadItems {

@FXML private TextField productName;
@FXML private TextField quantityForSale;
@FXML private TextField prices;
@FXML private Image image;
@FXML private Button imageUploadBtn;
@FXML private Button uploadBtn;
private MainController mainController;
private File file;
private ConnectToDb connectToDb = new ConnectToDb();
private StoreHomePageController storeHomePageController;
private final String fileLocation = "/Users/myname/IdeaProjects/AmeenazonOnlineStore/src/main/Images";

public UploadItems(){
mainController = new MainController();
storeHomePageController = new StoreHomePageController(mainController);

}
@FXML
private void initialize(){

imageUploadBtn.setOnAction(e -> {
uploadImageBtn();
});
uploadBtn.setOnAction(e -> {
uploadBtn();

});
}
private void uploadBtn(){
String name = productName.getText();
String otherFileLocation = "/Users/myname/IdeaProjects/AmeenazonOnlineStore/src/main/Images/";
int quantity = Integer.parseInt(quantityForSale.getText());
int price = Integer.parseInt(prices.getText());
String filename = otherFileLocation + file.getName();

if (!name.isEmpty() && quantity > 0 && price > 0){
String query = " INSERT INTO products(name, price, quantity, filename)"
+ " VALUES(?, ?, ?, ?)";
try {
PreparedStatement preparedStatement = connectToDb.getConn().prepareStatement(query);
preparedStatement.setString(1, name);
preparedStatement.setInt(2, price);
preparedStatement.setInt(3, quantity);
preparedStatement.setString(4, filename);

if (checkTableForProduct()){
// error is thrown here because the filename is invalid. printing to the console right here will show the valid file name
storeHomePageController.addToVBoxList(new Product(name, price, quantity, filename));

System.out.println("product added... hopefully ");
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Item Successfully Uploaded!");
alert.setHeaderText("You can now sell your stuff. We can't wait for our cut");
alert.setContentText("You better not stiff us or we'll send our goons out to get you!");
alert.showAndWait();
}
} catch (SQLException ex) {
ex.printStackTrace();
} try {

Window window = uploadBtn.getScene().getWindow();
window.hide();
FXMLLoader loader = new FXMLLoader(getClass().getResource("/StoreHomePageLayout.fxml"));
loader.setController(new StoreHomePageController(mainController));
Stage secondaryStage = new Stage();
secondaryStage.setTitle("Ameenazon");
secondaryStage.setHeight(800);
secondaryStage.setWidth(800);
Scene scene = new Scene(loader.load());
secondaryStage.setScene(scene);
secondaryStage.show();

} catch (IOException ex) {
ex.printStackTrace();
}
}
}
private void uploadImageBtn(){
String otherFileLocation = "/Users/myname/IdeaProjects/AmeenazonOnlineStore/src/main/Images/";
Stage stage = new Stage();
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Select Image");

file = fileChooser.showOpenDialog(stage);
if (file != null){
Path movefrom = FileSystems.getDefault().getPath(file.getPath());
Path target = FileSystems.getDefault().getPath(otherFileLocation + file.getName());
imageUploadBtn.getProperties().put(otherFileLocation, file.getAbsolutePath());
System.out.println("image upload button ");
try{
Files.move(movefrom,target, StandardCopyOption.ATOMIC_MOVE);
System.out.println("image moved");
} catch (IOException ex){
ex.printStackTrace();
}
}
}

这是产品类别

public class Product {

private String name;
private String companyName;
private double price;
private int quantity;
private String description;
private ImageView imageView;
private String fileName;
private String info;
private Image image;
quantity, String description, Image image, String fileName, String info){
public Product(String name, double price, int quantity, String fileNames) {
this.name = name;
this.companyName = companyName;
this.price = price;
this.description = description;
this.imageView = imageView;
this.quantity = quantity;
this.fileName = fileNames;
System.out.println(this.fileName);
System.out.println(fileNames);
// these both print the correct file path to the correct file which I can see in my project window
this.info = info;
this.image = new Image(fileNames);
// a bunch of getters
}

所以是的,我不知道发生了什么。这是因为我在 FXML 初始化中设置按钮操作吗?我可以看到 .png 文件已添加到我的项目中。当我可以在正确的文件夹中看到它时,我收到无效的网址或资源不存在错误,这对我来说没有意义。我什至可以将文件的视频剪辑添加到正确的位置,然后说那里没有文件。

将产品添加到首页的方法就可以了。它在过去曾发挥过作用。实际上只是文件名给了我一个错误。

谢谢

最佳答案

Image 构造函数需要一个 URL,而不是文件名。你缺少一个计划;此外,有效的文件名可能不是有效的 URL。

使用文件路径功能转换为URL:

String filename = new File(otherFileLocation + file.getName()).toURI().toString();

关于java - 我可以在项目的资源文件夹中看到图像,但仍然收到无效网址错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56115590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com