上傳文件到ftp 並下載,或刪除

 
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * Created by wangyanlong on 2017/4/19.
 */
public class FTPUtils {

    private FTPClient ftp;
    private String addrress;
    private int port;
    private String username;
    private String password;
    private String path;   //上傳到ftp下面的那個路徑下面


    public FTPUtils(String addrress, int port, String username, String password, String path) {
        this.addrress = addrress;
        this.password = password;
        this.username = username;
        this.path = path;
        this.port = port;
    }

    public FTPUtils() {

    }

    public static FTPUtils ftpUtils = null;

    public static FTPUtils getInstance() {
        if (ftpUtils == null) {
            synchronized (FTPUtils.class) {
                if (ftpUtils == null) {
                    ftpUtils = new FTPUtils();
                }
            }
        }
        return ftpUtils;
    }
/**
 * 下載文件
 *
 * @param path
 * @return
 */
public boolean dowinFile(String path, String filename) {
    boolean bool = false;
    OutputStream outputStream = null;
    try {
        String localPath = System.getProperty("user.home") + "\\Downloads\\";
        this.connect();
        System.out.println("鏈接成功!");
        ftp.changeWorkingDirectory(path);//切換到當前目錄
        System.out.println("切換目錄是否成功" + true);
        FTPFile[] files = ftp.listFiles(path + "\\" + filename);
        for (FTPFile f : files) {
            if (filename.equals(f.getName())) {
                System.out.println("本地下載路徑:" + localPath + filename);
                outputStream = new FileOutputStream(localPath + filename);
                bool = ftp.retrieveFile(f.getName(), outputStream);
            }
        }
        System.out.println("下載是否成功:" + bool);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            outputStream.close();
            this.destroy();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return bool;
}
/**
 * 刪除文件
 *
 * @param path
 * @return
 */
public boolean removeFile(String path) {
    boolean bool=false;
    try {
       bool= ftp.deleteFile(path);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return  bool;
}
/**
* @param file 上傳
* @throws Exception
*/
public void ftpUpload(File file, String dir) throws Exception {
this.connect(dir);//獲取鏈接
//創需要的文件夾
if (dir != null) {
ftp.makeDirectory(dir);
ftp.changeWorkingDirectory(dir);
}
this.uploadFile(file);

this.destroy();
}


/**
* 鏈接ftp
*
* @param dir
* @throws IOException
*/
public void connect(String dir) throws IOException {
ftp = new FTPClient();
ftp.connect(addrress, port);
ftp.login(username, password);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
int reply;
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
}
ftp.changeWorkingDirectory(path);

}

//真正上傳文件
public void uploadFile(File file) throws IOException {
if (file.isDirectory()) {
ftp.makeDirectory(file.getName());
ftp.changeWorkingDirectory(file.getName());
File[] files = file.listFiles();
for (File f : files) {
uploadFile(f);
if (f.isDirectory()) {
ftp.changeToParentDirectory();
}
}
} else {
FileInputStream input = new FileInputStream(file);
boolean result = ftp.storeFile(file.getName(), input);
ftp.setDataTimeout(1200);
System.out.println(result);
input.close();
}
}

public void destroy() throws IOException {
if (ftp != null) {
ftp.disconnect();
ftp = null;
}
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

public String getAddrress() {
return addrress;
}

public void setAddrress(String addrress) {
this.addrress = addrress;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public static void main(String[] args) throws Exception {

FTPUtils t = new FTPUtils();

t.setAddrress("");
t.setPort(21);
t.setUsername("");
t.setPassword("");
t.setPath("\\");

File file = new File("F:\\file\\ddd\\");
t.ftpUpload(file, "FF");
}


}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章