Proxy経由でURLからコンテンツを取得するとき

URLを指定してHTMLなどのファイルを取得するには以下のコードでできます。

    public void downloadFile(URL url, File file) throws IOException {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = url.openStream();
            out = new FileOutputStream(file);
            byte[] buffer = new byte[4096];
            int bytes_read;
            while((bytes_read  = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytes_read);
            }
        } finally {
            try { in.close(); out.close(); } catch(Exception e) {}
        }
    }

しかし、Proxyを経由する場合は、VMに引数を渡す必要があります。

-DproxySet=true -DproxyHost=proxy1.foo.co.jp -DproxyPort=8089