javax.xml.transform.TransformerException: java.io.FileNotFoundException: <file_name>(Access is denied)

社会主义新天地 提交于 2019-12-24 02:09:37

问题


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)


回答1:


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:

C:\ProgramData.\config.xml

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").




回答2:


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());




回答3:


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 "".




回答4:


try This

file = new File(System.getProperty("user.dir") + "/YOURFILEADDRESS.xml");


来源:https://stackoverflow.com/questions/6675292/javax-xml-transform-transformerexception-java-io-filenotfoundexception-file-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!