Tester image existe sur un serveur web [java]

Résolu
Bonjour,
Ben tout est dans le titre je voudrai tester si une image existe sur un serveur (http://balabalka.com/monimage.png)
en java, Merci de m'aider :)



--
Voila ma signature ^^

3 réponses

  1. Voila pour ceux, qui comme moi veulent tester su un fichier existe sur un serveur Web
    voici mon code :

    import java.net.*;

    public class main {

    public static void main(String[] args) throws Exception {

    URL url = new URL("http://site.com/fichier.png");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    // // Gets the status code from an HTTP response message.
    int jh = connection.getResponseCode();
    int fh = 200;

    if(jh == fh)
    {

    System.out.println(" fichier existe code est: " + jh);
    }
    else
    {
    System.out.println(" fichier introuvable " + jh);
    }
    }
    }

    Si la reponsecode est 200, le fichier existe, si la reponsecode est 404, c'est que le fichier n'existe pas Voila :)
    1. Alors, j'ai essayiez, ça pourrait fonctionner, mais je n'arrive pas a récupérez
      la Field value

      import java.io.IOException;
      import java.io.OutputStream;
      import java.net.*;

      public class main{
      public static void main ( String[] args ) throws IOException {

      try
      {
      URL url = new URL("Url-a-tester.com");
      URLConnection conn = url.openConnection();
      for (int i=0; ; i++)
      {
      String name = conn.getHeaderFieldKey(i);
      String value = conn.getHeaderField(i);

      if (name == null && value == null){

      break;

      }
      if (name == null){

      System.out.println(value);

      }
      else{

      }
      if(value == "200")
      {
      System.out.println("le fichier existe");
      }

      }
      }
      catch (Exception e) {

      }
      }

      }

      Voila, une idée ?
      Voila ma signature ^^
      1. Modérateur
        Essaye d'examiner les en-têtes:
        johand@osiris: ~/src/CCM/java $ cat GetHttpHeaders.java 
        import java.net.*; 
        
        public class GetHttpHeaders{ 
            public static void main ( String[] args ) throws IOException { 
         if (args.length == 0 ) 
             { 
          System.err.println("Usage:  GetHttpHeaders url"); 
          System.exit(1); 
             } 
               
         try  
             { 
          URL url = new URL(args[0]); 
          URLConnection conn = url.openConnection(); 
          for (int i=0; ; i++)  
              { 
           String name = conn.getHeaderFieldKey(i); 
           String value = conn.getHeaderField(i); 
           if (name == null && value == null){ 
               break;  
           } 
           if (name == null){ 
               System.out.println("Server HTTP version, Response code:"); 
               System.out.println(value); 
               System.out.print("\n"); 
           } 
           else{ 
               System.out.println(name + "=" + value); 
           } 
              } 
             }  
         catch (Exception e) {} 
            } 
        } 
        
        johand@osiris: ~/src/CCM/java $ java GetHttpHeaders http://static.ccm2.net/www.commentcamarche.net/_skin/_local/img/logo.png?201007091112 
        http://static.ccm2.net/www.commentcamarche.net/_skin/_local/img/logo.png?201007091112 
        Server HTTP version, Response code: 
        HTTP/1.1 200 OK 
        
        Content-Type=image/png 
        ETag="-1489846074" 
        Accept-Ranges=bytes 
        Last-Modified=Fri, 09 Jul 2010 09:37:19 GMT 
        Content-Length=2909 
        Server=lighttpd/1.4.13 
        Cache-Control=max-age=31536000 
        Date=Tue, 27 Dec 2011 05:57:57 GMT 
        Connection=keep-alive


        Gates gave ^H sold you the windows.
        GNU gave us the whole house.(Alexandrin)