Java 实现ftp上传下载文件

时间:2022-12-25 09:55:50

jar包:commons-net-ftp-2.0.jar

package com.antich.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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

public class FtpManager {

public static void main(String[] args) {
try {
String ip = "192.168.1.114";
String username = "xya_zzzz";
String pwd = "xya_admin";
String path = "./testftp/";
boolean uploadResult = uploadFile(ip,username,pwd,path,"111.jpg",new FileInputStream(new File("F:\\111.jpg")));
if(uploadResult){
System.out.println("上传文件成功");
}else{
System.out.println("上传文件失败");
}
boolean downloadResult = downFile(ip, username, pwd, path,"111.jpg","E:\\");
if(downloadResult){
System.out.println("下载文件成功");
}else{
System.out.println("下载文件失败");
}
} catch (Exception e) {
e.printStackTrace();
}
}




/**
* Description: 向FTP服务器上传文件
* @param ip FTP服务器hostname
* @param username FTP登录账号
* @param password FTP登录密码
* @param path FTP服务器保存目录
* @param filename 上传到FTP服务器上的文件名
* @param input 输入流
* @return 成功返回true,否则返回false
*/
public static boolean uploadFile(String ip,String username, String password, String path, String filename, InputStream input){
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(ip);//连接FTP服务器
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(username, password);//登录
ftp.setFileType(FTP.BINARY_FILE_TYPE);//登陆后设置文件类型为二进制否则可能导致乱码文件无法打开
ftp.setControlEncoding("UTF-8"); //设置格式
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(path);
ftp.storeFile(filename, input);
input.close();
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
return success;
}

/**
* Description: 从FTP服务器下载文件
* @param url FTP服务器hostname
* @param username FTP登录账号
* @param password FTP登录密码
* @param remotePath FTP服务器上的相对路径
* @param fileName 要下载的文件名
* @param localPath 下载后保存到本地的路径
* @return
*/
public static boolean downFile(String url, String username, String password, String remotePath,String fileName,String localPath) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
// ftp.connect(url, port);
ftp.connect(url);//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(username, password);//登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录
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);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
return success;
}
}