Debian Java网络设置需分别配置系统网络接口和Java应用程序参数,具体步骤如下:
使用命令 ip addr 或 ifconfig 确认网络接口名称(如 eth0、wlan0)。
sudo nano /etc/netplan/01-netcfg.yamlsudo nano /etc/network/interfaces# Netplan配置(Debian 10+)
network:
version: 2
ethernets:
eth0:
dhcp4: no
addresses: [192.168.1.100/24]
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
或(旧版本):# interfaces配置
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8 8.8.4.4
sudo netplan applysudo systemctl restart networking使用 ping www.google.com 确认网络连通性。
在Java代码中通过 System.setProperty 配置代理或网络参数:
// 设置HTTP/HTTPS代理
System.setProperty("http.proxyHost", "proxy.example.com");
System.setProperty("http.proxyPort", "8080");
System.setProperty("https.proxyHost", "proxy.example.com");
System.setProperty("https.proxyPort", "8080");
Socket socket = new Socket("192.168.1.100", 8080);
URL 类访问网络资源。URL url = new URL("http://example.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
Java默认使用系统DNS配置(需确保 /etc/resolv.conf 正确),无需在代码中额外设置。
Netplan,旧版本使用 /etc/network/interfaces。dhcp 即可,无需手动指定IP。参考来源: