Java NIO 和 Windows 磁盘访问
Java NIO 在 Windows 上需要特殊权限吗?
当我在 Windows Server 2003 上运行以下 Java 代码时,它失败并出现“访问被拒绝”错误(这是 cygwin 终端窗口中的全部消息):
new FileOutputStream(outputFile).getChannel()
.transferFrom(new FileInputStream(inputFile).getChannel(), 0, Long.MAX_VALUE);
但是如果我使用 Apache commons-io (我假设它不使用 NIO,它适用于相同的输入和输出文件:
final FileInputStream inputStream = new FileInputStream(inputFile)
final FileOutputStream outputStream = new FileOutputStream(outputStream)
IOUtils.copy(inputStream, outputStream);
我正在使用管理员帐户运行 Java 5 是否必须设置一些特殊的文件权限?
Does Java NIO need special permissions on Windows?
When I run the following Java code on Windows Server 2003, it fails with an "access denied" error (that's the whole message in the cygwin terminal window):
new FileOutputStream(outputFile).getChannel()
.transferFrom(new FileInputStream(inputFile).getChannel(), 0, Long.MAX_VALUE);
but if I use Apache commons-io (which I assume does NOT use NIO, it works with the same input and output files:
final FileInputStream inputStream = new FileInputStream(inputFile)
final FileOutputStream outputStream = new FileOutputStream(outputStream)
IOUtils.copy(inputStream, outputStream);
I am running in Java 5 with an administrator account. Is there some special file permission that must set?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
原因在代码中:
new FileOutputStream(outputFile).getChannel()
.transferFrom(new FileInputStream(inputFile).getChannel(), 0, Long.MAX_VALUE);
代码在几个层面上都是错误的。
没有关闭流,异常意味着文件很可能无法写入。如果用户实际上可以访问,“拒绝访问”类型的异常指向资源泄漏(即不关闭),这会阻止任何其他操作完成。
你不能像那样没有循环地传输。尽管它可以在 Windows 上运行,但 TransferTo/From 不会立即读取/写入所有内容。认为它与 inputStream.read()->outputStream.write() 相同,除了它可以使用操作系统映射的 DMA 之外,它很相似。
TransferTo/From 在 Windows 上毫无用处,因为操作系统不支持它,因此它实际上起作用的原因是:它是模拟的。在 Linux/Solaris/MacOS 上,它只需传输 X 个字节即可完成。
The reasons is in the code:
new FileOutputStream(outputFile).getChannel()
.transferFrom(new FileInputStream(inputFile).getChannel(), 0, Long.MAX_VALUE);
The code is wrong on few levels.
no closing of the streams, the exception means most likely the file is unavailable for writing. Provided the user can actually access, "denied access" type of exception point to resource leaks (i.e. not closing) which prevents any other operation to finish.
You can't transfer like that w/o loop. Although it will work on Windows, transferTo/From does not read/write everything at once. Consider it the same as inputStream.read()->outputStream.write(), it's similar except it can use DMA mapped by the OS.
TransferTo/From is useless on windows as the OS does not support it, hence the reason it actually works: it's emulated. On Linux/Solaris/MacOS it can just transfer X bytes and be done it.
您在什么情况下执行?是否有并发线程使用同一个文件?
如果是这种情况,FileChannel 会锁定正在使用的全部或部分文件。锁定方法(部分文件或全部文件)取决于平台,Windows 2003 可能已经成为该技术的过时平台。
解决方案:更换操作系统或使用apache commons IO。
注意:如果在一次请求中阻塞了该文件并且没有解除阻塞,则必须重新启动 jvm。
In what context are you executing? Are there concurrent thread using the same file?
If this is your case, FileChannel lock all or part of file that is using. The lock method (partial file or all file) depends of the plataform, and it is posible that windows 2003 has been obsolete plataform for this technics.
Solution: Change OS or use apache commons IO.
Note: If you block the file in one request and you do not unblock, you must restart jvm.