java.io.FileNotFoundException:(访问被拒绝)将字节数组转换为图像文件

java.io.FileNotFoundException:(Access is denied) convert byte array to image file

我有一个字节数组,我想创建一个字节数组的图像文件(bmp 文件)。我在 src 中创建了一个图像文件夹(我的路径是 src/images/test.bmp)。我的代码在下面,在

OutputStream stream = new FileOutputStream(file);

我收到错误消息。我的问题是什么?我该如何解决?

public static void saveImage() {
    String s="........................";
    byte[] dataCustImg = Base64.decode(s.getBytes());

    File file = new File("/images/test.bmp");
    if (file.exists()) {
        file.delete();
    }
    file = new File("/images/test.bmp");
    file.mkdirs();
    try {
        OutputStream stream = new FileOutputStream(file);

        stream.write(dataCustImg);
        stream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

错误:

java.io.FileNotFoundException: \images\test.bmp (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)

在这里,当您调用 mkdir 时,它会将 test.bmp 创建为目录而不是文件,因此您必须先创建目录,然后才能创建文件。看下面的代码。

        File dir = new File("/images/");
        dir.mkdirs();
        file = new File("/images/test.bmp");
        file.createNewFile();
File file = new File("/images/test.bmp");

好的。

if (file.exists()) {
    file.delete();
}

冗余。去掉。 new FileOutputStream() 将创建一个新文件。

file = new File("/images/test.bmp");

冗余。去掉。它已经是同名的 File

file.mkdirs();

问题就在这里。改为

file.getParentFile().mkdirs();

您正在创建一个名为 "/images/test.bmp" 目录,而不仅仅是确保 "/images" 存在。这将导致 new FileOutputStream() 访问权限失败,因为您不能用文件覆盖目录。

try {
    OutputStream stream = new FileOutputStream(file);

现在继续。请注意,您首先必须手动删除目录 "/images/test.bmp"

public static void saveImage() {
    String s="........................";
    byte[] dataCustImg = Base64.decode(s.getBytes());

    File file = new File("/images/test.bmp");
    if(!file.getParentFile().exists()) {
      file.getParentFile().mkdirs();
    }
    if(!file.exists()) {
      try {
        file.createNewFile();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    // because stream.write(dataCustImg) will overwrite the file if the file has already existed.
    try {
        OutputStream stream = new FileOutputStream(file);

        stream.write(dataCustImg);
        stream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

异常的原因是你实际上创建了一个目录,路径/images/test.bmp

file = new File("/images/test.bmp");
file.mkdirs();

稍后你想打开一个文件

OutputStream stream = new FileOutputStream(file);

如果您想在创建文件之前确保目录 /images 存在,您应该使用

File dir = new File("/images/");
dir.mkdirs();

没有必要在写入文件之前显式删除,因为默认情况下文件会被覆盖。

在下面找到一个小的工作片段。

// create the directory if not exist
File dir = new File("/images/");
dir.mkdirs();
// create a new file or overwrite an existing one
File file = new File("/images/test.bmp");
try (OutputStream os = new FileOutputStream(file)) {
    os.write((int) System.currentTimeMillis());
}