/**
* FTP上傳工具類別
*
*/
public class FtpUtil {
static Logger logger = Logger.getLogger(FtpUtil.class.getName());
private String ip;
String1private String用戶名;
私有字符串密碼;
私有int port = -1;
私有字符串路徑;
私有OutputStream os = null;
私有FileInputStream is = null;
private FTPClient ftpClient ;
public FTPClient getftpClient() { return ftpClient; } }
public void setftpClient(FTPClient ftpClient) {
this.ftpClient = ftpClient;
}
public FtpUtil(){
}
public FtpUtil(String serverIP, StringString使用者名,字串密碼) {
this.ip = serverIP;
this.username = 使用者名稱;
this.password = 密碼;
}
# public FtpUtil(String serverIP, int埠、字串使用者名稱、字串密碼) {
this.ip = 伺服器IP;
this.username = 使用者名稱;
this.password = 密碼;
this.port = 埠;
this. ftpClient= new FTPClient();
}
/**
*
* @return 是否連接伺服器
*/
public boolean ftpLogin() {
boolean isLogin = false;
FTPClientConfig ftpClientConfig = new FTPClientConfig();
ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());
this.ftpClient.setControlEncoding("GBK");
this.ftpClient.configure(ftpClientConfig);
嘗試 {
if (this.port > 0) {
this.ftpClient.connect(this.ip, this.port);
}else {
this.ftpClient.connect(this.ip);
}
intreply = this.ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
this.ftpClient.disconnect();
logger.info("登入FTP服務失敗!");
回傳 isLogin;
}
if(this.ftpClient.login(this.username, this.password)){
this.ftpClient.enterLocalPassiveMode(); // 設定傳輸協定
this.ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
logger.info(this.使用者名稱 + "成功登陸FTP伺服器");
isLogin = true;
}else{
logger.info(this.username + "登入FTP服務失敗!");
}
}catch (Exception e) {
e.printStackTrace();
logger.error( e.getMessage());
}
this.ftpClient.setBufferSize(1024 * 2);
this.ftpClient.setDataTimeout(30 * 1000);
回傳 isLogin;
}
/**
* @退出關閉伺服器連結
*/
public void ftpLogOut() {
if (null != this.ftpClient && this.ftpClient.isConnected()) {
嘗試 {
boolean reuslt = this.ftpClient.logout();//退出FTP伺服器
if (reuslt) {
logger.info("成功退出伺服器");
}
}catch (IOException e) {
e.printStackTrace();
logger.warn("退出FTP伺服器異常!" + e.getMessage());
}最後{
試試{
this.ftpClient.disconnect();
}catch (IOException e) {
e.printStackTrace();
logger.error("關閉FTP伺服器的連線異常!");
}
}
}
}
/**
* 上傳Ftp文件
* @param localFile 當地文件
* @param romotUpLoadePath 上傳服務器路徑
*/
public boolean uploadFile(File localFile, String romotUpLoadePath) {
BufferedInputStream inStream = null;
布林成功 = false;
嘗試 {
this.ftpClient.changeWorkingDirectory(romotUpLoadePath);
inStream = new BufferedInputStream(new FileInputStream(localFile));
success = this.ftpClient.storeFile(localFile.getName(), inStream);
if (success == true) {
System.out.println(localFile.getName() + "上傳成功");///
return success;
}
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}最後{
if (inStream != null) {
試{
inStream.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
回傳成功中;
}
/**上傳資料夾
* @param localDirectory 本機資料夾
* @param remoteDirectoryPath 遠端路徑
*/
public boolean uploadDirectory(String localDirectory,String remoteDirectoryPath) {
File src = new File(localDirectory);
System.out.println(src.getName());
try {
remoteDirectoryPath = remoteDirectoryPath + src.getName() + "/";
boolean makeDirFlag = ftpClient.makeDirectory(remoteDirectoryPath);
System.out.println("本地目錄:" + localDirectory);
System.out.println("remoteDirectoryPath : " + remoteDirectoryPath);
System.out.println("src.getName() : " + src.getName());
System.out.println("remoteDirectoryPath : " + remoteDirectoryPath);
System.out.println("makeDirFlag : " + makeDirFlag);
// ftpClient.listDirectories();
}catch (IOException e) {
e.printStackTrace();
}
File[] allFile = src.listFiles();
if(allFile.length==0||allFile.length>0){
for (int currentFile = 0;currentFile if (!allFile[currentFile] . isDirectory()) {
String srcName = allFile[currentFile].getPath().toString();
uploadFile(new File(srcName),remoteDirectoryPath);
}
}
for (int currentFile = 0;currentFile if (allFile[currentFile].isDirectory()) {
uploadDirectory(allFile[currentFile] ].getPath().toString(), //樓層
remoteDirectoryPath);
}
}
}
/*for (int currentFile = 0;currentFile if (!allFile[currentFile].isDirectory()) {
String srcName = allFile[currentFile].getPath().toString();
uploadFile(new File(srcName),remoteDirectoryPath);
}
}
for (int currentFile = 0;currentFile if (allFile[currentFile].isDirectory()) {
# uploadDirectory(allFile[currentFile] ].getPath().toString(), //樓層
remoteDirectoryPath);
}
} */
回傳 true;
}
以上是ftp上傳實例程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!