Home > Net >  the action can't be completed because the file is open in openjdk platform binary spring boot
the action can't be completed because the file is open in openjdk platform binary spring boot

Time:08-06

Hey I'm trying to delete a image (file) but I can't :(

That how I upload the image:

 try {
            List<String> imagesPaths = new ArrayList<>();
            for (String image : imagesBytes)
            {
                String base64Image = image.split(",")[1];
                byte[] imageByte = javax.xml.bind.DatatypeConverter.parseBase64Binary(base64Image);
                String folder = "C:/images/"   LoggedInUser.UserId();
                File newDirectory = new File(folder);
                if (!newDirectory.exists())
                {
                    newDirectory.mkdirs();
                }
                long timeMilli = new Date().getTime();
                String imageType = image.substring("data:image/".length(), image.indexOf(";base64"));
                String path = timeMilli   "."   imageType;
                Files.write(Paths.get(folder, path), imageByte);
                String newPath = LoggedInUser.UserId()   "/"   path;
                imagesPaths.add(newPath);
            }
            logger.debug("uploadImages() in ImageService Ended by  "   LoggedInUser.UserName());
            return new ResponseEntity<>(imagesPaths, HttpStatus.OK);
        } catch (Exception e) {
            throw new ApiRequestException(e.getMessage());
        }

And this how I delete it :

    List<ImageJpa> images = imageRepository.findByStatus(Image.UNUSED.status);
    System.out.println(images.size()   ": Images are not used");
    images.forEach(image -> {
        String imagePath = "C:/images/"   image.getPath();
        System.out.println(imagePath);
        File imagePathFile = new File(imagePath);
            if (imagePathFile.exists())
            {
                boolean isDeleted = imagePathFile.delete();
                if (isDeleted)
                {
                    imageRepository.deleteById(image.getId());
                    System.out.println("Deleted the file: "   imagePathFile.getName());

                } else {System.out.println("Failed to delete the file. :"   imagePathFile.getName());}
            }else {
                System.out.println("Already Deleted");
            }
    });

Always I got (Failed to delete the file ...)

  • Note : The image will deleted if I ReRender the the project again or close and open the IDE.

CodePudding user response:

The problem was on other function :\

I was open the files after save it without CLOSE the connection after it !

This one what was messing on the read files

inputStream.close();
  • Related