Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to check if an image exist in another application

Posted on 2006-07-19
11
Medium Priority
?
1,110 Views
Last Modified: 2011-10-03
I am trying to check if an image exists inanother application, if yes then show it otherwise don't show it.
I tried the below code but it is not working. It jst shows no image for all.

Any help will be greatly appreciated

<%
      file = new File("http://www.ccim.com/services/member/profile/pictures/"+next_record.getMember_ID()+".jpg");
      out.print(file);
      if(file.exists()){
%>
           <td nowrap width = '33%' align = 'right'>
                   <img src ="http://www.ccim.com/services/member/profile/pictures/<%=next_record.getMember_ID()%>.jpg" width="52" height="75">
           </td>
<%
       }else{
%>
                                                             
             <td nowrap width = '33%' align = 'right'>
           </td>
                                                            
<%
       }
%>
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
when I print the file it prints this out.print(file)
http:\www.ccim.com\services\member\profile\pictures\08914062.jpg (this is an image)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
http:\www.ccim.com\services\member\profile\pictures\00008477.jpg 
http:\www.ccim.com\services\member\profile\pictures\04688171.jpg 
http:\www.ccim.com\services\member\profile\pictures\06264205.jpg 
http:\www.ccim.com\services\member\profile\pictures\08901225.jpg 
http:\www.ccim.com\services\member\profile\pictures\08914062.jpg
0
Comment
Question by:huzefaq
  • 5
  • 5
11 Comments
 
LVL 12

Accepted Solution

by:
enachemc earned 1400 total points
ID: 17138825
Try to Open a HTTP connection to that file. If you receive a 404 status response, it means that the image does not exist.
0
 
LVL 19

Assisted Solution

by:Kuldeepchaturvedi
Kuldeepchaturvedi earned 600 total points
ID: 17138967
instead of doing
file = new File("http://www.ccim.com/services/member/profile/pictures/"+next_record.getMember_ID()+".jpg");

you should do..

URL img = new URL("http://www.ccim.com/services/member/profile/pictures/"+next_record.getMember_ID()+".jpg");
try{
HttpURLConnection con = img.getConnection();
if(con.getResponseCode!=HttpURLConnection.HTTP_NOT_FOUND)
//image exist
else
//image does not exist
}
catch(IOException e)
{
//image does not exist
}

P.S. Please note that it is just an elaboration of what enachemc is saying in previous post.
0
 

Author Comment

by:huzefaq
ID: 17139356
Thanks guys but I am getting an error when i try this code

this is what I tried
----------------------------------------------------------------------------------------------------
<%@ page import="java.net.URL" %>
.
.
.

<%
URL img = new URL("http://www.ccim.com/services/member/profile/pictures/"+next_record.getMember_ID()+".jpg");
      try{
            HttpURLConnection con = img.getConnection();
            if(con.getResponseCode!=HttpURLConnection.HTTP_NOT_FOUND){ %>
                  <td nowrap width = '33%' align = 'right'>
                        <img src ="http://www.ccim.com/services/member/profile/pictures/<%=next_record.getMember_ID()%>.jpg" width="52" height="75">
                  </td>
            <%}else{ %>
                  <td nowrap width = '33%' align = 'right'></td>
            <% }
        }
      catch(IOException e)
      { %>
              <td nowrap width = '33%' align = 'right'></td>  
       <% }%>

------------------------------------------------------------------------------------------------------------------------------------------------------------------
I get this error
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Unable to compile class for JSP

An error occurred at line: 127 in the jsp file: /templates_public/member_search_results.jsp
Generated servlet error:
HttpURLConnection cannot be resolved to a type

An error occurred at line: 127 in the jsp file: /templates_public/member_search_results.jsp
Generated servlet error:
The method getConnection() is undefined for the type URL

An error occurred at line: 127 in the jsp file: /templates_public/member_search_results.jsp
Generated servlet error:
HttpURLConnection.HTTP_NOT_FOUND cannot be resolved to a type

An error occurred at line: 139 in the jsp file: /templates_public/member_search_results.jsp
Generated servlet error:
IOException cannot be resolved to a type

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:enachemc
ID: 17139410
it's java.net.HttpURLConnection.
0
 

Author Comment

by:huzefaq
ID: 17139444
I changed the import to

<%@ page import="java.net.*" %>

but I still get these error

--------------------------------------------------
org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 127 in the jsp file: /templates_public/member_search_results.jsp
Generated servlet error:
The method getConnection() is undefined for the type URL

An error occurred at line: 127 in the jsp file: /templates_public/member_search_results.jsp
Generated servlet error:
con.getResponseCode cannot be resolved or is not a field

An error occurred at line: 139 in the jsp file: /templates_public/member_search_results.jsp
Generated servlet error:
IOException cannot be resolved to a type

0
 
LVL 12

Expert Comment

by:enachemc
ID: 17139493
HttpURLConnection con = new HttpURLConnection(img);
0
 

Author Comment

by:huzefaq
ID: 17139719
Thanks, I changed the line but it still gives error

---------------------------------------------------------------------------------------------------------
org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 127 in the jsp file: /templates_public/member_search_results.jsp
Generated servlet error:
Cannot instantiate the type HttpURLConnection

An error occurred at line: 127 in the jsp file: /templates_public/member_search_results.jsp
Generated servlet error:
con.getResponseCode cannot be resolved or is not a field

An error occurred at line: 139 in the jsp file: /templates_public/member_search_results.jsp
Generated servlet error:
IOException cannot be resolved to a type

-----------------------------------------------------------------------------------------------------------
0
 
LVL 12

Expert Comment

by:enachemc
ID: 17139767
sorry about that. Use :

HttpURLConnection con = (HttpURLConnection )img.openConnection();
0
 

Author Comment

by:huzefaq
ID: 17139866
no pro.

but it still gives this error. It looks like I need to import a java class for IOException. Would you know which one to import. thanks

-------------------------------------------------------------
org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 139 in the jsp file: /templates_public/member_search_results.jsp
Generated servlet error:
IOException cannot be resolved to a type



0
 
LVL 12

Expert Comment

by:enachemc
ID: 17139892
java.io.IOException
0
 

Author Comment

by:huzefaq
ID: 17140006
it works.  Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Your business may be under attack from a silent enemy that is hard to detect. It works stealthily in the shadows to access and exploit your critical business information, sensitive confidential data and intellectual property, for commercial gain. T…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question