【问题标题】:Unable to read XML file with ApplicationContext into a InputStream无法将带有 ApplicationContext 的 XML 文件读入 InputStream
【发布时间】:2017-05-19 15:09:45
【问题描述】:

我正在尝试使用 spring-mvc 的ApplicationContext 将 XML 文件加载到输入流中。但我无法读取该文件,而且我得到的信息相互矛盾。

我正在尝试将 xml 文件加载到 InputStream 并通过以下方式找到该文件:

ApplicationContext ctx =
  new ClassPathXmlApplicationContext(new String[]{"classpath*:config/validation.xml"});

然后我从ApplicantionContext 初始化我的InputStream,但我不清楚如何。现在我有:

private InputStream forms = getClass().getClassLoader().getResourceAsStream("/validation.xml");

但目前的问题是我什至找不到文件。

冲突信息:

我将文件放在资源下,然后再次放在webapp-> WEB-INF-> config 中,试图找出可以从哪里加载。

ApplicationContext ctx =
    new ClassPathXmlApplicationContext(new String[]{"classpath*:config/validation.xml"});

FOUND!!!!!!!!!!!

public Validator() {

        this.testResource();

        if(ctx != null){
            System.out.println("FOUND!!!!!!!!!!!!!!!!!!!!"+ctx); //this prints
        }else{
            System.out.println("NOT FOUND");
        }
    }

输出:

发现!!!!!!!!!!!!!!!!!!!!!org.springframework.beans.factory.support.DefaultListableBeanFactory@635bc5cc:定义bean [];工厂层次结构的根

但是这表示找不到文件:

private void testResource(){

    Resource resource =
            this.ctx.getResource("classpath*:/validation.xml");

    try{
        InputStream is = resource.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));

        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();

    }catch(IOException e){
        System.out.println("ERROR it is not found: ");
        e.printStackTrace();
    }
}

--------------更新1----------------- ----

我现在有以下内容,它也给了我相互矛盾的信息,说找到了一个 IOException:

@Configuration
public class Validator {

    private InputStream forms;
    private ValidatorResources resources;

    ApplicationContext ctx =
            new ClassPathXmlApplicationContext(new String[]{"classpath*:config/validation.xml"});

    private Resource resource = this.ctx.getResource("classpath*:validation.xml");

    public Validator() {

        if(resource != null){
            System.out.println("FOUND!!!!!!!!!!!!!!!!!!!!"+resource);
        }else{
            System.out.println("NOT FOUND");
        }

        this.runInputStream();
    }

    private void runInputStream(){
        try{
            this.forms = this.resource.getInputStream();
            this.resources = new ValidatorResources(this.forms);

        }catch (IOException ex) {
            System.out.println("runInputStream IOException: "+ex);
        }catch (SAXException e) {
            System.out.println("runInputStream SAXException: "+e);
        }catch (Exception e){
            System.out.println("ERROR-------------"+e);
        }

    }
}

输出:

找到!!!!!!!!!!!!!!!!!!!!!类路径资源 [classpath*:validation.xml] runInputStream IOException: java.io.FileNotFoundException:类路径资源 [classpath*:validation.xml] 无法打开,因为它不存在

【问题讨论】:

  • 好吧,我不知道,但是你在运行一个网络应用程序吗? WEB-INF 应该通过 ServletContext 访问? stackoverflow.com/questions/12883861/…。你能把文件放在 src/main/resources 中吗,我希望在那里可以访问任何旧文件。
  • @KarlNicholas 我在资源和 WEB-INF 中有它。我想弄清楚为什么它找不到它。

标签: java xml spring-mvc


【解决方案1】:

你应该删除/

 getClass().getClassLoader().getResourceAsStream("validation.xml");

validation.xml 文件必须位于资源文件夹的根目录。

如果一切正常,一旦您的项目构建完成,您可以在 WEB-INF/classes 中找到 validation.xml 文件,因此它可以在类加载器中使用。

【讨论】:

  • 我在资源文件的根目录和webapp -> config -> 中有一个错误,当我执行private InputStream forms = getClass().getClassLoader().getResourceAsStream("validation.xml"); 时出现ERROR-------------java.lang.IllegalArgumentException: Stream[0] is null 错误
【解决方案2】:

我认为您需要将文件加载为:

Resource resource =
    this.ctx.getResource("classpath*:validation.xml");

所以我们假设它在类路径中的任何位置加载文件

【讨论】:

  • 我这样做并尝试调试器说该类为空,当我打印到控制台时:System.out.println("RESOURCE: "+resource.getFile().getAbsolutePath()); 错误:IOException: java.io.FileNotFoundException
猜你喜欢
  • 2012-05-18
  • 1970-01-01
  • 1970-01-01
  • 2016-12-10
  • 2013-02-20
  • 1970-01-01
  • 1970-01-01
  • 2012-02-24
  • 1970-01-01
相关资源
最近更新 更多