因此,我有一个程序,用户可以在其中上传图像,然后将其和产品保存到在线商店。但是我遇到一个很奇怪的问题,其中没有文件名被识别。基本上,用户使用文件选择器在本地上传文件。我已经尝试将其保存到资源文件夹中,也尝试将其保存到资源/图像中,并且尝试将其保存到main / images中,并且每次发生相同的情况时都尝试过。用户选择了要上传的照片,我看着它进入了正确的文件夹,但是当用户单击“上传”并创建新产品时,抛出了一个错误,指出无效的URL或资源不存在。这是东西。资源确实存在。我看着它出现在我的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文件已添加到我的项目中。当我在正确的文件夹中看到该URL或资源不存在错误时,这对我来说没有任何意义。我什至可以将文件的视频剪辑添加到正确的位置,然后说那里没有文件。
将产品添加到首页的方法很好。过去一直有效。实际上,只有文件名才给我一个错误。
谢谢
答案 0 :(得分:0)
您是否尝试过将图像作为文件而不是作为字符串加载?
try {
File pathToFile = new File("image.png");
Image image = ImageIO.read(pathToFile);
} catch (IOException ex) {
ex.printStackTrace();
}
答案 1 :(得分:0)
Image
构造函数需要一个URL,而不是文件名。您错过了一个计划;此外,有效的文件名可能不是有效的URL。
使用File
或Path
的功能转换为URL:
String filename = new File(otherFileLocation + file.getName()).toURI().toString();