Ubuntu 中 Java 网络配置实用指南
一 前置检查
二 代理配置
java \
-Dhttp.proxyHost=proxy.example.com \
-Dhttp.proxyPort=8080 \
-Dhttps.proxyHost=proxy.example.com \
-Dhttps.proxyPort=8080 \
-Dhttp.nonProxyHosts=localhost|127.0.0.1 \
-jar your-application.jar
System.setProperty("http.proxyHost", "proxy.example.com");
System.setProperty("http.proxyPort", "8080");
System.setProperty("https.proxyHost", "proxy.example.com");
System.setProperty("https.proxyPort", "8080");
System.setProperty("http.nonProxyHosts", "localhost|127.0.0.1");
System.setProperty("http.proxyUser", "username");
System.setProperty("http.proxyPassword", "password");
export http_proxy=http://proxy.example.com:8080
export https_proxy=http://proxy.example.com:8080
export no_proxy=localhost,127.0.0.1
http_proxy=http://proxy.example.com:8080
https_proxy=http://proxy.example.com:8080
no_proxy=localhost,127.0.0.1
三 主机网络与防火墙
sudo ufw allow out to any port 80
sudo ufw allow out to any port 443
sudo ufw allow 8080/tcp
四 验证与排错
import java.net.InetAddress;
public class NetTest {
public static void main(String[] args) throws Exception {
InetAddress a = InetAddress.getByName("www.google.com");
System.out.println("OK: " + a.getHostAddress());
}
}
nc -vz 127.0.0.1 8080 # 应用端口
curl -I https://www.google.com