引起原因:创建 FileOutputStream 对象时 java.io.FileNotFoundException: /path/somthing.jar (是一个目录)

发布于 2024-12-12 08:45:20 字数 1521 浏览 0 评论 0原文

这是一个非常基本的问题,但它让我挠了 4 个小时,现在我放弃了。 为了提供尽可能多的信息,我可以说这是一个java webapp项目,在ubuntu 11.04下使用zk 5.0.8作为前端+spring+hibernate+maven,并将basedir权限设置为777。

尝试了文件上传,一切似乎都正常当我确信我的代码是正确的时,它只是不起作用。

这是代码

private boolean saveUploadledFile(Media uploadedMedia, String basedir) {

    String code = codeGenerator.generateContentCode(15);
    String FINAL_DIR_PATH = basedir + "/"+"Racing" + "/" + code;
    String FINAL_FILE_PATH = FINAL_DIR_PATH + "/" + uploadedMedia.getName();
    alert(FINAL_DIR_PATH);
    try {
        File finaldir = new File(FINAL_DIR_PATH);
        //apache commons
        FileUtils.forceMkdir(finaldir);
        alert("Size equals" + uploadedMedia.getByteData().length);
        fout = new FileOutputStream(new File(FINAL_DIR_PATH+"/"+addContentWindow1$txtName.getText()+".jar"));
          //apache commons
          IOUtils.copy(uploadedMedia.getStreamData(), fout);

    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(fout);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    return false;
}

new FileOutputStream 总是抛出异常。因此,如果我无法指定要保存的位置如何保存文件。有什么想法吗?我故意输出文件的大小以确保存在文件。任何人都可以透露一些信息吗?感谢您阅读本文,

实际的例外是

引起:java.io.FileNotFoundException:/joseph/mbcs/Games/Racing/20314/somthing.jar(是一个目录)

this is a really basic one, but it got me scratch my hear for 4 hours, now i'm giving up.
to give as much as possible information , i can say it's a java webapp project with zk 5.0.8 as frontend+spring+hibernate+maven under ubuntu 11.04 with permission to the basedir set to 777.

tried the file upload everything seems to be ok and where i have confidence that my code is correct it's just not working.

here is the code

private boolean saveUploadledFile(Media uploadedMedia, String basedir) {

    String code = codeGenerator.generateContentCode(15);
    String FINAL_DIR_PATH = basedir + "/"+"Racing" + "/" + code;
    String FINAL_FILE_PATH = FINAL_DIR_PATH + "/" + uploadedMedia.getName();
    alert(FINAL_DIR_PATH);
    try {
        File finaldir = new File(FINAL_DIR_PATH);
        //apache commons
        FileUtils.forceMkdir(finaldir);
        alert("Size equals" + uploadedMedia.getByteData().length);
        fout = new FileOutputStream(new File(FINAL_DIR_PATH+"/"+addContentWindow1$txtName.getText()+".jar"));
          //apache commons
          IOUtils.copy(uploadedMedia.getStreamData(), fout);

    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(fout);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    return false;
}

new FileOutputStream always throws exceptions. so if i can't specify where i want to save how to save the files. any ideas? i intentionally output the size of the file to make sure there is a file. Can anyone shed some light? thanks for reading this

the actual exception is

Caused by: java.io.FileNotFoundException: /joseph/mbcs/Games/Racing/20314/somthing.jar (is a directory)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

驱逐舰岛风号 2024-12-19 08:45:20

我可能是错的,但这部分代码不是有问题吗?

     if (!finaldir.exists()) {
        if (!finaldir.canWrite())
            finaldir.mkdirs(); // this creates no directory no error
         else
             alert("Cannot write to the directory" );
     }

如果该目录不存在,则检查是否无法在那里写入,然后创建它,否则会输出错误。我认为 ! 那里是错误的。

可能是您的问题的原因,但也可能不是。

I may be wrong, but isn't this part of your code faulty?

     if (!finaldir.exists()) {
        if (!finaldir.canWrite())
            finaldir.mkdirs(); // this creates no directory no error
         else
             alert("Cannot write to the directory" );
     }

If the directory doesn't exist, you check if you can't write there and then create it, otherwise you output an error. I think that ! there is wrong.

Might be the reason for your problem but it just as well might not be.

我喜欢麦丽素 2024-12-19 08:45:20

省略:

if(finalfile.canWrite()) {

因为您刚刚创建了文件并正在写入它。
当 OutputStream 构造函数写入失败时,您将得到一个错误命名的 FileNotFoundException(我认为在 Java 7 中重命名)。

另一个提示,像复制这样的一般工作可以使用 apache-commons(IOUtils、FileUtils)来完成,
IE:

import org.apache.commons.fileupload.util.Streams;
Streams.copy(in, out, false);

Leave out:

if(finalfile.canWrite()) {

as you just created the file and are writing to it.
You will get a misnamed FileNotFoundException (I think renamed in Java 7) when the OutputStream constructor failed in writing.

Another tip, general work like copying may be done using apache-commons (IOUtils, FileUtils),
i.e.:

import org.apache.commons.fileupload.util.Streams;
Streams.copy(in, out, false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文