Skip to content

Instantly share code, notes, and snippets.

@fmamud
Last active November 9, 2015 17:41
Show Gist options
  • Save fmamud/7c67448b10c80e0926b7 to your computer and use it in GitHub Desktop.
Save fmamud/7c67448b10c80e0926b7 to your computer and use it in GitHub Desktop.
Retrieve an image with a connection via proxy and uses the Java API NIO.2 to save the inputstream in a file on disk.
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Paths;
public class HttpFileGet {
public static void main(String[] args) throws Exception {
Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress("127.0.0.1",8888));
URLConnection conn = new URL("http://flupy.org/data/flags/br/br.gif")
.openConnection(proxy);
try (InputStream is = conn.getInputStream()) {
Files.copy(is, Paths.get("/temp/br.gif"));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment