0

i am copying a file like this:

            File copy = new File(file.getName());
            // path relative to current working directory
            Path relativePath = Paths.get("src/main/resources/images/",  copy.getPath());
            java.nio.file.Files.copy(
                    file.toPath(), // input path
                    relativePath, // target path
                    java.nio.file.StandardCopyOption.REPLACE_EXISTING);

            logger.info("Copy file to path {}", relativePath);
            // save path for NPC
            currentImagePath = "/images/"+copy.getPath();

and then save the path to the database. When I try to open the copied file like this:

            Image image = new Image(bild);

(bild is the currentImagePath from before), I get an Exception saying that it was an invalid URL or the resource was not found. However, if I end the program and start it anew, opening the file works without a problem. Is there a solution to this?

Tandar
  • 3
  • 2

2 Answers2

1

Maybe the file is not done copying yet. It could take the OS a while to copy the file. The copy method might return before the copy is complete.

Teto
  • 475
  • 2
  • 10
  • that shouldn't be the case because the copy method returns the total bytes written – sorifiend Jun 16 '16 at 00:19
  • 1
    @ sorifiend. That is incorrect. The copy method he is using returns a Path. Further the documentation at the top of the Files class says "In most cases, the methods defined here will delegate to the associated file system provider to perform the file operations.". Furthermore the Files class is part of the non-blocking io package. I would expect such calls, delegated to the OS, to not block my thread. In any case, it is easy enough for the OP to test my hypothesis. – Teto Jun 16 '16 at 01:24
  • Sorry, right you are, I was reading the wrong copy method. I will remove my down vote as soon as it becomes unlocked. – sorifiend Jun 16 '16 at 01:45
  • Turns out I am unable to undo my down vote unless your answer is edited. Sorry again. – sorifiend Jun 16 '16 at 02:14
  • I am not too worried about cred, but I added a space to make it show an edit. – Teto Jun 17 '16 at 02:15
0

Without seeing more code I would say that your issue is this line Image image = new Image(bild);.

Also "/images/"+copy.getPath() is not the same as relativePath, are you using them both correctly? Try this to check if there is an issue:

System.out.println("/images/"+copy.getPath());
System.out.println(relativePath);
System.out.println(copy.getAbsolutePath());

Was your issue caused by an incorrect path?

Now try something a bit like this to load your image (Note how ImageIO is used):

System.out.println("Does file exist: "+ copy.exists());
System.out.println("Is file locked: "+ copy.canWrite());
while (copy.canWrite() != true){
    //stall your application until the file is accessible.
}
//now load the file
Image image = ImageIO.read(copy);

This answer has excellent info on checking for a locked file: https://stackoverflow.com/a/1500521/1270000

Community
  • 1
  • 1
sorifiend
  • 5,927
  • 1
  • 28
  • 45
  • Edited to include additional check as suggested by Teto – sorifiend Jun 16 '16 at 02:09
  • The problem was that I tried to access the files via classpath, which wouldn't work without a restart since the paths aren't remade at runtime. – Tandar Jun 16 '16 at 20:42