【本文将介绍java实现ftp文件的上传、下载、删除等操作】
本地搭建FTP服务器见:http://www.xitongcheng.com/jiaocheng/win7_article_38469.html
需要commons-net.jar,需要目标FTP服务器的ip、端口(默认21)、用户名、密码。
public class FtpUtil {
private String ipAddr;
private int port;
private String userName;
private String pwd;
public FtpUtil(){super();}
public FtpUtil(String ipAddr, int port, String userName, String pwd){
this.ipAddr = ipAddr;
this.port = port;
this.userName = userName;
this.pwd = pwd;
}
/**
* 连接ftp ,成功返回FTPClient
*/
public FTPClient connectFTP() {
boolean success = false;
FTPClient ftp = null;
while (!success) {
try {
ftp = new FTPClient();
ftp.setControlEncoding(\"UTF-8\");
ftp.connect(ipAddr, port);
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
conf.setServerLanguageCode(\"zh\");
ftp.login(userName, pwd);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
success = false;
}else{
success = true;
}
if(!success){
System.out.println(\"连接失败\");
Thread.sleep(3000);
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(\"连接成功\");
return ftp;
}
/**
* 创建目录
* @param paths
*/
private synchronized boolean markAndChangeDirectory(String paths , FTPClient ftp){
boolean code = false;
try {
if(paths==null){
paths =\"\";
}
String[] pathArrray = paths.split(\"/\");
for(String path : pathArrray){
if(!ftp.changeWorkingDirectory(path)){
ftp.makeDirectory(path);
ftp.changeWorkingDirectory(path);
}
}
code = true;
} catch (IOException e) {
code = false;
}
return code;
}
/**
* 上传文件
* @param pathname ftp服务保存地址
* @param fileName 上传到ftp的文件名
* @param originfilename 待上传文件的名称(绝对地址)
* @return
*/
//ftp.uploadFile(\"ftpFile/data\", \"123.docx\", \"E://123.docx\");
public boolean uploadFile( String pathname, String fileName,String originfilename){
InputStream inputStream = null;
FTPClient ftp = null;
try{
inputStream = new FileInputStream(new File(originfilename));
ftp = connectFTP();
ftp.setFileType(FTP.BINARY_FILE_TYPE);
markAndChangeDirectory(pathname,ftp);
boolean code = ftp.storeFile(fileName, inputStream);
inputStream.close();
if(code){
StringBuffer sb = new StringBuffer();
sb.append(\"ftp://\");
sb.append(userName).append(\":\").append(pwd).append(\"@\").append(ipAddr).append(\":\").append(port).append(ftp.printWorkingDirectory()+ \"/\" + fileName);
System.out.println(\"文件地址:\"+sb);
ftp.logout();
}else{
System.out.println(\"上传失败\");
return false;
}
} catch (Exception e) {
System.out.println(\"上传文件失败\");
e.printStackTrace();
return false;
} finally{
if(ftp.isConnected()){
try{
ftp.disconnect();
}catch(IOException e){
e.printStackTrace();
}
}
if(null != inputStream){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
/**
* 下载文件
* @param pathname FTP服务器文件目录
* @param filename 文件名称
* @param localpath 下载后的文件路径
* @return */
//ftp.downloadFile(\"ftpFile/data\", \"123.docx\", \"E://\");
public boolean downloadFile(String pathname, String filename, String localpath){
FTPClient ftp = null;
OutputStream os=null;
try {
ftp = connectFTP();
ftp.changeWorkingDirectory(pathname);
FTPFile[] ftpFiles = ftp.listFiles();
for(FTPFile file : ftpFiles){
if(filename.equalsIgnoreCase(file.getName())){
File localFile = new File(localpath + \"/\" + file.getName());
os = new FileOutputStream(localFile);
ftp.retrieveFile(file.getName(), os);
os.close();
}
}
ftp.logout();
System.out.println(\"下载文件成功\");
} catch (Exception e) {
System.out.println(\"下载文件失败\");
e.printStackTrace();
return false;
} finally{
if(ftp.isConnected()){
try{
ftp.disconnect();
}catch(IOException e){
e.printStackTrace();
}
}
if(null != os){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
/**
* 删除文件 *
* @param pathname FTP服务器保存目录
* @param filename 要删除的文件名称
* @return */
public boolean deleteFile(String pathname, String filename){
FTPClient ftp = null;
try {
ftp = connectFTP();
ftp.changeWorkingDirectory(pathname);
ftp.dele(filename);
ftp.logout();
System.out.println(\"删除文件成功\");
} catch (Exception e) {
System.out.println(\"删除文件失败\");
e.printStackTrace();
return false;
} finally {
if(ftp.isConnected()){
try{
ftp.disconnect();
}catch(IOException e){
e.printStackTrace();
}
}
}
return true;
}
public String getIpAddr() {
return ipAddr;
}
public void setIpAddr(String ipAddr) {
this.ipAddr = ipAddr;
}
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 getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
继续阅读与本文标签相同的文章
上一篇 :
Quick BI----让每个人都是数据分析师
下一篇 :
看我如何黑进邻居的电视
-
两问快递涨价
2026-05-19栏目: 教程
-
一图了解顺丰全球供应链网络布局
2026-05-19栏目: 教程
-
这款 IDE 插件再次升级,让「小程序云」的开发部署提速 8 倍
2026-05-19栏目: 教程
-
专注于技术能力提升的央企,注定不平凡,我有看点!
2026-05-19栏目: 教程
-
男友力爆棚的Mac电脑办公软件WPS Office
2026-05-19栏目: 教程
