Solved

how to check if an image exist in another application

Posted on 2006-07-19
11
1,103 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
11 Comments
 
LVL 12

Accepted Solution

by:
enachemc earned 350 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 150 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
Independent Software Vendors: 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
issue with pdf generation 2 98
build fail in maven project 11 212
mask rule in struts validation.xml 5 104
DTD and JAVA versions 1 55
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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