123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- package com.xt.jygl.ftp;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import org.apache.commons.net.ftp.FTPClient;
- import org.apache.commons.net.ftp.FTPFile;
- import org.apache.commons.net.ftp.FTPReply;
- import org.apache.log4j.Logger;
- import com.xt.jygl.common.GlobalData;
- public class FtpUtil {
- private static Logger logger = Logger.getLogger(FtpUtil.class);
- private static FTPClient ftp;
- /**
- * 获取ftp连接
- *
- * @param f
- * @return
- * @throws Exception
- */
- public static boolean connectFtp(Ftp f) throws Exception {
- boolean flag = false;
- try {
- ftp = new FTPClient();
- ftp.connect(f.getIpAddr(), f.getPort());
- ftp.login(f.getUserName(), f.getPwd());
- ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
- int reply = ftp.getReplyCode();
- if (!FTPReply.isPositiveCompletion(reply)) {
- ftp.disconnect();
- return flag;
- }
- ftp.changeWorkingDirectory(f.getPath());
- flag = true;
- } catch (Exception e) {
- flag = false;
- logger.error(e);
- logger.error("FTP链接失败");
- }
- return flag;
- }
- /**
- * 关闭ftp连接
- */
- public static void closeFtp() {
- if (ftp != null && ftp.isConnected()) {
- try {
- ftp.logout();
- ftp.disconnect();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * ftp上传文件
- *
- * @param f
- * @throws Exception
- */
- @SuppressWarnings("finally")
- public static boolean upload(File file) {
- boolean flag = true;
- try {
- if (file.isDirectory()) {
- ftp.makeDirectory(new String(file.getName().getBytes("UTF-8"), "iso-8859-1"));
- ftp.changeWorkingDirectory(file.getName());
- String[] files = file.list();
- for (int i = 0; i < files.length; i++) {
- File file1 = new File(file.getPath() + "\\" + files[i]);
- if (file1.isDirectory()) {
- upload(file1);
- ftp.changeToParentDirectory();
- } else {
- File file2 = new File(file.getPath() + "\\" + files[i]);
- FileInputStream input = new FileInputStream(file2);
- ftp.storeFile(file2.getName(), input);
- input.close();
- }
- }
- } else {
- FileInputStream input = new FileInputStream(file);
- ftp.setControlEncoding("UTF-8");
- ftp.storeFile(new String(file.getName().getBytes("UTF-8"), "iso-8859-1"), input);
- input.close();
- }
- } catch (Exception e) {
- flag = false;
- e.printStackTrace();
- } finally {
- closeFtp();
- return flag;
- }
- }
- /**
- * 下载链接配置
- *
- * @param f
- * @param localBaseDir
- * 本地目录
- * @param remoteBaseDir
- * 远程目录
- * @throws Exception
- */
- public static boolean startDown(Ftp f, String localBaseDir, String remoteBaseDir, String filename) throws Exception {
- boolean flag = false;
- try {
- if (FtpUtil.connectFtp(f)) {
- FTPFile[] files = null;
- boolean changedir = ftp.changeWorkingDirectory(remoteBaseDir);
- if (changedir) {
- ftp.setControlEncoding("GBK");
- files = ftp.listFiles();
- for (int i = 0; i < files.length; i++) {
- if (filename.equals(files[i].getName())) {
- try {
- flag = downloadFile(files[i], localBaseDir, remoteBaseDir);
- } catch (Exception e) {
- logger.error(e);
- logger.error("<" + files[i].getName() + ">下载失败");
- }
- }
- }
- }
- } else {
- logger.error("链接失败!");
- flag = false;
- }
- } catch (Exception e) {
- flag = false;
- logger.error(e);
- logger.error("FTP下载过程中链接失败");
- } finally {
- closeFtp();
- }
- return flag;
- }
- /**
- *
- * 下载FTP文件 当你需要下载FTP文件的时候,调用此方法 根据<b>获取的文件名,本地地址,远程地址</b>进行下载
- *
- * @param ftpFile
- * @param relativeLocalPath
- * @param relativeRemotePath
- */
- private static boolean downloadFile(FTPFile ftpFile, String relativeLocalPath, String relativeRemotePath) {
- boolean flag = false;
- if (ftpFile.isFile()) {
- if (ftpFile.getName().indexOf("?") == -1) {
- OutputStream outputStream = null;
- try {
- File locaFile = new File(relativeLocalPath + ftpFile.getName());
- // 判断文件是否存在,存在则返回
- if (locaFile.exists()) {
- return true;
- } else {
- outputStream = new FileOutputStream(relativeLocalPath + ftpFile.getName());
- ftp.retrieveFile(ftpFile.getName(), outputStream);
- outputStream.flush();
- outputStream.close();
- flag = true;
- }
- } catch (Exception e) {
- logger.error(e);
- } finally {
- try {
- if (outputStream != null) {
- outputStream.close();
- }
- } catch (IOException e) {
- logger.error("输出文件流异常");
- }
- }
- }
- } else {
- String newlocalRelatePath = relativeLocalPath + ftpFile.getName();
- String newRemote = new String(relativeRemotePath + ftpFile.getName().toString());
- File fl = new File(newlocalRelatePath);
- if (!fl.exists()) {
- fl.mkdirs();
- }
- try {
- newlocalRelatePath = newlocalRelatePath + '/';
- newRemote = newRemote + "/";
- String currentWorkDir = ftpFile.getName().toString();
- boolean changedir = ftp.changeWorkingDirectory(currentWorkDir);
- if (changedir) {
- FTPFile[] files = null;
- files = ftp.listFiles();
- for (int i = 0; i < files.length; i++) {
- downloadFile(files[i], newlocalRelatePath, newRemote);
- }
- }
- if (changedir) {
- ftp.changeToParentDirectory();
- }
- } catch (Exception e) {
- logger.error(e);
- }
- }
- return flag;
- }
- public static void main(String[] args) throws Exception {
- Ftp f = new Ftp();
- f.setIpAddr("192.168.30.145");
- f.setPort(21);
- f.setUserName("test");
- f.setPwd("test");
- f.setPath(GlobalData.FTP_VIEW_PATH);
- FtpUtil.connectFtp(f);
- String path = "D:\\upload\\xkzsq";
- File file = new File(path);
- FtpUtil.upload(file);// 把文件上传在ftp上
- // FtpUtil.startDown(f, "E:\\upload\\jdtx\\", "/jdtx/",
- // "20160718164041.txt");// 下载ftp文件测试
- System.out.println("ok");
- }
- public static void makeDirFile(String path) {
- File file = new File(path);
- if (file.isDirectory()) {
- try {
- ftp.makeDirectory(new String(file.getName().getBytes("UTF-8"), "iso-8859-1"));
- ftp.changeWorkingDirectory(file.getName());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
|