上传文件到ftp 并下载,或删除

来源:互联网 发布:闪电VPN软件怎么样 编辑:程序博客网 时间:2024/05/20 14:24
 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");
}


}
0 0
原创粉丝点击