use of org.pdown.core.proxy.ProxyConfig in project proxyee-down by monkeyWie.
the class AppUtil method download.
/**
* 下载http资源
*/
public static void download(String url, String path) throws IOException {
URL u = new URL(url);
HttpURLConnection connection;
ProxyConfig proxyConfig = PDownConfigContent.getInstance().get().getProxyConfig();
if (proxyConfig != null) {
Type type;
if (proxyConfig.getProxyType() == ProxyType.HTTP) {
type = Type.HTTP;
} else {
type = Type.SOCKS;
}
if (!StringUtils.isEmpty(proxyConfig.getUser())) {
Authenticator authenticator = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(proxyConfig.getUser(), proxyConfig.getPwd() == null ? null : proxyConfig.getPwd().toCharArray());
}
};
Authenticator.setDefault(authenticator);
}
Proxy proxy = new Proxy(type, new InetSocketAddress(proxyConfig.getHost(), proxyConfig.getPort()));
connection = (HttpURLConnection) u.openConnection(proxy);
} else {
connection = (HttpURLConnection) u.openConnection();
}
connection.setConnectTimeout(30000);
connection.setReadTimeout(0);
File file = new File(path);
if (!file.exists() || file.isDirectory()) {
FileUtil.createFileSmart(file.getPath());
}
try (InputStream input = connection.getInputStream();
FileOutputStream output = new FileOutputStream(file)) {
byte[] bts = new byte[8192];
int len;
while ((len = input.read(bts)) != -1) {
output.write(bts, 0, len);
}
}
}
Aggregations