import java.io.FileOutputStream; import java.io.InputStream; import java.net.Authenticator; import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.PasswordAuthentication; import java.net.Proxy; import java.net.SocketAddress; import java.net.URL; class ProxyAuthenticator extends Authenticator { private String userName, password; public ProxyAuthenticator(String userName, String password) { this.userName = userName; this.password = password; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password.toCharArray()); } } public class Main { public static void main(String[] args) throws Exception { downloadFromUrl("http://hgci.cn/style/download.png"); } private static String getOutputFileName(URL url) { String[] urlParts = url.getPath().split("/"); return "c:/temp/" + urlParts[urlParts.length - 1]; } private static void downloadFromUrl(String urlString) throws Exception { InputStream is = null; FileOutputStream fos = null; URL url = new URL(urlString); System.out.println("Reading..." + url); Authenticator.setDefault(new ProxyAuthenticator("username", "password")); SocketAddress addr = new InetSocketAddress("your proxyserver ip address", 80); Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy); is = conn.getInputStream(); String filename = getOutputFileName(url); fos = new FileOutputStream(filename); byte[] readData = new byte[1024]; int i = is.read(readData); while (i != -1) { fos.write(readData, 0, i); i = is.read(readData); } System.out.println("Created file: " + filename); System.out.println("Completed"); } }