import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.Scanner; public class Recurs { // Funció que valida si un recurs està en format GIF boolean isGifFormat(URL url) { boolean ret = false; try { URLConnection con = url.openConnection(); String headerType = con.getContentType(); String guessType = con.guessContentTypeFromName(url. getFile()); ret = headerType.endsWith("gif") || guessType.endsWith("gif"); } catch (IOException ex) { System.err.println(ex); } return ret; } // Funció que descarrega i mostra per pantalla el contingut d'un recurs private void printContent(URL url) { InputStream in; char[] cbuf = new char[512]; int caractersLlegits; if (!isGifFormat(url)) { return; } try { in = url.openStream(); InputStreamReader inr = new InputStreamReader(in); while ((caractersLlegits = inr.read(cbuf)) != -1) { String str = String.copyValueOf(cbuf, 0, caractersLlegits); System.out.print(str); } System.out.println(); } catch (IOException ex) { System.err.println(ex); } } }