上传文件到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
- 上传文件到ftp 并下载,或删除
- Java连接FTP服务器,并上传或下载以及删除ftp服务器中的文件
- Java 使用ftp的方式上传文件到linux,并实现删除,下载
- 定期自动重命名文件并上传到ftp然后删除
- php操作ftp上传文件、创建目录、删除文件、删除目录、下载文件到本地
- php操作ftp上传文件、创建目录、删除文件、删除目录、下载文件到本地
- java上传、下载、删除ftp文件
- FTP连接,上传,下载,删除文件方法
- 上传文件或图片到ftp
- 自动打包文件并上传到ftp
- java 上传 下载 文件到FTP服务器
- ftp 上传 下载 删除
- C# FTP常用操作 FTP上传文件 FTP下载文件 FTP删除文件
- window服务器通过sftp从linux下载文件,并ftp上传到window服务器
- 通过ftp上传图片到服务器并支持下载预览
- ftp文件下载、上传
- ftp上传 下载文件
- FTP实现文件/文件夹的上传或下载
- codeforces 803E Roma and Poker (dp)
- nginx 事件驱动模型
- [PlantSimulation]层式结构及其动画设置
- 水仙花数
- 5月题目汇总1 (4.30-5.6)
- 上传文件到ftp 并下载,或删除
- 又见01背包
- 火狐浏览器enter键不跳转登录问题
- 求三角形面积
- JavaScript语言基础---函数细节继续
- mybatis多查询
- Node.js单例模式
- Web前端复习——JS(正则表达式+内置对象)
- PHP设计模式系列(十二):适配器模式