Skip to content

Copy Image From URL To File

Just saving this away for a rainy day…something I concocted in response to a question on the Groovy mailing list:

new File("local.gif").withOutputStream { os ->
  new URL("remote.gif").withInputStream { is ->
    os << is
  }
}

The important thing to note here is the way the streams are carefully ’scoped’ and maintained so that no leaks can occur even if exceptions or other sticky situations arise.

Tags:

Java Enterprise Edition, JEE, JavaServer Pages, JSP, Tag Libraries, Servlets, Enterprise Java Beans, EJB, Java Messaging Service JMS, BEA Weblogic, JBoss, Application Servers, Spring Framework, Groovy, Grails, Griffon, Seam, Open Source, Service Oriented Architectures, SOA, Java 2 Standard Edition, J2SE