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

时间:2016-05-23 04:57:54

标签: java image file filenotfoundexception fileoutputstream

我有一个字节数组,我想创建一个字节数组的图像文件(bmp文件)。我在src中创建了一个images文件夹(我的路径是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)

4 个答案:

答案 0 :(得分:2)

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"

答案 1 :(得分:0)

这里当你调用mkdir然后它创建test.bmp作为一个目录而不是一个文件,所以你必须首先创建目录然后你可以创建文件。见下面的代码。

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

答案 2 :(得分:0)

例外的原因是您实际创建了一个目录,其路径为/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());
}

答案 3 :(得分:-1)

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();
    }

}
相关问题