引起原因:创建 FileOutputStream 对象时 java.io.FileNotFoundException: /path/somthing.jar (是一个目录)
这是一个非常基本的问题,但它让我挠了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能是错的,但这部分代码不是有问题吗?
如果该目录不存在,则检查是否无法在那里写入,然后创建它,否则会输出错误。我认为
!
那里是错误的。可能是您的问题的原因,但也可能不是。
I may be wrong, but isn't this part of your code faulty?
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.
省略:
因为您刚刚创建了文件并正在写入它。
当 OutputStream 构造函数写入失败时,您将得到一个错误命名的 FileNotFoundException(我认为在 Java 7 中重命名)。
另一个提示,像复制这样的一般工作可以使用 apache-commons(IOUtils、FileUtils)来完成,
IE:
Leave out:
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.: