cancel
Showing results for 
Search instead for 
Did you mean: 

File Resource

Former Member
0 Kudos

I have added a PDF to my web project and now I want to access this within a servlet/jsp with a file reference. Like new File("webproject/webContent/pdfs/mine.pdf");. Is this possible?

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Nathan, you can read a .pdf file but what you will see is something like that:

"%PDF-1.4

%âãÏÓ

962 0 obj+­µk#Ù»Òêb[ B–1)n,G6 -Ie#Û8ÁÁÆ–?¡´ 9Ä&q( Ѧ?l\u00A2¤¥1!†„Kë&! Jƒ“´ôFˆ éL‡á? 3?I i3}Hé é%íL_zþ¯’Öm úØ?‘µÿ¹Ÿïv- ..."

Former Member
0 Kudos

The path for your file object can be obtained via the different path methods in the class

ServletContext

which can be obtained from class

ServletConfig

on initialization of your servlet.

Regards,

Dieter

Former Member
0 Kudos

Karsten,

I am not getting this type of problem. What you are showing looks something like a character change, or if you are not setting the return type. It is thinking text/html, when you want application/pdf.

Nathan

Former Member
0 Kudos

Dieter,

Thanks, I noticed this late last night.

Former Member
0 Kudos

Dieter,

I was trying to reward you the points, but everytime I do, there is an error.

I will try again later.

Nathan.

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi Nathan,

Usually you can do it but not with new File(…). You can do it via the getResourceAsStream from your class loader object, which you can access through your class or through you thread.

For example :

ClassName.class.getResourceAsStream(…) or this.getClass().getResourceAsStream(…) or Thread. currentThread().getContextClassLoader().().getResourceAsStream(…)

Regards,

Avi