javax.xml.transform.TransformerException: java.io.FileNotFoundException:(访问被拒绝)
我在最后一行代码中遇到异常 -
Transformer transformer = TransformerFactory.newInstance().newTransformer();
DOMSource xmlSource = new DOMSource(document);
StreamResult result;
File f = new File(sFilePath);
if (f.exists() == false) {
result = new StreamResult(f);
} else {
result = new StreamResult(sFilePath);
}
transformer.transform(xmlSource, result);
异常堆栈跟踪是 -
java.io.FileNotFoundException: C:\ProgramData.\config\.xml(访问权限为 拒绝)堆栈跟踪 javax.xml.transform.TransformerException: java.io.FileNotFoundException: C:\ProgramData.\config\.xml(访问权限为 被拒绝)在 com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getOutputHandler(未知 来源)位于 com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(未知 来源)位于 com..main.dr.a(来源未知)位于 com..main.dr.a(来源不明)位于 com..main.dr.a(来源不明)位于 com..main.hk(来源不明)位于 com..main.ay.run(来源不明) 原因: java.io.FileNotFoundException: C:\ProgramData.\config\.xml(访问权限为 被拒绝)在 java.io.FileOutputStream.open(Native Method) 处 java.io.FileOutputStream。 (来源不明)位于 java.io.FileOutputStream。 (来源不明)...还有7个--------- java.io.FileNotFoundException: C:\ProgramData.\config\.xml(访问权限为 被拒绝)在 java.io.FileOutputStream.open(Native Method) 处 java.io.FileOutputStream。 (来源不明)位于 java.io.FileOutputStream。 (来源不明)位于 com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getOutputHandler(未知 来源)位于 com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(未知 来源)位于 com..main.dr.a(来源未知)位于 com..main.dr.a(来源不明)位于 com..main.dr.a(来源不明)位于 com..main.hk(来源不明)位于 com..main.ay.run(来源不明)及其原因 isjava.io.FileNotFoundException: C:\ProgramData.\config\.xml(访问权限为 被拒绝)
I am getting exception at last line of code -
Transformer transformer = TransformerFactory.newInstance().newTransformer();
DOMSource xmlSource = new DOMSource(document);
StreamResult result;
File f = new File(sFilePath);
if (f.exists() == false) {
result = new StreamResult(f);
} else {
result = new StreamResult(sFilePath);
}
transformer.transform(xmlSource, result);
The exception stacktrace is -
java.io.FileNotFoundException:
C:\ProgramData.\config\.xml (Access is
denied) stacktrace javax.xml.transform.TransformerException:
java.io.FileNotFoundException:
C:\ProgramData.\config\.xml (Access is
denied) at
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getOutputHandler(Unknown
Source) at
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown
Source) at com..main.dr.a(Unknown Source) at
com..main.dr.a(Unknown Source) at
com..main.dr.a(Unknown Source) at
com..main.h.k(Unknown Source) at
com..main.ay.run(Unknown Source) Caused by:
java.io.FileNotFoundException:
C:\ProgramData.\config\.xml (Access is
denied) at java.io.FileOutputStream.open(Native Method) at
java.io.FileOutputStream. (Unknown Source) at
java.io.FileOutputStream. (Unknown Source) ... 7 more ---------
java.io.FileNotFoundException:
C:\ProgramData.\config\.xml (Access is
denied) at java.io.FileOutputStream.open(Native Method) at
java.io.FileOutputStream. (Unknown Source) at
java.io.FileOutputStream. (Unknown Source) at
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getOutputHandler(Unknown
Source) at
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown
Source) at com..main.dr.a(Unknown Source) at
com..main.dr.a(Unknown Source) at
com..main.dr.a(Unknown Source) at
com..main.h.k(Unknown Source) at
com..main.ay.run(Unknown Source) and the cause
isjava.io.FileNotFoundException:
C:\ProgramData.\config\.xml (Access is
denied)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试将其更改为使用文件的
URI.getPath()
而不是仅将 File 对象传递到 StreamResult。例如。
StreamResult 结果 = new StreamResult(anOutputFile.toURI().getPath());
Try changing it to use the file's
URI.getPath()
instead of just passing File object into StreamResult.eg.
StreamResult result = new StreamResult(anOutputFile.toURI().getPath());
听起来像是 XML 文件的文件权限,不是吗?
如果您从 Web 上下文中执行,请记住 Web 用户(例如,Linux/Apache 下的“nobody”或 Windows/IIS 下的“IUSR_MACHINE”)具有访问文件系统的最低权限。
这是一件好事:特别是如果您的应用程序暴露在互联网上:)
PS:
另外,您引用的目录路径看起来不正确:
您确定它不应该是“C:\Program Data\config.xml”吗???
聚苯醚:
当我们谈论“文件权限”时; Windows Vista、Windows 7 和 Server 2008 都有更严格的规则,禁止访问驱动器根目录(例如:“c:\”)或系统目录(例如:“c:\windows”或“c:\Program files”)中的任何内容。
Sounds like file permissions on your XML files, doesn't it?
If you're executing from a web context, please bear in mind that the web user (for example, "nobody" under Linux/Apache, or "IUSR_MACHINE" under Windows/IIS) has MINIMAL privileges to access your filesystem.
And this is a Good Thing: especially if your application is exposed to the Internet :)
PS:
Also, the directory path you cited doesn't look right:
Are you sure it's not supposed to be "C:\Program Data\config.xml"????
PPS:
While we're talking about "file permissions"; Windows Vista, Windows 7 and Server 2008 all have stricter rules against accessing anything in a drive's root (EX: "c:\") or system directories (EX: "c:\windows" or "c:\Program files").
尝试检查您尝试转换的文件是否具有这些值并且它们是否有效。
可能您正在使用循环迭代一组文件,有时您最终会尝试转换没有任何值的文件,例如
null
或""
。Try checking whether the file you try to transform does have the values and that they are valid.
May be you are iterating over a set of files using a loop and some times you end up trying to transform files which do not have any value like
null
or""
.试试这个
try This