java编程,通过代理服务器访问外网的FTP

有些时候我们的网络不能直接连接到外网, 需要使用http或是https或是socket代理来连接到外网, 这里是java使用代理连接到外网的一些方法, 希望对你的程序有用.
方法一:使用系统属性来完成代理设置, 这种方法比较简单, 但是不能对单独的连接来设置代理:
/**
*@paramargs
*/
/**
*@paramargs
*/
public static void main(String[] args) {
Properties prop = System.getProperties();
// 设置http访问要使用的代理服务器的地址
prop.setProperty("http.proxyHost", "192.168.0.254");
// 设置http访问要使用的代理服务器的端口
prop.setProperty("http.proxyPort", "8080");
// 设置不需要通过代理服务器访问的主机,可以使用*通配符,多个地址用|分隔
prop.setProperty("http.nonProxyHosts", "localhost|192.168.0.*");
// 设置安全访问使用的代理服务器地址与端口
// 它没有https.nonProxyHosts属性,它按照http.nonProxyHosts 中设置的规则访问
prop.setProperty("https.proxyHost", "192.168.0.254");
prop.setProperty("https.proxyPort", "443");
// 使用ftp代理服务器的主机、端口以及不需要使用ftp代理服务器的主机
prop.setProperty("ftp.proxyHost", "192.168.0.254");
prop.setProperty("ftp.proxyPort", "2121");
prop.setProperty("ftp.nonProxyHosts", "localhost|192.168.0.*");
// socks代理服务器的地址与端口
prop.setProperty("socksProxyHost", "192.168.0.254");
prop.setProperty("socksProxyPort", "8000");
// 设置登陆到代理服务器的用户名和密码
Authenticator.setDefault(new MyAuthenticator("userName", "Password"));
}
static class MyAuthenticator extends Authenticator {
private String user = "";
private String password = "";
public MyAuthenticator(String user, String password) {
this.user = user;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
returnnew PasswordAuthentication(user, password.toCharArray());
}
}
方法二:使用Proxy来对每个连接实现代理, 这种方法只能在jdk 1.5以上的版本使用(包含jdk1.5), 优点是可以单独的设置每个连接的代理, 缺点是设置比较麻烦:
public static void main(String[] args) {
try {
URL url = new URL("http://www.baidu.com");
// 创建代理服务器
InetSocketAddress addr = new InetSocketAddress("192.168.0.254",
8080);
// Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr); // Socket 代理
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); // http 代理
// 如果我们知道代理server的名字, 可以直接使用
// 结束
URLConnection conn = url.openConnection(proxy);
InputStream in = conn.getInputStream();
// InputStream in = url.openStream();
String s = IOUtils.toString(in);
System.out.println(s);
} catch (Exception e) {
e.printStackTrace();
}
}
不是很详细, 有什么问题还望大家指正
================================
/*
* Output:
*
*/ import java.io.InputStream;
import java.net.URL; public class MainClass { public static void main(String args[]) {
try { URL url = new URL("http://www.java2s.com"); // Obtain output stream
InputStream is = url.openStream(); // Read and display data from url
byte buffer[] = new byte[1024];
int i;
while ((i = is.read(buffer)) != -1) {
System.out.write(buffer, 0, i);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
———————————————————————————————————————————————————————— package exp; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection; public class Download {
// 生成图片函数
public void makeImg(String imgUrl, String fileURL) {
try {
InetSocketAddress addr = new InetSocketAddress("proxy_ip", 7777);
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); // http 代理
// 如果我们知道代理server的名字, 可以直接使用
// 结束
URLConnection conn = new URL(imgUrl).openConnection(proxy);
System.out.println(conn);
BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
// 创建流
// BufferedInputStream in = new BufferedInputStream(
// new URL(imgUrl).openStream());
// 生成图片名
int index = imgUrl.lastIndexOf("/");
String sName = imgUrl.substring(index + 1, imgUrl.length());
System.out.println(sName);
// 存放地址
File img = new File(fileURL + sName);
// 生成图片
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(img));
byte[] buf = new byte[2048];
int length = in.read(buf);
while (length != -1) {
out.write(buf, 0, length);
length = in.read(buf);
}
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Download().makeImg("http://i3.ku6img.com/cms/jc/201107/26/129920Hme3_1.jpg", "./");
// new Download().makeImg("http://baidu.com", "C:\\Users\\decli\\Desktop\\");
}
}
上一篇:大规模集群FTP代理(基于lvs的vsftpd网络负载均衡方案部署(PASV))


下一篇:iOS,长按图片保存实现方法,轻松搞定!