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

I believe that the “right” way that should do is getResourceAsStream(), but it just doesn't cut it.

My situation:

I have a Java web application that will eventually be deployed to several different web servers. GlassFish , Tomcat, and WildFly >. I am currently working on a mac with Netbeans, and I have access to WildFly and GlassFish . The project is stunned.

What I need to do is read and write two different Excel files. One of them serves as a "template", and one serves as an output file. The application previously ran offline, and I'm trying to turn it into a JSF website.

I need to get FileInputStreamfor my one of the methods and ultimately create an output file that will be sent to the user via http.

From what I'm compiling, there should be a suitable way to do this:

Xlsx file

/src/main/resources/ANALYSIS_template.xlsx

Location after compiling to WAR file :: WildFly

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

Location once compiled into a WAR file :: GlassFish

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

Java code

It seems to me that I may have managed to get the input stream to a file using the command:

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

I also tried ("/ANALYSIS_template.xlsx")to behave similarly. When I try to read from this input stream

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

I get it 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

, , .

+4
4

, , , . , , , , , .

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

, . !

+6

- :

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

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

:

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

Java , :

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

Is this potentially a problem with the relative path to the file versus the absolute path to the file? Can you try to use FileInputStream and use an absolute path? If this works, you may need to get over the relative path?

0
source

Source: https://habr.com/ru/post/1541656/


All Articles