- import java.io.BufferedInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.SocketException;
- import java.text.SimpleDateFormat;
- import java.util.Properties;
-
- import org.apache.commons.logging.Log;
- import org.apache.commons.net.ftp.FTP;
- import org.apache.commons.net.ftp.FTPClient;
- import org.apache.commons.net.ftp.FTPClientConfig;
- import org.apache.commons.net.ftp.FTPReply;
-
-
- public class Ftp {
-
- private static Log logger;
-
- private static String UserName;
-
- private static String Password;
-
- private static String Ip;
-
- private static int Port;
-
- private static Properties Property = null;
-
- private static String ConfigFile = "src/com/wwkj/cms/test/ftp/ftpconfig.properties";
-
- private static FTPClient FtpClient = null;
-
- private static SimpleDateFormat dateFormat = new SimpleDateFormat(
- "yyyy-MM-dd hh:mm");
-
- private static final String[] FILE_TYPES = { "文件", "目录", "符号链接", "未知类型" };
-
- public static final int BINARY_FILE_TYPE = FTP.BINARY_FILE_TYPE;
-
- public static final int ASCII_FILE_TYPE = FTP.ASCII_FILE_TYPE;
- public static int i=1;
- public static void main(String[] args) {
-
- connectServer();
-
-
-
-
- setFileType(FTP.BINARY_FILE_TYPE);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- closeConnect();
- }
-
-
- public static boolean uploadFile(String localFile, String newFileName) {
- boolean flag = true;
- try {
-
- connectServer();
- FtpClient.setFileType(BINARY_FILE_TYPE);
-
- FtpClient.enterLocalPassiveMode();
-
- FtpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
- File file = new File(localFile);
-
- File newFile = new File(newFileName);
- String dir = newFile.getParentFile().getPath();
- if (!FtpClient.changeWorkingDirectory(dir)) {
-
- if (!makeDirectory(newFile.getParentFile().getPath())) {
-
- System.out.println("创建文件目录【"+dir+"】 失败!");
- }
- }
- changeWorkingDirectory("/");
- InputStream input = new FileInputStream(file);
-
- if (input == null) {
- System.out.println("本地文件不存在");
- logger.debug("本地文件不存在,请重新选择!");
-
- }
- if (newFileName.trim().equals("")) {
-
- newFileName = file.getName();
-
- }
- flag = FtpClient.storeFile(newFileName, input);
- if (flag) {
- System.out.println("upload File succeed");
-
- } else {
- System.out.println("upload File false");
-
- }
- input.close();
-
- } catch (IOException e) {
- e.printStackTrace();
- logger.debug("本地文件上传失败!", e);
-
- } catch (Exception e) {
- e.printStackTrace();
-
-
- }
- return flag;
- }
-
-
-
-
- public static void closeConnect() {
- try {
- if (FtpClient != null) {
- FtpClient.logout();
- FtpClient.disconnect();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
- public static void setConfigFile(String configFile) {
- Ftp.ConfigFile = configFile;
- }
-
-
- public static void setFileType(int fileType) {
- try {
- connectServer();
- FtpClient.setFileType(fileType);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
- protected static FTPClient getFtpClient() {
- connectServer();
- return FtpClient;
- }
-
-
- private static void setArg(String configFile) {
- Property = new Properties();
- BufferedInputStream inBuff = null;
- try {
- File file = new File(configFile);
-
- inBuff = new BufferedInputStream(new FileInputStream(file));
-
- Property.load(inBuff);
-
- UserName = Property.getProperty("username");
- Password = Property.getProperty("password");
- Ip = Property.getProperty("ip");
- Port = Integer.parseInt(Property.getProperty("port"));
- } catch (FileNotFoundException e1) {
- System.out.println("配置文件 【" +configFile +"】不存在!");
- e1.printStackTrace();
- } catch (IOException e) {
- System.out.println("配置文件 【" +configFile+ "】无法读取!");
- e.printStackTrace();
- }
-
-
- }
-
-
- public static boolean connectServer() {
- boolean flag = true;
- if (FtpClient == null) {
- int reply;
- try {
- setArg(ConfigFile);
- FtpClient = new FTPClient();
- FtpClient.setControlEncoding("GBK");
- FtpClient.setDefaultPort(Port);
- FtpClient.configure(getFtpConfig());
- FtpClient.connect(Ip);
- FtpClient.login(UserName, Password);
- FtpClient.setDefaultPort(Port);
-
- reply = FtpClient.getReplyCode();
- FtpClient.setDataTimeout(120000);
-
- if (!FTPReply.isPositiveCompletion(reply)) {
- FtpClient.disconnect();
- System.err.println("FTP server refused connection.");
-
- flag = false;
- }
-
- } catch (SocketException e) {
- flag = false;
- e.printStackTrace();
- System.err.println("登录ftp服务器【" +Ip+ "】失败,连接超时!");
-
- } catch (IOException e) {
- flag = false;
-
- e.printStackTrace();
- System.err.println("登录ftp服务器【"+ Ip +"】失败,FTP服务器无法打开!");
-
- }
-
- }
- return flag;
- }
-
-
- public static void changeWorkingDirectory(String directory) {
- try {
- connectServer();
- FtpClient.changeWorkingDirectory(directory);
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
- }
-
-
-
- public static void renameFile(String oldFileName, String newFileName) {
- try {
- connectServer();
- FtpClient.rename(oldFileName, newFileName);
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
- }
-
-
- private static FTPClientConfig getFtpConfig() {
- FTPClientConfig ftpConfig = new FTPClientConfig(
- FTPClientConfig.SYST_UNIX);
- ftpConfig.setServerLanguageCode(FTP.DEFAULT_CONTROL_ENCODING);
- return ftpConfig;
- }
-
-
- private static String iso8859togbk(Object obj) {
- try {
- if (obj == null)
- return "";
- else
- return new String(obj.toString().getBytes("iso-8859-1"), "GBK");
- } catch (Exception e) {
- return "";
- }
- }
-
-
- public static boolean makeDirectory(String dir) {
- connectServer();
- boolean flag = true;
- try {
-
- flag = FtpClient.makeDirectory(dir);
- if (flag) {
- System.out.println("make Directory " +dir +" succeed");
-
- } else {
-
- System.out.println("make Directory " +dir+ " false");
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return flag;
- }
-
- public static Log getLogger() {
- return logger;
- }
-
- public static void setLogger(Log logger) {
- Ftp.logger = logger;
- }
-
- }
java 远程ftp建立文件夹
原文:http://www.cnblogs.com/lzw0414/p/5411509.html