为了将 xml 输出写入 Tomcat 服务器中的文件,我编写了以下代码。当我运行代码时,出现以下错误。我可以知道我的代码有什么问题吗?提前致谢。它应该创建一个名为 test 的目录,然后在我在服务器中指定的路径中创建文件 test.xml。但是,它并没有这样做,而是在我的本地计算机中寻找该路径
java.io.FileNotFounException: C:\test\test.xml 系统找不到指定的路径
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("/test/test.xml"));
transformer.transform(source, result);
// Output to console for testing
StreamResult consoleResult = new StreamResult(System.out);
transformer.transform(source, consoleResult);
最佳答案
默认情况下,Tomcat 将 java.io.tmpdir
系统属性的值设置为其 tmp
目录。所以下面的代码应该创建一个 File
对象指向 Tomcat tmp 中的一个文件:
String tempDir = System.getProperty("java.io.tmpdir");
File outputFile = new File(tempDir, "test.xml");
关于java - 无法将文件写入服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46652348/