gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:22:07 25 4
gpt4 key购买 nike

我有一个字节数组,我想创建一个字节数组的图像文件(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)

最佳答案

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

好的。

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

冗余。消除。 new FileOutputStream() 将创建一个新文件。

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

冗余。消除。它已经是一个具有此名称的文件

file.mkdirs();

问题就出在这里。更改为

file.getParentFile().mkdirs();

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

try {
OutputStream stream = new FileOutputStream(file);

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

关于java.io.FileNotFoundException :(Access is denied) convert byte array to image file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37382901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com