简体   繁体   English

从WAR文件读取文件时出现问题

[英]Issue while reading a file from WAR file

I have a war file with the below structure. 我有一个具有以下结构的战争文件。

--js
  --sum.js
--WEB-INF
  --classes
    ---com
       -----test
           -----MyTest.class
--home.html

I am trying to read the js file in my MyTest.class file,But I am getting exception while reading it. 我正在尝试读取MyTest.class文件中的js文件,但是在读取时却出现异常。 I tried most of the solutions already mentioned in the stack. 我尝试了堆栈中已经提到的大多数解决方案。

I have tried 我努力了

1) 1)

String path = Thread.currentThread().getContextClassLoader().getResource("js/sum.js").getPath();
        File f = new File(path);
        System.out.println(f.getAbsolutePath());

First line is throwing nullpointer exception 第一行引发nullpointer异常

2) 2)

InputStream in =MyNashHornTest.class.getClassLoader().getResourceAsStream("/js/sum.js");
        BufferedReader br = new BufferedReader(new InputStreamReader(in));

Second line is throwing null pointer exception 第二行抛出空指针异常

3) 3)

InputStream in =MyNashHornTest.class.getClassLoader().getResourceAsStream("../../../../js/sum.js");
        BufferedReader br = new BufferedReader(new InputStreamReader(in));

Second line is throwing null pointer exception 第二行抛出空指针异常

Please help me to resolve this issue. 请帮助我解决此问题。

For war files, don't use the servlet container's classloader, but use the ServletContext instead. 对于war文件,请勿使用Servlet容器的类加载器,而应使用ServletContext。

This method allows servlet containers to make a resource available to a servlet from any location, without using a class loader. 此方法允许Servlet容器从任何位置使servlet可以使用资源,而无需使用类加载器。

ServletContext context = getServletContext();
InputStream is = context.getResourceAsStream("/yourfilename.txt");

It is recommended to keep it under the /WEB-INF directory if you don't want browers being able to access it. 如果您不希望浏览器访问它,建议将其保存在/ WEB-INF目录下。

The path must begin with a « / » and is interpreted as relative to the current context root. 该路径必须以« / »开头,并被解释为相对于当前上下文根。 This method returns null if no resource exists at the specified path. 如果指定路径上不存在任何资源,则此方法返回null。 For example ServletContext.getResourceAsStream(« WEB-INF/resources/yourfilename.cnf ») will return a nul exception, so be careful ! 例如ServletContext.getResourceAsStream(« WEB-INF/resources/yourfilename.cnf »)将返回nul异常,所以要小心!

Why null pointer comes?? 为什么空指针来了?

The path must begin with a "/" and is interpreted as relative to the current context root. 该路径必须以“ /”开头,并被解释为相对于当前上下文根。 This method returns null if no resource exists at the specified path. 如果指定路径上不存在任何资源,则此方法返回null。 For example, using a path that doesn't start with a slash, You will get a null return value. 例如,使用不以斜杠开头的路径,您将获得空返回值。

Details Description is given here: How to use ServletContext.getResourceAsStream(java.lang.String path)? 详细信息在这里给出: 如何使用ServletContext.getResourceAsStream(java.lang.String path)?

Resource Link: 资源链接:

HOW TO: Read a file from jar and war files (java and webapp archive) ? 如何:从jar和war文件(java和webapp存档)中读取文件?

You can get resources from the class path with ClassLoader.getResource or from the web root directory with ServletContext.getResource . 您可以使用ClassLoader.getResource从类路径获取资源,或者使用ServletContext.getResource从Web根目录获取资源。

In a war, you use the former to access ressources stored under WEB-INF/classes , or in jars under WEB-INF/lib , and the former for what lies directly at the root of the web application. 在战争中,您可以使用前者访问存储在WEB-INF/classes下或WEB-INF/lib下的WEB-INF/lib ,而前者则用于直接位于Web应用程序根目录下的资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM