cancel
Showing results for 
Search instead for 
Did you mean: 

Accessing image from dist/imgaes folder inside JSPDynpage Component

Amey-Mogare
Contributor
0 Kudos

Hi,

I have a sceanario where I want to access JPG image stored in dist/images folder inside JSPDynpage Component java file.

It is ok if I get it in the form of a byte array (byte[]).

Any idea how to achieve it?

Please help.

Thanks and regards,

Amey Mogare

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

>

> Hi,

>

> I have a sceanario where I want to access JPG image stored in dist/images folder inside JSPDynpage Component java file.

> It is ok if I get it in the form of a byte array (byte[]).

>

> Any idea how to achieve it?

>

> Please help.

>

> Thanks and regards,

> Amey Mogare

Hi Arney,

you can get the Path to it by using the following code:


//Suppose that request is an object of IPortalComponentRequest
IResource res = request.getResource(IResource.IMAGE, "images/myImage.jpg");		  
String url = res.getResourceInformation().getURL(request));	 

When you have the url you can either just display it in HTML via the img-Tag or you can also get a byte array by using standard Java classes/methods.

Amey-Mogare
Contributor
0 Kudos

Hi Clemens,

Thank you for valuable reply.

I tried your code and now I am getting following exception


java.io.FileNotFoundException: /irj/portalapps/Ameymypay/dist/images/draft.jpg (No such file or directory (errno:2))

This draft.jpg image is in dist\image folder in my PAR file named Ameymypay.

Thanks and regards,

Amey Mogare

Former Member
0 Kudos

>

> Hi Clemens,

>

> Thank you for valuable reply.

>

> I tried your code and now I am getting following exception

>

>


> java.io.FileNotFoundException: /irj/portalapps/Ameymypay/dist/images/draft.jpg (No such file or directory (errno:2))
> 

>

> This draft.jpg image is in dist\image folder in my PAR file named Ameymypay.

>

> Thanks and regards,

> Amey Mogare

Hi Arney,

the above code works for my components... But I have two suggestions which you could try:

- I suppose that you have created an IView for your application. Check the isolation mode of this IView in PCD. If it is set to "URL isolated" then set it to "Embedded". With this setting it should work.

- If this is not possible (e.g. you want to keep the isolation mode as URL isolated or for any other reason) then just prefix the URL with your server host name like this:


//...
String myServerPrefix = "http://localhost:50000"; //suppose that this is your server
String url = res.getResourceInformation().getURL(request));	
url = myServerPrefix + url; 

This should also work. Of course you can also dynamically get your server name (or get it via portalapp configuration). But maybe you try the hard coding at first.

Amey-Mogare
Contributor
0 Kudos

Hi Clemens,

Thank you. It worked.

For my surprise, Isolation mode of iView is Embedded, but still it wasn't working for me!

I tried your code and it worked!


  	IResource res = request.getResource(IResource.IMAGE, "images/draft.jpg");		  
	String myServerPrefix = "http://mytest.com"; 
	String url = myServerPrefix + res.getResourceInformation().getURL(request);	

Now, can you tell me how to I get rid of this hard-coding server URL?

Thanks and regards,

Amey Mogare

Former Member
0 Kudos

>

> Hi Clemens,

>

> Thank you. It worked.

>

> For my surprise, Isolation mode of iView is Embedded, but still it wasn't working for me!

>

> I tried your code and it worked!

>


>   	IResource res = request.getResource(IResource.IMAGE, "images/draft.jpg");		  
> 	String myServerPrefix = "http://mytest.com"; 
> 	String url = myServerPrefix + res.getResourceInformation().getURL(request);	
> 

>

> Now, can you tell me how to I get rid of this hard-coding server URL?

>

> Thanks and regards,

> Amey Mogare

Hi Arney,

maybe you should first also give the suggestion of ram another chance, because you can also use it in your class code:

String url = request.getWebResourcePath() + "/images/draft.jpg";

Otherwise to avoid the hardcoding you can either perform configuration in your portalapp.xml with the server prefix and read it out, or write a little method that retrieves your server name. This is however not as trivial as it might look like. I personally have a method like follows:


public String getBasePortalUrl(IPortalComponentRequest request) {
     StringBuffer requestURL = request.getServletRequest().getRequestURL();
     //Parse the server name,port etc. out of this request url, depending on your system environment
     //Normally you parse till you get to /irj
}

Amey-Mogare
Contributor
0 Kudos

Hi Clemens,

I tried Ram's suggestion but it gives


 java.io.FileNotFoundException: /irj/portalapps/Ameymypay/images/draft.jpg (No such file or directory (errno:2))

I have written method as suggested by you and its working fine now.


public java.lang.String getBasePortalUrl(IPortalComponentRequest iPortalCompRequest) {
			
	String requestURL = iPortalCompRequest.getServletRequest().getRequestURL().toString();
			
	int indexOfIrj = requestURL.indexOf("/irj");
	
        return requestURL.substring(0, indexOfIrj);
}

Thank you for help.

Thanks and regards,

Amey Mogare

Answers (1)

Answers (1)

former_member189631
Active Contributor
0 Kudos

Amey,

Here is the code to access a jpg image stored in the folder PORTAL-INF -> dist -> Images


<%=componentRequest.getWebResourcePath()%>/images/image_file.jpg%>

Ram

Amey-Mogare
Contributor
0 Kudos

Hi Ram,

I want to access it inside my Component Java class, not in jsp file.

Thanks and regards,

Amey Mogare