javax.xml.transform.TransformerException: java.io.FileNotFoundException: <file_name>(访问被拒绝)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6675292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javax.xml.transform.TransformerException: java.io.FileNotFoundException: <file_name>(Access is denied)
提问by Ashish Pancholi
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)
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) ) 在 com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getOutputHandler(Unknown Source) 在 com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source) 在com..main.dr.a(Unknown Source) at com..main.dr.a(Unknown Source) at com..main.dr.a(Unknown Source) at com..main.hk(Unknown Source) at com..main.ay.run(Unknown Source) 引起:java.io.FileNotFoundException: C:\ProgramData.\config\.xml (Access is denied) at java.io.FileOutputStream.open(Native Method) at java .io.FileOutputStream。(来源不明)在 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。(来源不明)在 java.io.FileOutputStream。(来源不明)在 com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getOutputHandler(来源不明)在 com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(未知) 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.hk(Unknown Source) 在 com..main.ay.run(Unknown Source) 和原因是java.io.FileNotFoundException: C:\ProgramData.\config\.xml (Access is denied) (来源不明)在 java.io.FileOutputStream。(来源不明)在 com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getOutputHandler(来源不明)在 com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(未知) 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.hk(Unknown Source) 在 com..main.ay.run(Unknown Source) 和原因是java.io.FileNotFoundException: C:\ProgramData.\config\.xml (Access is denied) (来源不明)在 java.io.FileOutputStream。(来源不明)在 com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.getOutputHandler(来源不明)在 com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(未知) 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.hk(Unknown Source) 在 com..main.ay.run(Unknown Source) 和原因是java.io.FileNotFoundException: C:\ProgramData.\config\.xml (Access is denied)
采纳答案by paulsm4
Sounds like file permissions on your XML files, doesn't it?
听起来像是对 XML 文件的文件权限,不是吗?
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.
如果您从 Web 上下文执行,请记住 Web 用户(例如,Linux/Apache 下的“nobody”或 Windows/IIS 下的“IUSR_MACHINE”)具有访问您的文件系统的最低权限。
And this is a Good Thing: especially if your application is exposed to the Internet :)
这是一件好事:尤其是当您的应用程序暴露在 Internet 上时 :)
PS: Also, the directory path you cited doesn't look right:
PS:另外,你引用的目录路径看起来不对:
C:\ProgramData.\config.xml
C:\ProgramData.\config.xml
Are you sure it's not supposed to be "C:\Program Data\config.xml"????
你确定它不应该是“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").
PPS:当我们谈论“文件权限”时;Windows Vista、Windows 7 和 Server 2008 都有更严格的规则,禁止访问驱动器根目录(例如:“c:\”)或系统目录(例如:“c:\windows”或“c:\Program files”)中的任何内容。
回答by Sue Anne
Try changing it to use the file's URI.getPath()
instead of just passing File object into StreamResult.
尝试将其更改为使用文件的URI.getPath()
而不是仅将 File 对象传递到 StreamResult。
eg. StreamResult result = new StreamResult(anOutputFile.toURI().getPath());
例如。 StreamResult result = new StreamResult(anOutputFile.toURI().getPath());
回答by Dilruk
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 ""
.
可能是您正在使用循环遍历一组文件,有时您最终会尝试转换没有任何值的文件,例如null
或 ""
。
回答by Ahmad R. Nazemi
try This
试试这个
file = new File(System.getProperty("user.dir") + "/YOURFILEADDRESS.xml");