Java programming, access to external network FTP via Proxy server

Source: Internet
Author: User

Sometimes our network can not be directly connected to the external network, you need to use HTTP or HTTPS or socket proxy to connect to the external network, here is the Java use Proxy to connect to the external network of some methods, I hope that your program useful.
Method One: Use the System properties to complete the proxy settings, which is relatively simple, but cannot set the proxy for a separate connection:
/**
* @paramargs
*/
/**
* @paramargs
*/
public static void Main (string[] args) {
Properties prop = System.getproperties ();
Set HTTP access to the address of the proxy server to use
Prop.setproperty ("Http.proxyhost", "192.168.0.254");
To set the port for HTTP access to the proxy server to use
Prop.setproperty ("Http.proxyport", "8080");
To set up a host that does not need to be accessed through a proxy server, you can use the * wildcard character and multiple addresses separated by |
Prop.setproperty ("Http.nonproxyhosts", "localhost|192.168.0.*");
To set the proxy server address and port used by secure access
It does not have the Https.nonproxyhosts property, which is accessed according to the rules set in http.nonproxyhosts
Prop.setproperty ("Https.proxyhost", "192.168.0.254");
Prop.setproperty ("Https.proxyport", "443");
Hosts, ports, and hosts that do not require an FTP proxy server using the FTP proxy server
Prop.setproperty ("Ftp.proxyhost", "192.168.0.254");
Prop.setproperty ("Ftp.proxyport", "2121");
Prop.setproperty ("Ftp.nonproxyhosts", "localhost|192.168.0.*");
Socks the address and port of the proxy server
Prop.setproperty ("Socksproxyhost", "192.168.0.254");
Prop.setproperty ("Socksproxyport", "8000");
Set the user name and password to log on to the proxy server
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 ());
}
}
Method Two: Using proxy to implement the agent for each connection, this method can only be used in the version of JDK 1.5 or more (including jdk1.5), the advantage is that each connected agent can be set separately, the disadvantage is that the setup is more troublesome:
public static void Main (string[] args) {
try {
URL url = new URL ("http://www.baidu.com");
Creating a proxy Server
Inetsocketaddress addr = new Inetsocketaddress ("192.168.0.254",
8080);
Proxy proxy = new Proxy (Proxy.Type.SOCKS, addr); Socket Proxy
Proxy proxy = new Proxy (Proxy.Type.HTTP, addr); HTTP Proxy
If we know the name of the proxy server, you can use it directly
End
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 ();
}
}
Not very detailed, there is any problem, I hope you correct
================================
/*
* 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 {
Generating picture functions
public void Makeimg (string imgurl, String FileURL) {
try {
Inetsocketaddress addr = new Inetsocketaddress ("Proxy_ip", 7777);
Proxy proxy = new Proxy (Proxy.Type.HTTP, addr); HTTP Proxy
If we know the name of the proxy server, you can use it directly
End
URLConnection conn = new URL (imgurl). OpenConnection (proxy);
SYSTEM.OUT.PRINTLN (conn);
Bufferedinputstream in = new Bufferedinputstream (Conn.getinputstream ());
Create a Stream
Bufferedinputstream in = new Bufferedinputstream (
New URL (Imgurl). OpenStream ());
Generate Picture Name
int index = Imgurl.lastindexof ("/");
String sName = imgurl.substring (index + 1, imgurl.length ());
System.out.println (SName);
Store Address
File img = new file (FileURL + sName);
Create a picture
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\\");
}
}

Java programming, access to external network FTP via Proxy server

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.