3

编辑:getResourceAsStream() 与 FileInputStream

是我找到的最好的解释。就个人而言,在尝试了 InputStream 的所有不同子类之后,我觉得 FileInputstream() 返回的 InputStream 和 getResourceAsStream() 返回的实现在某些方面略有不同(导致与 javax.xml.parsers.. 不兼容)

我将把这个打开一段时间,以防有人碰巧有答案,但我已经完成了。感谢所有的意见、建议和帮助。时间也转移到下一件事。


我有一个 servlet,它从 XML ini 文件中收集它的初始化数据,它像这样从 servlet 会话上下文中将其作为 InputStream 打开

   HttpSession session = request.getSession(true);
   ServletContext ctx=session.getServletContext();
   InputStream myini = ctx.getResourceAsStream("/WEB-INF/myini.xml");

这行得通,但后来我正在编写 JUnit 测试并在 setup 函数中,我需要访问这个相同的 ini。我在测试环境中没有 servlet 上下文,所以我尝试使用创建 InputStream

InputStream testing = new FileInputStream(String pathToFile);

并且

InputStream testing = new FileInputStream(File fileObj); 

XML 解析器抛出的异常(下)

我发现无法为我的 init 文件获取 InputStream 并且被迫使用 File.

我检查了javadoc,移动了文件位置以防安全限制。最终将双重构造函数添加到需要 ini 的类中,以接受 File 和 InputStream 以满足单元测试(我需要 File ref)和运行时(从 servlet 会话上下文返回 InputStream)...

但我很困惑/很沮丧,所以我不得不问

为什么我可以使用“文件”返回一个文件对象,然后它可以被 javax.xml.parsers 成功解析(见下面的函数)

File myini = new File("C:\\apache-tomcat-7.0.30\\myini\\myini.xml");

但我不能用“InputStream”做同样的事情吗?

InputStream myini = new FileInputStream("C:\\apache-tomcat-7.0.30\\myini\\myini.xml");

使用完全相同的字符串路径(即文件存在)

每个都被分别传递给

public xmlNode parse(InputStream is) throws xmlException {
        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            document = dBuilder.parse(is);
            document.getDocumentElement().normalize();

            xmlNode node = new xmlNode(document.getDocumentElement());
            return node;
        } catch (ParserConfigurationException e) {
            throw new xmlException("Error in configuration of XML parser", e);
        } catch (SAXException e) {
            throw new xmlException("Error in parsing XML document", e);
        } catch (IOException e) {
            throw new xmlException("Error in reading InputStream", e);
        }
    }

或者

public xmlNode parse(File file) throws xmlException {
        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            document = dBuilder.parse(file);
            document.getDocumentElement().normalize();

            xmlNode node = new xmlNode(document.getDocumentElement());
            return node;
        } catch (ParserConfigurationException e) {
            throw new xmlException("Error in configuration of XML parser", e);
        } catch (SAXException e) {
            throw new xmlException("Error in parsing XML document", e);
        } catch (IOException e) {
            throw new xmlException("Error in opening file", e);
        }
    }

只有在调用 InputStream 方法时才会抛出此异常(在上述适当的初始化之后)

xml.utils.xmlException: Error in reading InputStream
    at xml.utils.xmlDocument.parse(xmlDocument.java:40)
    at com.jcando.util.XMLini.<init>(XMLini.java:49)

是否有不同的方式将路径定义为 InputStream 的字符串?是否有我不知道的安全块?

如果有人可以解释我缺少什么,或者我在哪里厚得像树桩,我会很感激。

4

3 回答 3

2

你总是可以写File f = new File("anyName"):即使文件不存在,它也永远不会抛出异常。

然而,如果文件不存在,如您在文档InputStream myini = new FileInputStream("anyName")中看到的那样,写入将引发异常。FileNotFoundException

于 2013-07-17T16:13:14.500 回答
1

我怀疑您可能遇到字符编码错误或与解析文档中的相对 url 有关的错误(尽管我不确定这是否会导致异常)。

我认为您可能想尝试以下方法:

InputSource source = new InputSource(is);
source.setEncoding(... your character encoding ...);
source.setSystemId(systemId);
dBuilder.parse(inputSource);

其中systemId确定为file.toURI().toASCIIString()

于 2013-07-17T19:18:11.217 回答
0

getResourceAsStream() 与 FileInputStream

是我找到的最好的解释。就个人而言,在尝试了 InputStream 的所有不同子类之后,我觉得 FileInputstream() 返回的 InputStream 和 getResourceAsStream() 返回的实现在某些方面略有不同(导致与 javax.xml.parsers.. 不兼容)

于 2013-07-22T08:15:23.607 回答