How to get a file as a resource

G

GIMME

The following lines exist in a JSP ...

String sRealPath = session.getServletContext().getRealPath("/"); /*1*/
String sXSLFile = sRealPath + "/xsl/xl.xsl" /*2*/
Reader r = new FileReader( new File(sXSLFile) ) ; /*3*/
StreamSource ss = new StreamSource(r); /*4*/

transformer = TransformerFactory.newInstance().newTransformer( ss ) ;

How would lines 1,2,3 and 4 be changed so that the xl.xsl file
is fetched as a resource instead of from the file system?

Thanks
 
J

Jacob

GIMME said:
The following lines exist in a JSP ...

String sRealPath = session.getServletContext().getRealPath("/"); /*1*/
String sXSLFile = sRealPath + "/xsl/xl.xsl" /*2*/
Reader r = new FileReader( new File(sXSLFile) ) ; /*3*/
StreamSource ss = new StreamSource(r); /*4*/

transformer = TransformerFactory.newInstance().newTransformer( ss ) ;

How would lines 1,2,3 and 4 be changed so that the xl.xsl file
is fetched as a resource instead of from the file system?


Something like:

String filePath = "/com/company/dir/xl.xsl";
InputStream stream = getClass().getResourceAsStream (filePath);
BufferedReader reader = new BufferedReader (new InputStreamReader (stream));
StreamSource source = new StreamSource (reader);

Don't forget the initial "/" in the path.
It is a common source of error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top