packagecom.zskj.pd.util;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.nio.charset.Charset;importjava.text.SimpleDateFormat;importjava.util.Date;importorg.apache.commons.net.ftp.FTPClient;importorg.apache.commons.net.ftp.FTPFile;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importcom.zskj.pd.pojo.FtpEntity;public classftpUtil {private final static Logger LOG = LoggerFactory.getLogger(ftpUtil.class);public static voidmain(String args[]) {
FtpEntity m= new FtpEntity("100.80.10.38", 21, "test", "test");
FTPClient fTPClient=login(m);
uploadFile(fTPClient,"6666666666", "doc", "2.bat", "D:\\otherFiles");
}//验证登录
public staticFTPClient login(FtpEntity ftpEntity) {
FTPClient ftp= null;try{
ftp= newFTPClient();
ftp.connect(ftpEntity.getIp(), ftpEntity.getPort());
ftp.login(ftpEntity.getName(), ftpEntity.getPwd());
ftp.setCharset(Charset.forName("UTF-8"));
ftp.setControlEncoding("UTF-8");
LOG.info(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+" 登录成功");
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}returnftp;
}//获取ftp某一文件(路径)下的文件名字,用于查看文件列表
public static voidgetFilesName(FTPClient fTPClient) {try{//获取ftp里面,“Windows”文件夹里面的文件名字,存入数组中
FTPFile[] files = fTPClient.listFiles("/Windows");//打印出ftp里面,“Windows”文件夹里面的文件名字
for (int i = 0; i < files.length; i++) {
System.out.println(files[i].getName());
}
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}/***
*@paramtargetFileName
*@paramtargetPath
*@paramsourceFileName
*@paramsourcePath*/
public static voiduploadFile(FTPClient fTPClient,String targetFileName,String targetPath,String sourceFileName,String sourcePath) {try{
File sfp= new File(sourcePath+"\\"+sourceFileName);
fTPClient.storeFile(targetPath+"\\"+targetFileName, newFileInputStream(sfp));
LOG.info(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+" 上传完成");
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}/***
*@paramtargetFileName
*@paramtargetPath
*@paramsourceFileName
*@paramsourcePath*/
public static voiduploadFile2(FTPClient fTPClient,String targetFileName,String targetPath,String sourceFileName,String sourcePath) {
OutputStream os= null;
FileInputStream fis= null;try{
os= fTPClient.storeFileStream(targetPath+"\\"+targetFileName);
fis= new FileInputStream(new File(sourcePath+"\\"+sourceFileName));byte[] b = new byte[1024];int len = 0;while ((len = fis.read(b)) != -1) {
os.write(b,0, len);
}
os.flush();
LOG.info(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+" 上传完成");
}catch(IOException e) {
e.printStackTrace();
}finally{try{
os.close();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}try{
fis.close();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}/***
*@paramtargetFileName
*@paramtargetPath
*@paramsourceFileName
*@paramsourcePath*/
public static voiddownloadFile(FTPClient fTPClient,String targetFileName,String targetPath,String sourceFileName,String sourcePath) {try{//将ftp根目录下的"jsoup-1.10.2.jar"文件下载到本地目录文件夹下面,并重命名为"1.jar"
fTPClient.retrieveFile(sourcePath+"\\"+sourceFileName, new FileOutputStream(new File(targetPath+"\\"+targetFileName)));
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}/***
*@paramtargetFileName
*@paramtargetPath
*@paramsourceFileName
*@paramsourcePath*/
public static voiddownloadFile2(FTPClient fTPClient,String targetFileName,String targetPath,String sourceFileName,String sourcePath) {
InputStream is= null;
FileOutputStream fos= null;try{
is= fTPClient.retrieveFileStream(sourcePath+"\\"+sourceFileName);
fos= new FileOutputStream(new File(targetPath+"\\"+targetFileName));byte[] b = new byte[1024];int len = 0;while ((len = is.read(b)) != -1) {
fos.write(b,0, len);
}
fos.flush();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}finally{try{
fos.close();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}try{
is.close();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}