开发者

use a URL object to read a resource in web content

开发者 https://www.devze.com 2023-04-05 17:58 出处:网络
Im working in a web application using JSF2 and in one of my forms I create a PDF file that contains a image and a specific font. The ressorces are located in the WebContent directory.

Im working in a web application using JSF2 and in one of my forms I create a PDF file that contains a image and a specific font. The ressorces are located in the WebContent directory. My problem is when I want to create the pdf File I want to read the ressources and pass it in parameters to the method creating PDF FILE. I dont know how to use the URL object or any ot开发者_运维百科her way to do the task.

Thanks.


The ressorces are located in the WebContent directory.

WebContent directory is just for development environment under eclipse. When deployed the files there would go into the root of the web-app. So I take it that you want to access files in the root of your web-app.

You can use ServletContext.getResource(String path) with a path as "/myFileName" where the / signifies the web-app context root.

To get the reference to the ServletContext you may try this


The URL class has an openStream() method which gives you an InputStream of the resource's content:

URL resource = externalContext.getResource("/image.png");
InputStream input = resource.openStream();
// ...

You can also immediately get the resource as stream without getting it as URL first:

InputStream input = externalContext.getResourceAsStream("/image.png");
// ...

Having the InputStream, you can just read it into whatever target object which your PDF creator is expecting as input.

0

精彩评论

暂无评论...
验证码 换一张
取 消