- package ftp2;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.SocketException;
- import org.apache.commons.io.FileUtils;
- import org.apache.commons.io.IOUtils;
- import org.apache.commons.net.ftp.FTPClient;
- import org.apache.commons.net.ftp.FTPClientConfig;
- import org.apache.commons.net.ftp.FTPFile;
- import org.apache.commons.net.ftp.FTPReply;
- /**
- * 使用commons的net包進行ftp鏈接. 相關包:commons-net-1.4.1.jar ;
- * commons-io-1.2.jar;jakarta-oro-2.0.8.jar測試通過.可以列出ftp上的文件
- * 通過把ftp服務器上的文件流連接到outSteam及可以把文件下載到本機的目錄..限制如果目錄爲中文則需要處理.最好使用英文文件名
- *
- */
- public class ListFtpFile {
- private FTPClient ftpClient = new FTPClient();
- private OutputStream outSteam = null;
- /**
- * ftp服務器地址
- */
- private String hostName = "192.168.0.2";
- private int port = 212;
- /**
- * 登錄名
- */
- private String userName = "anonymous";//匿名登錄,空字符串不行
- /**
- * 登錄密碼
- */
- private String password = "[email protected]";//隨便一個地址,我胡亂寫一個也可以運行的
- /**
- * 需要訪問的遠程目錄
- */
- private String remoteDir = "/software/dreamweaver/";
- /**
- * 下載
- */
- private void download() {
- try {
- // 鏈接到ftp服務器
- ftpClient.connect(hostName,port);
- System.out.println("連接到ftp服務器:" + hostName + " 成功..開始登錄");
- // 登錄.用戶名 密碼
- boolean b = ftpClient.login(userName, password);
- System.out.println("登錄成功." + b);
- // 檢測連接是否成功
- int reply = ftpClient.getReplyCode();
- if(!FTPReply.isPositiveCompletion(reply)) {
- ftpClient.disconnect();
- System.err.println("FTP server refused connection.");
- System.exit(1);
- }
- ftpClient.setControlEncoding("GBK");
- FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
- conf.setServerLanguageCode("zh");
- FTPFile[] remoteFiles = ftpClient.listFiles(remoteDir);
- if (remoteFiles != null) {
- for (int i = 0; i < remoteFiles.length; i++) {
- String name = remoteFiles[i].getName();
- //下載
- File localFile = new File("c:/001/ftp/" + name);
- OutputStream is = new FileOutputStream(localFile);
- //retrieveFile的第一個參數需要是 ISO-8859-1 編碼,並且必須是完整路徑!
- String fileName = remoteDir + name;
- ftpClient.retrieveFile(new String(fileName.getBytes("GBK"),"ISO-8859-1"), is);
- is.close();
- //打印
- long length = remoteFiles[i].getSize();
- String readableLength = FileUtils.byteCountToDisplaySize(length);
- System.out.println(name + ":\t"+remoteFiles[i].isFile()+"\t" + readableLength);
- }
- }
- ftpClient.logout();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- IOUtils.closeQuietly(outSteam);
- try {
- ftpClient.disconnect();
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
- }
- }
- /**
- * 上傳
- * */
- public void upload(){
- String srcUrl = "C:/001/菜單權限設計.doc";
- String targetFileName = "菜單權限設計.doc";
- try {
- ftpClient.connect(hostName,port);
- boolean b = ftpClient.login(userName, password);
- // 檢測連接是否成功
- int reply = ftpClient.getReplyCode();
- if (!FTPReply.isPositiveCompletion(reply)) {
- ftpClient.disconnect();
- System.err.println("FTP server refused connection.");
- System.exit(1);
- }
- ftpClient.setControlEncoding("GBK");
- FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
- conf.setServerLanguageCode("zh");
- File srcFile = new File(srcUrl);
- FileInputStream fis = null;
- fis = new FileInputStream(srcFile);
- // 設置上傳目錄
- ftpClient.changeWorkingDirectory(remoteDir);
- ftpClient.setBufferSize(1024);
- ftpClient.setControlEncoding("GBK");
- // 設置文件類型(二進制)
- ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
- // 上傳
- b = ftpClient.storeFile(targetFileName, fis);
- IOUtils.closeQuietly(fis);
- /*boolean bool = ftpClient.changeWorkingDirectory("/NC");
- System.out.println("changeWorkingDirectory="+bool);
- bool = ftpClient.makeDirectory("/NC");
- System.out.println("makeDirectory="+bool);*/
- ftpClient.logout();
- } catch (SocketException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- try {
- ftpClient.disconnect();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 測試
- * */
- public static void main(String[] args) {
- ListFtpFile listFtpfiles = new ListFtpFile();
- listFtpfiles.download();
- listFtpfiles.upload();
- }
- }
package ftp2;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.SocketException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
/**
* 使用commons的net包進行ftp鏈接. 相關包:commons-net-1.4.1.jar ;
* commons-io-1.2.jar;jakarta-oro-2.0.8.jar測試通過.可以列出ftp上的文件
* 通過把ftp服務器上的文件流連接到outSteam及可以把文件下載到本機的目錄..限制如果目錄爲中文則需要處理.最好使用英文文件名
*
*/
public class ListFtpFile {
private FTPClient ftpClient = new FTPClient();
private OutputStream outSteam = null;
/**
* ftp服務器地址
*/
private String hostName = "192.168.0.2";
private int port = 212;
/**
* 登錄名
*/
private String userName = "anonymous";//匿名登錄,空字符串不行
/**
* 登錄密碼
*/
private String password = "[email protected]";//隨便一個地址,我胡亂寫一個也可以運行的
/**
* 需要訪問的遠程目錄
*/
private String remoteDir = "/software/dreamweaver/";
/**
* 下載
*/
private void download() {
try {
// 鏈接到ftp服務器
ftpClient.connect(hostName,port);
System.out.println("連接到ftp服務器:" + hostName + " 成功..開始登錄");
// 登錄.用戶名 密碼
boolean b = ftpClient.login(userName, password);
System.out.println("登錄成功." + b);
// 檢測連接是否成功
int reply = ftpClient.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
ftpClient.setControlEncoding("GBK");
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
conf.setServerLanguageCode("zh");
FTPFile[] remoteFiles = ftpClient.listFiles(remoteDir);
if (remoteFiles != null) {
for (int i = 0; i < remoteFiles.length; i++) {
String name = remoteFiles[i].getName();
//下載
File localFile = new File("c:/001/ftp/" + name);
OutputStream is = new FileOutputStream(localFile);
//retrieveFile的第一個參數需要是 ISO-8859-1 編碼,並且必須是完整路徑!
String fileName = remoteDir + name;
ftpClient.retrieveFile(new String(fileName.getBytes("GBK"),"ISO-8859-1"), is);
is.close();
//打印
long length = remoteFiles[i].getSize();
String readableLength = FileUtils.byteCountToDisplaySize(length);
System.out.println(name + ":\t"+remoteFiles[i].isFile()+"\t" + readableLength);
}
}
ftpClient.logout();
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(outSteam);
try {
ftpClient.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
/**
* 上傳
* */
public void upload(){
String srcUrl = "C:/001/菜單權限設計.doc";
String targetFileName = "菜單權限設計.doc";
try {
ftpClient.connect(hostName,port);
boolean b = ftpClient.login(userName, password);
// 檢測連接是否成功
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
ftpClient.setControlEncoding("GBK");
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
conf.setServerLanguageCode("zh");
File srcFile = new File(srcUrl);
FileInputStream fis = null;
fis = new FileInputStream(srcFile);
// 設置上傳目錄
ftpClient.changeWorkingDirectory(remoteDir);
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("GBK");
// 設置文件類型(二進制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// 上傳
b = ftpClient.storeFile(targetFileName, fis);
IOUtils.closeQuietly(fis);
/*boolean bool = ftpClient.changeWorkingDirectory("/NC");
System.out.println("changeWorkingDirectory="+bool);
bool = ftpClient.makeDirectory("/NC");
System.out.println("makeDirectory="+bool);*/
ftpClient.logout();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 測試
* */
public static void main(String[] args) {
ListFtpFile listFtpfiles = new ListFtpFile();
listFtpfiles.download();
listFtpfiles.upload();
}
}
需要common-io 和common-net兩個jar包的,同學們自己下載吧