Solved

how to check if an image exist in another application

Posted on 2006-07-19
11
1,098 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
What is Backup? Backup software creates one or more copies of the data on your digital devices in case your original data is lost or damaged. Different backup solutions protect different kinds of data and different combinations of devices. For e…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now