1. 程式人生 > >ftp上傳

ftp上傳

ted setfile rep dex gbk tostring pre err timezone

/**
* FTP上傳工具類
*
*/
public class FtpUtil {
static Logger logger = Logger.getLogger(FtpUtil.class.getName());
private String ip;
private String username;
private String password;
private int port = -1;
private String path;
private OutputStream os = null;
private 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, String username, String password) {
this.ip = serverIP;
this.username = username;
this.password = password;
}

public FtpUtil(String serverIP, int port, String username, String password) {
this.ip = serverIP;
this.username = username;
this.password = password;
this.port = 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);
try {
if (this.port > 0) {
this.ftpClient.connect(this.ip, this.port);
}else {
this.ftpClient.connect(this.ip);
}
int reply = this.ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
this.ftpClient.disconnect();
logger.info("登錄FTP服務失敗!");
return isLogin;
}
if(this.ftpClient.login(this.username, this.password)){
this.ftpClient.enterLocalPassiveMode(); // 設置傳輸協議
this.ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
logger.info(this.username + "成功登陸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);
return isLogin;
}
/**
* @退出關閉服務器鏈接
*/
public void ftpLogOut() {
if (null != this.ftpClient && this.ftpClient.isConnected()) {
try {
boolean reuslt = this.ftpClient.logout();// 退出FTP服務器
if (reuslt) {
logger.info("成功退出服務器");
}
}catch (IOException e) {
e.printStackTrace();
logger.warn("退出FTP服務器異常!" + e.getMessage());
}finally {
try {
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;
boolean success = false;
try {
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();
}finally {
if (inStream != null) {
try {
inStream.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
return success;
}

/**上傳文件夾
* @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 : " + 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 < allFile.length;currentFile++) {
if (!allFile[currentFile].isDirectory()) {
String srcName = allFile[currentFile].getPath().toString();
uploadFile(new File(srcName), remoteDirectoryPath);
}
}
for (int currentFile = 0;currentFile < allFile.length;currentFile++) {
if (allFile[currentFile].isDirectory()) {
uploadDirectory(allFile[currentFile].getPath().toString(), //遞歸
remoteDirectoryPath);
}
}
}
/*for (int currentFile = 0;currentFile < allFile.length;currentFile++) {
if (!allFile[currentFile].isDirectory()) {
String srcName = allFile[currentFile].getPath().toString();
uploadFile(new File(srcName), remoteDirectoryPath);
}
}
for (int currentFile = 0;currentFile < allFile.length;currentFile++) {
if (allFile[currentFile].isDirectory()) {
uploadDirectory(allFile[currentFile].getPath().toString(), //遞歸
remoteDirectoryPath);
}
} */
return true;
}

ftp上傳