JavaEE - WAR - 已部署 :: 如何从资源目录读取文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23813669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:21:46  来源:igfitidea点击:

JavaEE - WAR - Deployed :: How to read a file from resource directory

javamavenjakarta-eeglassfishwar

提问by Jeef

I believe the "correct" way one is supposed to do this is getResourceAsStream()but it just doesn't seem to cut it.

我相信人们应该这样做的“正确”方式是,getResourceAsStream()但它似乎并没有削减它。

My situation:

我的情况:

I have a web application in Java that will be eventually deployed to a few different web servers the possibilities are GlassFish, Tomcat, and WildFly. Currently I'm working on a mac with Netbeans and I have access to WildFlyand GlassFish. The project is mavenized

我有一个 Java 网络应用程序,最终将部署到几个不同的网络服务器上,可能是GlassFishTomcatWildFly。目前我正在使用 Netbeans 开发 mac,我可以访问WildFlyGlassFish。该项目是mavenized

What I need to do is read and write two different Excel files. One serves as a "template" and one serves as the output file. The application previously ran standalone and I'm trying to wrap it into a JSF web site.

我需要做的是读取和写入两个不同的 Excel 文件。一个用作“模板”,一个用作输出文件。该应用程序以前是独立运行的,我正在尝试将它包装到一个 JSF 网站中。

I need to obtain an FileInputStreamfor my one of the methods and eventually create an output file which will be served back to the user via http.

我需要FileInputStream为我的方法之一获取一个,并最终创建一个输出文件,该文件将通过 http 返回给用户。

From what I gather the appropriate way to do this is supposed to be:

从我收集到的适当方法应该是:

XLSX File

XLSX 文件

/src/main/resources/ANALYSIS_template.xlsx

Location once compiled into WAR file :: WildFly

位置一旦被编译成 WAR 文件 :: WildFly

/usr/local/opt/wildfly-as/libexec/standalone/deployments/fleetForecast-1.0-SNAPSHOT.war/WEB-INF/classes/ANALYSIS_template.xlsx

Location once compiled into WAR file :: GlassFish

编译成 WAR 文件的位置 :: GlassFish

~/devel/fleetforecast/target/fleetForecast-1.0-SNAPSHOT/WEB-INF/classes/ANALYSIS_template.xlsx

Java Code

Java代码

I think I've maybe managed to get an input stream to the file with the command:

我想我可能已经设法使用以下命令获取文件的输入流:

InputStream is1 = getClass().getResourceAsStream("ANALYSIS_template.xlsx");

I've also tried ("/ANALYSIS_template.xlsx")by the way which behaves similarly. When I try to read from this input stream

我也尝试过("/ANALYSIS_template.xlsx")行为类似的方式。当我尝试从此输入流中读取时

try {
        is1.read();
    } catch (IOException ex) {
        Logger.getLogger(FleetBean.class.getName()).log(Level.SEVERE, null, ex);
    }

I get a java.lang.NullPointerException.

我得到一个java.lang.NullPointerException.

Caused by: java.lang.NullPointerException
    at org.mitre.fleetforecast.bean.FleetBean.<init>(FleetBean.java:57)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at java.lang.Class.newInstance(Class.java:433)
    at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:186)
    ... 76 more

Any assistance offered or thought of would be greatly appreciated.

任何提供或想到的帮助将不胜感激。

采纳答案by Jeef

In the end it looks like this is the code that did the trick. I have NO idea why but apparently it worked and I'm not complaining.

最后看起来这就是成功的代码。我不知道为什么,但显然它有效,我没有抱怨。

InputStream inputStream = getClass().getClassLoader()
                         .getResourceAsStream("/ANALYSIS_template.xlsx");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

I made a new project form scratch and this code worked. Thanks for the help guys!

我从头开始创建了一个新项目,此代码有效。感谢您的帮助!

回答by Jeef

What about something like this :

这样的事情怎么样:

InputStream is1 = URLClassLoader.getSystemResourceAsStream("/classes/ANALYSIS_template.xlsx");

or

或者

InputStream is1 = this.getClass().getResourceAsStream("/classes/ANALYSIS_template.xlsx"));

Edit :

编辑 :

Use this when you are accessing the file from a servlet container

当您从 servlet 容器访问文件时使用它

InputStream is1 = ServletContext.getResourceAsStream("/WEB-INF/classes/ANALYSIS_template.xlsx") ;

回答by Ryan D

Is this potentially an issue with the relative path to the file vs the absolute path to the file? Can you attempt to use FileInputStream and use the absolute path? If that works then you may just have to tinker with the relative path?

这可能是文件的相对路径与文件的绝对路径的问题吗?您可以尝试使用 FileInputStream 并使用绝对路径吗?如果可行,那么您可能只需要修改相对路径?

回答by lidox

If you want to get the java File object, you can try this:

如果你想得到java File 对象,你可以试试这个:

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