中国开发网: 论坛: Java/J2ME: 贴子 228233
空山新雨: FtpUtil.java
/*
* Created on May 18, 2005
*
* To handle the transferring of data file from/to FTP servers.
* This class support servers outside of corporate firewall.
*/
package com.bnpparibas.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;

/**
* @author Alex SUO
*
* To handle the downloading of data file from Reuters FTP server
* through Firewall. The data files are for OPTIME server.
*
* Methods in this class support proxy settings. To make this utility
* class work for FTP through proxy/firewall, use the
* <code>setProxy()</code> and <code>clearProxy()</code>
* to apply the settings.
*
* Another way is to set the corresponding JVM parameters for the
* following types of proxy:
*
* FTP proxy: -DftpProxySet=true
* -DftpProxyPort=<proxy port>
* -DftpProxyHost=<proxy host>
*
* HTTP proxy: -DproxySet=true
* -DproxyPort=<proxy port>
* -DproxyHost=<proxy host>
*
* SOCK4 or SOCK5 proxy:
* -DsocksProxyPort=<proxy port>
* -DsocksProxyHost=<proxy host>
* -Djava.net.socks.username=<proxy user name>
* -Djava.net.socks.password=<proxy password>
*
* However, please note that, for both <code>setProxy()</code>
* and parameters in start command, those parameters are set for the
* entire JVM, so other classes in the application will also
* apply such settings. This is why the class is implemented with
* all static methods.
*
* Those proxy parameters are ONLY needed for FTP servers
* outside of the corporate firewall (e.g. Reuters FTP server).
* For any FTP servers inslde the firewall, those parameters
* must NOT be set. Otherwise the program won't work.
*
* This class is fully tested in the following scenario:
*
* Connect Type Action File Type
* ==========================================
* Direct Download Text
* Direct Download Binary
* Direct Upload Text
* Direct Upload Binary
*
* FTP Proxy Download Text
* FTP Proxy Download Binary
*
* =========================================
*
* Currently we are using FTP proxy for FTP protocol. Upload
* for FTP proxy is not tested because the firewall blocked it.
* However, it is believed to be OK.
*
* One may notice that there are code to support proxy
* authentication, as well as HTTP ans SOCKET 4/5 proxies.
* However, functions for those part are not tested.
*/
public class FtpUtil {

/** flag for get files from URL to local folder. */
public static final String GET = "get";

/** flag for put files from local folder to URL. */
public static final String PUT = "put";

/** flag for setting FTP proxy. */
public static final int FTP_PROXY = 0;

/** flag for setting HTTP proxy. */
public static final int HTTP_PROXY = 1;

/** flag for setting SOCK4 or SOCK5 proxy. */
public static final int SOCK_PROXY = 2;

/**
* Set the proxy for the current JVM.
* The proxy parameters will be applied for the entire JVM.
* To clear the settings, call <code>clearProxy()</code> to
* reset all the proxy settings.
*
* @param proxyType the type of the proxy for FTP protocol.
*
* FtpUtil.FTP_PROXY: FTP proxy
* FtpUtil.HTTP_PROXY: HTTP proxy
* FtpUtil.SOCK_PROXY: SOCK4 or SOCK5 proxy
*
* @param proxyHost the host string for the proxy.
* @param proxyPort the port of the proxy.
* @see clearProxy()
*/
public static void setProxy(int proxyType, String proxyHost,
String proxyPort) {

Properties prop = System.getProperties();
clearProxy();
if (FTP_PROXY == proxyType) {
prop.put("ftpProxySet", Boolean.TRUE);
prop.put("ftpProxyHost", proxyHost);
prop.put("ftpProxyPort", proxyPort);
} else if (HTTP_PROXY == proxyType) {
prop.put("proxySet", Boolean.TRUE);
prop.put("proxyHost", proxyHost);
prop.put("proxyPort", proxyPort);
} else if (SOCK_PROXY == proxyType) {
prop.put("socksProxySet", Boolean.TRUE);
prop.put("socksProxyHost", proxyHost);
prop.put("socksProxyPort", proxyPort);
}
}

/**
* Set the proxy for the current JVM.
* The proxy parameters will be applied for the entire JVM.
* To clear the proxy settings, call <code>clearProxy()</code>
* to reset the setting.
* @param proxyType the type of the proxy for FTP protocol.
*
* FtpUtil.FTP_PROXY: FTP proxy
* FtpUtil.HTTP_PROXY: HTTP proxy
* FtpUtil.SOCK_PROXY: SOCK4 or SOCK5 proxy
*
* @param proxyHost the host string for the proxy.
* @param proxyPort the port of the proxy.
* @param proxyUser the user name for the proxy, if any.
* @param proxyPassword the password for the proxy, if any.
* @see clearProxy()
* @see setProxy()
*/
public static void setProxy(int proxyType, String proxyHost,
String proxyPort, String proxyUser, String proxyPassword) {

Properties prop = System.getProperties();
clearProxy();
if (SOCK_PROXY == proxyType) {
prop.put("socksProxySet", Boolean.TRUE);
prop.put("socksProxyHost", proxyHost);
prop.put("socksProxyPort", proxyPort);
prop.put("java.net.socks.username", proxyUser);
prop.put("java.net.socks.password", proxyPassword);
}
}

/**
* Clear all the proxy settings for FTP, HTTP and SOCK4/5 proxies.
* @see setProxy()
*/
public static void clearProxy() {
Properties prop = System.getProperties();
if (Boolean.TRUE.equals(prop.get("ftpProxySet"))) {
prop.put("ftpProxySet", Boolean.FALSE);
prop.put("ftpProxyHost", "");
prop.put("ftpProxyPort", "");
} else if (Boolean.TRUE.equals(prop.get("proxySet"))) {
prop.put("ftpProxySet", Boolean.FALSE);
prop.put("ftpProxyHost", "");
prop.put("ftpProxyPort", "");
} else if (Boolean.TRUE.equals(prop.get("socksProxySet"))) {
prop.put("socksProxySet", Boolean.FALSE);
prop.put("socksProxyHost", "");
prop.put("socksProxyPort", "");
}
}

/**
* Get the file at the specific URL, and save it to the
* file for the filePath.
*
* Note that the URL should include the username and
* password for the server. A valid URL is like:
*
* <code>ftp://username:password@server.com/file.txt</code>
*
* @param url the URL of the file to be downloaded.
* @param filePath the full path of the file to be stored.
* @throws Exception if any happened.
*/
public static void getFile(URL url, String filePath)
throws Exception {
URLConnection conn = null;
InputStream is = null;
FileOutputStream fos = null;

//open the connection through the URL
try {
conn = url.openConnection();
is = conn.getInputStream();
} catch (Exception e) {
System.err.println("Error open InputStream from URL: " +
e.getMessage());
throw e;
}

//store the file to the specific path
File f = new File(filePath);
try {
int length = 0;
byte[] buf = new byte[4096];

fos = new FileOutputStream(f);
while ((length = is.read(buf)) != -1) {
fos.write(buf, 0, length);
fos.flush();
}
fos.flush();
} catch (Exception e) {
System.err.println("Problem while downloading the file.");
throw e;
} finally {
if (null != fos) {
fos.close();
}
if (null != is) {
is.close();
}
}
}

/**
* Put a local file to the specific URL.
*
* Note that the URL should include the username and
* password for the server. A valid URL is like:
*
* <code>ftp://username:password@server.com/file.txt</code>
*
* @param url the URL of the file to be put to.
* @param filePath the full path of the file to be uploaded.
* @throws Exception if any happened.
*/
public static void putFile(URL url, String filePath)
throws Exception {
URLConnection conn = null;
OutputStream os = null;
FileInputStream fis = null;

//open the connection through the URL
try {
conn = url.openConnection();
conn.setDoOutput(true);
os = conn.getOutputStream();
} catch (Exception e) {
System.err.println("Error open OutputStream from URL: " +
e.getMessage());
throw e;
}

//store the file to the specific path
File f = new File(filePath);
try {
int length = 0;
byte[] buf = new byte[4096];

fis = new FileInputStream(f);
while ((length = fis.read(buf)) != -1) {
os.write(buf, 0, length);
os.flush();
}
os.flush();
} catch (Exception e) {
System.err.println("Problem while uploading the file.");
throw e;
} finally {
if (null != fis) {
fis.close();
}
if (null != os) {
os.close();
}
}
}

/**
* Entrance for the command line batches to directly call this
* class. This is to ease the integration.
* @param args The arguments should be in the following format
*
* 1. Transfer direction. Should be "get" or "put".
* 2. The remote URL in string format.
* 3. The local folder in string format.
*/
public static void main(String[] args) {

//parameter checking
if (args.length < 3) {
System.out.println("Usage: <program> <get/put> <url> <local folder>");
System.exit(0);
}

try {
if (args[0].equals(FtpUtil.GET)) {
FtpUtil.getFile(new URL(args[1]), args[2]);
} else if (args[0].equals(FtpUtil.PUT)) {
FtpUtil.putFile(new URL(args[1]), args[2]);
} else {
throw new Exception ("args[0] should be get or put");
}
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
}

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录