Proxy サーバを指定する場合は System.setProperty で各プロパティ値を設定する
Example: java_net_03.java
// データを取得する
import java.net.*;
import java.io.*;
public class java_net_03 {
public static void main(String[] args) {
try {
// Proxy の設定
System.setProperty("proxySet", "true");
System.setProperty("proxyHost", "myproxy.mydomain");
System.setProperty("proxyPort", "8080");
URL url = new URL(args[0]);
FileOutputStream out = new FileOutputStream(args[1]);
InputStream input = url.openStream();
while (true) {
int read = input.read();
if (read == -1) {
break;
}
out.write(read);
}
out.close();
}
catch (Exception e) {
System.err.println(e);
return;
}
}
}
実行結果
> javac java_net_03.java > java java_net_03 http://www.google.co.jp/intl/ja_jp/images/logo.gif logo.gif実行したディレクトリに logo.gif というファイルが生成される
0 件のコメント:
コメントを投稿