2013-02-11 2 views
4

이미지에 URL이 있습니다. 이 이미지는 각 요청 동안 업데이트됩니다 (즉, (동일한) URL에 대한 각 요청은 새 이미지를 반환합니다). 예,이 URL은 보안 문자를 가리 킵니다. 내 목표는 내 프로그램에 이러한 이미지를로드하고 표시하는 것입니다.Java (인터넷에서)의 ImageIcon 업데이트?

다음 코드 내 로컬 파일 시스템에 이러한 이미지를로드하고 확인 작업 (즉, 모든 이미지는 고유 다르다) :

String filePath; 
String urlPath; 
int numOfFilesToDownload; 

//Here filePath and urlPath are initialized. 
//filePath points to the directory, where to save images 
//urlPath is the url from where to download images 
//numOfFilesToDownload is the number of files to download 

for(int i = 0; i < numOfFilesToDownload; i++){ 
    //Initializing connection 
    URL url = new URL(urlPath); 
    HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

    //Downloading image 
    try(InputStream is = conn.getInputStream(); 
     FileOutputStream os = new FileOutputStream(filePath + "img" + i + ".jpg")){ 
     int b; 
     while((b = is.read()) != -1) 
      os.write(b); 
    } 
} 

그러나 이상한 일이, 일이 내가 다음 일을하려고하면

후자의 경우
for(int i = 0; i < numOfFilesToDownload; i++){ 
    //Initializing image from the url 
    URL url = new URL(urlPath); 
    javax.swing.ImageIcon ico = new javax.swing.ImageIcon(url); 

    //Showing the graphical dialog window with the image 
    javax.swing.JOptionPane.showMessageDialog(null, ico); 
} 

각 대화는 처음 반복 동안 다운로드 동일한 화상, 즉 하나를 포함한다.

또한 "? r ="을 urlPath (즉, 간단한 GET 요청 매개 변수)에 연결하면 URL이 여전히 유효 함을 보여줍니다. 그리고 다음 코드 (즉, 표시되는 각 이미지는 이전부터 다른입니다) 유효 표시하고있다 정확히 수행합니다

for(int i = 0; i < numOfFilesToDownload; i++){ 
    //Initializing image from the url 
    URL url = new URL(urlPath + "?r=" + i); 
    javax.swing.ImageIcon ico = new javax.swing.ImageIcon(url); 

    //Showing the graphical dialog window with the image 
    javax.swing.JOptionPane.showMessageDialog(null, ico); 
} 

은 그러므로 내가 결론을 할 수 있습니다, 그 이미지 아이콘은 어떻게 든 URL을 기억하고 그것을 처리하고 단순히 동일한 작업을 두 번 수행하는 것을 귀찮게하지 않습니다 ... 왜 그리고 어떻게합니까? Javadoc에는 아무것도 없다.

+0

코드가 스레딩 스레딩 규칙을 준수하지 않는 것으로 보입니다. SwingWorker를 사용하면 어떻게됩니까? 우리가 테스트 할 수 있도록 [sscce] (http://sscce.org)를 작성하고 게시하는 것을 고려하십시오. –

답변

2

코드 변형을 시도해 보았지만 정상적으로 작동했습니다. 내 SSCCE :

import java.awt.image.BufferedImage; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URL; 

import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JOptionPane; 
import javax.swing.SwingUtilities; 

public class TestUrls { 
    public static final String BASE_URL_PATH = "http://static.ed.edmunds-media.com/" + 
     "unversioned/adunit/homepage_showcase/"; 
    public static final String[] URL_PATHS = { 
     "honda-odyssey-2013.png", 
     "chevrolet-impala-2013.png", 
     "mazda-cx9-2013.png", 
     "toyota-rav4-2013-2.png" 
    }; 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      for (String urlPath : URL_PATHS) { 
       String fullUrlPath = BASE_URL_PATH + urlPath; 
       try { 
        URL url = new URL(fullUrlPath); 
        BufferedImage img = ImageIO.read(url); 
        ImageIcon icon = new ImageIcon(img); 
        JOptionPane.showMessageDialog(null, icon); 
       } catch (MalformedURLException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
     }); 
    } 
} 
+0

+1 for'ImageIO' – MadProgrammer

+0

고마워요. ImageIO가 저를 도왔습니다. – Angstrem

관련 문제