这里我就简单的写一下FTP的连接,上传,下载,关闭的灵魂。至于多文件上传、下载,文件夹上传、下载等等可以在此基础上修改。
工具类代码:
package com.yj.until;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
/**
* FTP的上传下载
* @author jing
*
*/
public class FtpUtil {
public static final String FTP_IP=PropertiesUtil.get("FTP_IP");//ftp服务器地址
public static final int FTP_PORT=Integer.parseInt(PropertiesUtil.get("FTP_PORT"));//ftp 端口号
public static final String FTP_LOGIN=PropertiesUtil.get("FTP_LOGIN");//ftp服务器用户名
public static final String FTP_PASSWORD=PropertiesUtil.get("FTP_PASSWORD");//ftp服务器密码
/**
* 连接ftp
*/
public static FTPClient connectServer(){
FTPClient ftp=null;
try {
ftp= new FTPClient();
// 如果采用默认端口,可以使用client.connect(host)的方式直接连接FTP服务器
ftp.connect(FTP_IP, FTP_PORT);
// 登录
ftp.login(FTP_LOGIN,FTP_PASSWORD);
// 获取ftp登录应答码
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
System.out.println("未连接到FTP,用户名或密码错误。");
ftp.disconnect();
throw new RuntimeException("未连接到FTP,用户名或密码错误。");
}
// 2、设置连接属性
ftp.setControlEncoding("UTF-8");
// 设置以二进制方式传输
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
} catch (Exception e) {
e.printStackTrace();
}
return ftp;
}
/**
* 关闭ftp连接
* @param FTPClient ftp
*/
public static void closeServer(FTPClient ftp){
try {
ftp.disconnect();
System.out.println("disconnect success");
} catch (IOException e) {
System.out.println("not disconnect");
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* ftp上传
* 如果服务器段已存在名为filename的文件夹,该文件夹中与要上传的文件夹中同名的文件将被替换
* @param filename 要上传的文件(或文件夹)名
* @return
*/
public static boolean upLoad(String fileName,String ftpPath){
String newName="";
if(fileName.indexOf("/")>-1){
newName=fileName.substring(fileName.lastIndexOf("/")+1);
}else{
newName=fileName;
}
return upLoad(fileName,newName,ftpPath);
}
/**
* ftp上传 (就简单写一下上传文件。如多文件上传、文件夹等等在此基础上修改)
* 如果服务器段已存在名为newName的文件夹,该文件夹中与要上传的文件夹中同名的文件将被替换
* @param fileName 要上传的文件(或文件夹)名
* @param newName 服务器段要生成的文件(或文件夹)名
* @return
*/
public static boolean upLoad(String fileName,String newName,String ftpPath){
FTPClient ftp=null;
FileInputStream is=null;
// OutputStream os=null;
boolean result=false;
try {
File file_in = new File(fileName);//打开本地待长传的文件
if(!file_in.exists()){
throw new Exception("此文件或文件夹[" + file_in.getName() + "]有误或不存在!");
}
ftp=connectServer();
if(ftpPath!=null){
ftp.changeWorkingDirectory(ftpPath);
}
/****下载两种方法****/
//第一种:自带的方法
is=new FileInputStream(file_in);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
result=ftp.storeFile(new String(newName.getBytes("UTF-8"), "ISO-8859-1"), is);
//第二种:自带的方法
/* os = ftp.storeFileStream(newName);
if(os== null) throw new RuntimeException("上传失败,请检查是否有上传权限");
IOUtils.copy(new FileInputStream(file_in), os);
IOUtils.closeQuietly(os);*/
System.out.println("ftp 上传:"+result);
} catch (Exception e) {
e.printStackTrace();
}finally{
closeIO(is);
closeServer(ftp);
}
return result;
}
/**
* 下载文件
* @param FileName 下载的文件名
* @param localFilePath 本地存放的路径
*/
public static void downLoad(String fileName,String ftpFilePath,String localFilePath){
FTPClient ftp=null;
FileOutputStream os=null;
try {
ftp=connectServer();
ftp.enterLocalPassiveMode();
os=new FileOutputStream(localFilePath+"/"+fileName);
/*******FTP下载有两种方式*******/
//第一种:jar自带在方法
if(ftpFilePath!=null){
ftp.changeWorkingDirectory(new String(ftpFilePath.getBytes("UTF-8"), "ISO-8859-1"));
}
ftp.retrieveFile(fileName, os);
//第二种:输入流和输出流
/* is= ftp.retrieveFileStream(new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
} */
os.flush();
System.out.println("download success");
} catch (Exception e) {
System.out.println("download defeat");
e.printStackTrace();
}finally{
closeIO(os);
closeServer(ftp);
}
}
/**
* 关闭所有的流
* @param clsObjs
*/
public static void closeIO(Closeable ...clsObjs){
//遍历所有的流对象
for (Closeable clsObj : clsObjs) {
//关闭流对象
if(clsObj!=null){
try {
clsObj.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.err.println("关闭的对象:"+clsObj);
}
}
public static void main(String[] args) {
/*String fileName="lqk.dbf";
String localFilePath="E:/";
downLoad(fileName, null, localFilePath);*/
String fileName ="E:/新建文本文档.txt";
upLoad(fileName,null);
}
}