I am getting exception at last line of code -
Transformer transformer = TransformerFactory.newInstance().newTransformer();
DOMSource xmlSource = new DOMSource(document);
StreamResu开发者_C百科lt 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)
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());
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").
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
file = new File(System.getProperty("user.dir") + "/YOURFILEADDRESS.xml");
精彩评论