作为经常使用电脑整理文件的童鞋,应该都使用过从ftp服务器上传下载文件,那么今天就了解下如何通过java程序操作ftp服务的文件
首先你要知道ftp的ip,路径,端口,有操作权限的账号和密码
commons-net-3.6.jar
这个jar包用来设置编码,经过测试,不加也可用
/**
* 验证登录
* @param ip
* @param port
* @param name
* @param pwd
* @return
*/
public boolean login(String ip,int port,String name,String pwd){
try{
ftp =newFTPClient();
ftp.connect(ip, port);
System.out.println(ftp.login(name, pwd));
if(!ftp.login(name, pwd)){
returnfalse;
}
ftp.setCharset(Charset.forName("UTF-8"));
ftp.setControlEncoding("UTF-8");
}catch(IOException e){
e.printStackTrace();
returnfalse;
}
returntrue;
}
注意:获取远程文件目录,上传和下载方法基于登陆方法
2.2 获取远程文件目录
/**
* 获取ftp某一文件(路径)下的文件名字,用于查看文件列表
* @param ip
* @param port
* @param name
* @param pwd
* @param remotedir 远程地址目录
* @return
*/
publicboolean getFilesName(String ip,int port,String name,String pwd,String remotedir){
try{
if(!login(ip, port, name, pwd)){
returnfalse;
}
//获取ftp里面,指定文件夹 里面的文件名字,存入数组中
FTPFile[] files = ftp.listFiles(remotedir);
//打印出ftp里面,指定文件夹 里面的文件名字
for(int i =0; i < files.length; i++){
System.out.println(files[i].getName());
}
}catch(IOException e){
e.printStackTrace();
returnfalse;
}finally{
this.close();
}
returntrue;
}
/**
* 上传文件 方法一
* @param ip
* @param port
* @param name
* @param pwd
* @param remotepath 远程地址文件路径
* @param localpath 本地文件路径
* @return
*/
publicboolean putFileOne(String ip,int port,String name,String pwd,String remotepath,String localpath){
try{
if(!login(ip, port, name, pwd)){
returnfalse;
}
//将本地的 localpath 文件上传到ftp的根目录文件夹下面,并重命名为 remotepath中的名字
return ftp.storeFile(remotepath,newFileInputStream(newFile(localpath)));
}catch(IOException e){
e.printStackTrace();
returnfalse;
}finally{
this.close();
}
}
/**
* 上传文件的第二种方法,优化了传输速度
* @param ip
* @param port
* @param name
* @param pwd
* @param remotepath 远程地址文件路径
* @param localpath 本地文件路径
* @return
*/
publicboolean putFileTwo(String ip,int port,String name,String pwd,String remotepath,String localpath){
try{
if(!login(ip, port, name, pwd)){
returnfalse;
}
os = ftp.storeFileStream(remotepath);
fis =newFileInputStream(newFile(localpath));
byte[] b =newbyte[1024];
int len =0;
while((len = fis.read(b))!=-1){
os.write(b,0,len);
}
}catch(IOException e){
e.printStackTrace();
returnfalse;
}finally{
this.close();
}
returntrue;
}
/**
* 下载文件 方法一
* @param ip
* @param port
* @param name
* @param pwd
* @param remotepath 远程地址文件路径
* @param localpath 本地文件路径
* @return
*/
publicboolean getFileOne(String ip,int port,String name,String pwd,String remotepath,String localpath){
try{
if(!login(ip, port, name, pwd)){
returnfalse;
}
//将ftp资源中 remotepath 文件下载到本地目录文件夹下面,并重命名为 localpath 中的名字
return ftp.retrieveFile(remotepath,newFileOutputStream(newFile(localpath)));
}catch(IOException e){
e.printStackTrace();
returnfalse;
}finally{
this.close();
}
}
/**
* 下载文件的第二种方法,优化了传输速度
* @param ip
* @param port
* @param name
* @param pwd
* @param remotepath 远程地址文件路径
* @param localpath 本地文件路径
* @return
*/
publicboolean getFileTwo(String ip,int port,String name,String pwd,String remotepath,String localpath){
try{
if(!login(ip, port, name, pwd)){
returnfalse;
}
is = ftp.retrieveFileStream(remotepath);
fos =newFileOutputStream(newFile(localpath));
byte[] b =newbyte[1024];
int len =0;
while((len = is.read(b))!=-1){
fos.write(b,0,len);
}
}catch(IOException e){
e.printStackTrace();
returnfalse;
}finally{
this.close();
}
returntrue;
}
当然上面代码只是重要的部分,如果有问题可去github自行下载 charmsongo
如果有什么更好的方法欢迎留言