Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';t删除另一进程正在使用的图像_Java_Image_Exception - Fatal编程技术网

Java Can';t删除另一进程正在使用的图像

Java Can';t删除另一进程正在使用的图像,java,image,exception,Java,Image,Exception,我开始在一个SpringBoot项目中使用库来制作缩略图 但是当我试图删除文件时,遇到了一个问题,我得到了一个异常,告诉我该文件正被另一个进程使用。我对Java非常陌生,我不知道问题可能来自哪里,我应该停止/关闭哪个进程: File originalFile = mediaUtils.saveFile(pathOriginal, file); String path = mediaUtils.resolvePath(imageDir, name, false, image.getCreatio

我开始在一个SpringBoot项目中使用库来制作缩略图 但是当我试图删除文件时,遇到了一个问题,我得到了一个异常,告诉我该文件正被另一个进程使用。我对Java非常陌生,我不知道问题可能来自哪里,我应该停止/关闭哪个进程:

File originalFile = mediaUtils.saveFile(pathOriginal, file);

String path = mediaUtils.resolvePath(imageDir, name, false, image.getCreation());
mediaUtils.saveJPG(originalFile, file.getContentType(), WIDTH_IMAGE_SIZE, path);

String pathThumb = mediaUtils.resolvePath(imageDir, name, true, image.getCreation());
mediaUtils.saveJPG(originalFile, file.getContentType(), WIDTH_IMAGE_SIZE_THUMB, pathThumb);

public File saveFile(String filePath, MultipartFile file) {
try {
  Path path = Paths.get(getPath(filePath));
  Files.createDirectories(path.getParent());

  Files.copy(file.getInputStream(), path);

  return new File(path.toString());

} catch (IOException e) {
  LOG.error("could not save file", e);
  throw new FileException("could not create file: " + getPath(filePath), e);
}
}

private void saveJPG(InputStream imageInputStream, File file, String contentType, int newWidth, String outputPath) {
try {
  // verify it is an image
  if (!Arrays.asList("image/png", "image/jpeg").contains(contentType)) {
    throw new IllegalArgumentException("The file provided is not a valid image or is not supported (should be png or jpeg): " + contentType);
  }

  // Create input image
  BufferedImage inputImage = ImageIO.read(imageInputStream);
  newWidth = newWidth > inputImage.getWidth() ? inputImage.getWidth() : newWidth;
  double ratio = (double) inputImage.getWidth() / (double) inputImage.getHeight();
  int  scaledHeight = (int) (newWidth / ratio);

  Path path = Paths.get(baseUrl + outputPath + ".jpg");

  Thumbnails.of(file)
    .size(newWidth, scaledHeight)
    .toFile(path.toFile());

  LOG.info("writing image to {}", path);

} catch (IOException e) {
  LOG.error("could not write image", e);
}
}

感谢您的建议或帮助:)

您应该确保在使用输入流和文件后关闭它们。 否则,你提到的事情就会发生。进程会阻止您的文件

因此,我建议不要使用简单的try-catch块,而是使用try-with-resources来关闭底层流和文件。例如:

try(InputStream imageInputStream = new FileInputStream(...)) {
    // do your stuff
}

括号中的代码完成或出现异常后,inputstream将关闭。

哦,好的,你对java不是真的感兴趣,你只想使用你的ThumblAllAtri,但我处于一个特定的情况下,我在项目中学习,所以即使有非常基本的东西,有时对我来说也有点困难:)