Java StreamSource from file in web application project

2.2k views Asked by At
                TransformerFactory factory = TransformerFactory.newInstance();    
                Source stylesheetSource = new StreamSource(new File("stylesheet.xsl").getAbsoluteFile());
                Transformer transformer = factory.newTransformer(stylesheetSource);
                Writer strWriter = new StringWriter();
                StreamResult result = new StreamResult(strWriter);
                transformer.transform(source, result);

This looks for the stylesheet file from C://stylesheet.xsl but how do I get it to point to a file in my project for example /src/main/java/util/stylesheet.xsl

I've tried

                InputStream is = getClass().getResourceAsStream("/main/webapp/stlesheets/stylesheet.xsl"); 
                Source stylesheetSource = new StreamSource(is);
                ...

but I get these exceptions

ERROR:  'Could not compile stylesheet' 
FATAL ERROR:  'java.net.MalformedURLException' 
:null

EDIT:

I've tracked the ServletContext and it gives the correct context path /project_name so the error is coming from the getResourceAsStream function

The file path is /project_name/src/main/resources/stylesheet.xsl The servletcontext path is /project_name I'm using servletContext.getResourceAsStream("/src/main/resources/stylesheet.xsl") Still getting the error

                ServletContext sc = req.getSession().getServletContext();
                InputStream is = sc.getResourceAsStream("/src/main/resources/stylesheet.xsl");
0

There are 0 answers