package com.free.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
/**
* 文件上传到FTP服务器
* @author guzai
* @since 2014-09-10
* 本地可用(serv-u建立FTP服务器)
*/
public class FtpUtil {
private static FTPClient ftp;
/**
* 连接FTP服务器
* <hr>
* @param path 上传到ftp服务器哪个路径下
* @param addr 地址
* @param port 端口号
* @param username 用户名
* @param password 密码
* @return
*/
public static boolean connect(String path,String addr,int port,String username,String password) throws Exception {
int reply;
boolean result = false;
ftp = new FTPClient();
//设置编码
ftp.setControlEncoding("UTF-8");
//连接ftp
ftp.connect(addr,port);
//登录
boolean flag = ftp.login(username,password);
if(!flag){
return false;
}else{
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
ftp.changeWorkingDirectory(path);
result = true;
return result;
}
}
/**
* 上传文件到FTP
* <hr>
* @param file 上传的文件或文件夹
* @param path 上传的文件的路径
*/
public static void upload(File file , String path) {
boolean flag = false;
try {
//文件夹
if (file.isDirectory()) {
ftp.makeDirectory(file.getName());
ftp.changeWorkingDirectory(file.getName());
String[] files = file.list();
for (int i = 0; i < files.length; i++) {
File file1 = new File(file.getPath() + "\\" + files[i]);
if (file1.isDirectory()) {
upload(file1, path);
ftp.changeToParentDirectory();
} else {
File file2 = new File(file.getPath() + "\\" + files[i]);
FileInputStream input = new FileInputStream(file2);
flag = ftp.storeFile(file2.getName(), input);
input.close();
}
}
} else {
File file2 = new File(file.getPath());
InputStream input = new FileInputStream(file2);
ftp.changeWorkingDirectory(path);
flag = ftp.storeFile(file2.getName(), input);
input.close(); //关闭输入流
ftp.logout(); //退出连接
}
} catch (Exception e) {
e.printStackTrace();
}
if(flag){
System.out.println("上传成功");
}else{
System.out.println("上传失败");
}
}
/**
* FTP下载文件
* <hr>
* @Description : FPT 下载文件方法
* @param downloadPath 下载文件的路径
* @param fileName 下载的文件名
* @param localPath 下载的文件本地路径
*/
public static void download(String downloadPath , String fileName , String localPath ){
boolean flag = false;
try {
ftp.changeWorkingDirectory(downloadPath);
// 列出该目录下所有文件
FTPFile[] fs = ftp.listFiles();
// 遍历所有文件,找到指定的文件
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
// 根据绝对路径初始化文件
File localFile = new File(localPath + "/"
+ ff.getName());
// 输出流
OutputStream is = new FileOutputStream(localFile);
// 下载文件
flag = ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
ftp.logout(); //退出连接
} catch (Exception e) {
e.printStackTrace();
}
if(flag){
System.out.println("下载成功!");
}else{
System.out.println("下载失败!");
}
}
/**
* 测试
*/
public static void main(String[] args) throws Exception{
/* boolean flag = FtpUtil.connect("D:\\", "127.0.0.1", 21, "guzai", "guzai");
if(flag){
//上传
File file = new File("d:\\44.jpg");
FtpUtil.upload(file , "F:\\FTP");
//下载
//t.download("F:\\FTP", "我创建的文件.txt", "D:\\");
}*/
boolean flag = FtpUtil.connect("test\\", "192.168.101.97", 33, "ewaytec\\daemon_work", "12#$abCD");
if(flag){
File file = new File("d:\\44.jpg");
//上传
FtpUtil.upload(file , "192.168.101.97\\test");
//下载
//t.download("F:\\FTP", "我创建的文件.txt", "D:\\");
}
}
}
文件上传至FTP工具类
最新推荐文章于 2021-11-25 15:48:48 发布