2011-09-03 5 views
1

나는 서버에서 JPG를 다운로드하는 Android 앱을 가지고 있으며 인터넷에 특정 파일이있을 때마다 "Down for Maintenance"이미지를 표시 할 수 있어야합니다. 예 : "yes.maint"또는 웹 서버의 다른 파일에 대한 전화 확인 - 존재하는 경우 다른 이미지 대신 유지 관리 이미지를 표시합니다. 없는 경우 다른 이미지를 정상적으로로드하십시오.Android : 다운로드 JPG if 문

Android에서 가능합니까?

감사

답변

0

을 수행해야합니다. 그런 다음 getStatusLine()을 호출하여 StatusLine getStatusCode()에서 상태 코드를 확인하고 가져옵니다. 찾지 못했다면, 404 번을 통해

0

나는 이것이이 더 신속하고 더러운 솔루션처럼 들리는이 같은 어떤 것을 구현하는 가장 좋은 방법이라고 생각 해달라고하지만 여기

어쨌든 수는 HTTP 다운로드에 대한 예제 클래스입니다 그것은 파일 "yes.maint"또는 다른 파일 중 일부는 HTTP를 통해 액세스 할 수 있습니다, 당신은 HTTPResponse를 얻기 위해 실행 HTTPClient를 사용하여 부를 수있는 가정 트릭

 
http download java get 
//------------------------------------------------------------// 
// JavaGetUrl.java:           // 
//------------------------------------------------------------// 
// A Java program that demonstrates a procedure that can be // 
// used to download the contents of a specified URL.   // 
//------------------------------------------------------------// 
// Code created by Developer's Daily       // 
// http://www.DevDaily.com         // 
//------------------------------------------------------------// 

import java.io.*; 
import java.net.*; 

public class JavaGetUrl { 

    public static void main (String[] args) { 

     //-----------------------------------------------------// 
     // Step 1: Start creating a few objects we'll need. 
     //-----------------------------------------------------// 

     URL u; 
     InputStream is = null; 
     DataInputStream dis; 
     String s; 

     try { 

     //------------------------------------------------------------// 
     // Step 2: Create the URL.         // 
     //------------------------------------------------------------// 
     // Note: Put your real URL here, or better yet, read it as a // 
     // command-line arg, or read it from a file.     // 
     //------------------------------------------------------------// 

     u = new URL("http://200.210.220.1:8080/index.html"); 

     //----------------------------------------------// 
     // Step 3: Open an input stream from the url. // 
     //----------------------------------------------// 

     is = u.openStream();   // throws an IOException 

     //-------------------------------------------------------------// 
     // Step 4:              // 
     //-------------------------------------------------------------// 
     // Convert the InputStream to a buffered DataInputStream.  // 
     // Buffering the stream makes the reading faster; the   // 
     // readLine() method of the DataInputStream makes the reading // 
     // easier.              // 
     //-------------------------------------------------------------// 

     dis = new DataInputStream(new BufferedInputStream(is)); 

     //------------------------------------------------------------// 
     // Step 5:             // 
     //------------------------------------------------------------// 
     // Now just read each record of the input stream, and print // 
     // it out. Note that it's assumed that this problem is run // 
     // from a command-line, not from an application or applet. // 
     //------------------------------------------------------------// 

     while ((s = dis.readLine()) != null) { 
      System.out.println(s); 
     } 

     } catch (MalformedURLException mue) { 

     System.out.println("Ouch - a MalformedURLException happened."); 
     mue.printStackTrace(); 
     System.exit(1); 

     } catch (IOException ioe) { 

     System.out.println("Oops- an IOException happened."); 
     ioe.printStackTrace(); 
     System.exit(1); 

     } finally { 

     //---------------------------------// 
     // Step 6: Close the InputStream // 
     //---------------------------------// 

     try { 
      is.close(); 
     } catch (IOException ioe) { 
      // just going to ignore this one 
     } 

     } // end of 'finally' clause 

    } // end of main 

} // end of class definition 
+0

을로드해야하는 이미지를 확인하거나 http://stackoverflow.com/questions/4596447/java-check-if-file을 확인하십시오. -exists-on-remote-server-its-url을 사용합니다. – sherif