Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. public static Image loadImage(String offline, String online) {
  2.         Image image = null;
  3.         File file = new File(offline);
  4.         if (!file.exists()) {
  5.             URL url = null;
  6.             try {
  7.                 url = new URL(online);
  8.             } catch (MalformedURLException e) {
  9.                 e.printStackTrace();
  10.             }
  11.             InputStream is = null;
  12.             try {
  13.                 is = url.openStream();
  14.             } catch (IOException e) {
  15.                 e.printStackTrace();
  16.             }
  17.             OutputStream os = null;
  18.             try {
  19.                 os = new FileOutputStream(file);
  20.             } catch (FileNotFoundException e) {
  21.                 e.printStackTrace();
  22.             }
  23.  
  24.             byte[] b = new byte[2048];
  25.             int length;
  26.             try {
  27.                 while ((length = is.read(b)) != -1) {
  28.                     try {
  29.                         os.write(b, 0, length);
  30.                     } catch (IOException e) {
  31.                         e.printStackTrace();
  32.                     }
  33.                 }
  34.             } catch (IOException e) {
  35.                 e.printStackTrace();
  36.             }
  37.             try {
  38.                 is.close();
  39.             } catch (IOException e) {
  40.                 e.printStackTrace();
  41.             }
  42.             try {
  43.                 os.close();
  44.             } catch (IOException e) {
  45.                 e.printStackTrace();
  46.             }
  47.         }
  48.         try {
  49.             image = ImageIO.read(file);
  50.         } catch (IOException e) {
  51.             e.printStackTrace();
  52.         }
  53.         return image;
  54.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement