Solved

how to check if an image exist in another application

Posted on 2006-07-19
11
1,102 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 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article shows the method of using the Resultant Set of Policy Tool to locate Group Policy that applies a particular setting.
Ransomware continues to grow in reach and sophistication, putting data everywhere at risk. Learn how to avoid being caught in its sinister clutches with these 11 key tips.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 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