• 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.wrt.zjg.webservices.model.FtpModel;

    public class FtpUtil {

    private static Logger logger = Logger.getLogger(FtpUtil.class);

    private static FTPClient ftp;

    /**
    * 获取ftp连接
    * @param fm
    * @return
    * @throws Exception
    */
    public static boolean connectFtp(FtpModel fm) throws Exception{
    ftp = new FTPClient();
    boolean flag = false;
    int reply;
    if(fm.getPort() == null) {
    ftp.connect(fm.getIpAddr(), 21);
    } else {
    ftp.connect(fm.getIpAddr(), fm.getPort());
    }
    ftp.login(fm.getUserName(), fm.getPwd());
    ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
    reply = ftp.getReplyCode();
    if(!FTPReply.isPositiveCompletion(reply)) {
    ftp.disconnect();
    return flag;
    }
    ftp.changeWorkingDirectory(fm.getPath());
    flag = true;
    return flag;
    }

    /**
    * 关闭ftp连接
    */
    public static void closeFtp() {
    if(ftp != null && ftp.isConnected()) {
    try {
    ftp.logout();
    ftp.disconnect();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

    /**
    * ftp上传文件
    * @param file
    * @throws Exception
    */
    public static void upload(File file) throws Exception {
    if(file.isDirectory()) {
    ftp.makeDirectory(file.getName());
    ftp.changeWorkingDirectory(file.getName());
    String[] files = file.list();
    for(String fstr : files) {
    File file1 = new File(file.getPath() + "/" + fstr);
    if(file1.isDirectory()) {
    upload(file1);
    ftp.changeToParentDirectory();
    } else {
    File file2 = new File(file.getPath() + "/" + fstr);
    FileInputStream input = new FileInputStream(file2);
    ftp.storeFile(file2.getName(), input);
    input.close();
    }
    }
    } else {
    File file2= new File(file.getPath());
    FileInputStream input = new FileInputStream(file2);
    ftp.storeFile(file2.getName(), input);
    input.close();
    }
    }

    /**
    *
    * @param fm 将要上传的文件
    * @param localBaseDir 本地目录
    * @param remoteBaseDir 远程目录
    * @throws Exception
    */
    public static void startDown(FtpModel fm, String localBaseDir, String remoteBaseDir) throws Exception {
    if(FtpUtil.connectFtp(fm)) {
    try {
    FTPFile[] files = null;
    boolean changedir = ftp.changeWorkingDirectory(remoteBaseDir);
    if(changedir) {
    ftp.setControlEncoding("GBK");
    files = ftp.listFiles();
    for(int i = 0; i < files.length; i++) {
    try {
    downloadFile(files[i], localBaseDir, remoteBaseDir);
    } catch (Exception e) {
    logger.error(e);
    logger.error("<"+files[i].getName()+">下载失败");
    }
    }
    }

    } catch (Exception e) {
    logger.error(e);
    logger.error("下载过程中出现异常!");
    }
    } else {
    logger.error("链接失败!");
    }
    }

    /**
    * 下载FTP文件
    * 当你需要下载FTP文件的时候,调用此方法
    * 根据<b>获取的文件名,本地地址,远程地址</b>进行下载
    * @param ftpFile
    * @param relativeLocalPath
    * @param relativeRemotePath
    */
    public static void downloadFile(FTPFile ftpFile, String relativeLocalPath, String relativeRemotePath) {
    if(ftpFile.isFile()) {
    if(ftpFile.getName().indexOf("?") == -1) {
    OutputStream outputStream = null;
    try {
    File locaFile = new File(relativeLocalPath + ftpFile.getName());
    if(locaFile.exists()) {
    return;
    } else {
    outputStream = new FileOutputStream(relativeLocalPath + ftpFile.getName());
    ftp.retrieveFile(ftpFile.getName(), outputStream);
    outputStream.flush();
    outputStream.close();
    }
    } 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 (relativeLocalPath + ftpFile.getName().toString());
    File file = new File(newLocalRelatePath);
    if(!file.exists()) {
    file.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);
    }
    }
    }

    public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
    FtpModel fm = new FtpModel();
    fm.setIpAddr("192.168.1.252");
    fm.setUserName("ftp");
    fm.setPwd("ftp12345");
    fm.setPath("/temp/zjg/messageXML/");
    FtpUtil.connectFtp(fm);
    File file = new File("E:/test/959663081-20170606154839294.xml");
    FtpUtil.upload(file);

    }

    }

  • 相关阅读:
    PHPCMS网站关站了打不开-站长真的凉了吗?
    PHPCMS倒闭关站后,国内CMS系统该何去何从
    企业网站建设如何选择cms建站系统
    网站建设之常用CMS系统的SEO优化特点总结
    PageAdmin CMS仿站教程,如此简单就可以自己建网站
    c#之lamda表达式的前世今生
    c#之Linq的原理讲解及封装自己的Linq
    三大CMS建站系统助你免费建网站
    网站建设的完整流程来了,新手必看
    从零自学Java-7.使用数组存储信息
  • 原文地址:https://www.cnblogs.com/vofill/p/6953266.html
Copyright © 2020-2023  润新知