• FTPService工具类


    package com.vcredit.ddcash.server.commons.net;

    import com.vcredit.ddcash.server.commons.model.FtpParam;
    import org.apache.commons.net.ftp.*;
    import org.apache.commons.net.ftp.parser.MLSxEntryParser;
    import org.apache.commons.net.io.*;
    import org.apache.commons.net.io.SocketOutputStream;
    import org.apache.log4j.Logger;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Component;

    import java.io.*;
    import java.net.*;
    import java.nio.charset.Charset;
    import java.util.ArrayList;

    /**
    * Created by guanshuli on 2017/2/7.
    */
    @Component
    public class FTPService {
    private static final Logger logger = Logger.getLogger(FTPService.class);
    @Value("${identity.disk.save.path}")
    private String identityPath;
    @Value("${identity.ftp.userName}")
    private String userName;
    @Value("${identity.ftp.password}")
    private String password;
    @Value("${identity.ftp.server}")
    private String hostname;
    @Value("${identity.ftp.port}")
    private int port;
    public String __systemName;
    public boolean __autodetectEncoding = false;
    public int __dataConnectionMode;
    public int __fileType;
    public boolean __remoteVerificationEnabled;
    public int __controlKeepAliveReplyTimeout = 1000;
    public long __controlKeepAliveTimeout;
    public int __fileFormat;
    public int __bufferSize;
    public CopyStreamListener __copyStreamListener;
    public int receiveBufferSize = -1;
    public int sendBufferSize = -1;

    public boolean connect(FTPClient ftpClient) throws IOException {
    if (!ftpClient.isConnected()) {
    ftpClient.connect(hostname, port);
    }
    ftpClient.setConnectTimeout(50000);
    return ftpClient.login(userName, password);
    }

    public void connect(FTPClient ftpClient,String hostname, int port) throws IOException {
    ftpClient.connect(InetAddress.getByName(hostname), port);
    }


    public void connect(FTPClient ftpClient,String hostname, int port, String userName, String password) throws IOException {
    ftpClient.setConnectTimeout(50000);
    ftpClient.connect(hostname, port);
    ftpClient.login(userName, password);
    }

    static String __parsePathname(String reply) {
    String param = reply.substring(4);
    if (param.startsWith(""")) {
    StringBuilder sb = new StringBuilder();
    boolean quoteSeen = false;

    for (int i = 1; i < param.length(); ++i) {
    char ch = param.charAt(i);
    if (ch == 34) {
    if (quoteSeen) {
    sb.append(ch);
    quoteSeen = false;
    } else {
    quoteSeen = true;
    }
    } else {
    if (quoteSeen) {
    return sb.toString();
    }

    sb.append(ch);
    }
    }

    if (quoteSeen) {
    return sb.toString();
    }
    }

    return param;
    }


    public void closeFtpClient(FTPClient ftpClient) throws IOException {
    if (ftpClient.isConnected()) {
    ftpClient.disconnect();
    }
    }

    public boolean exists(FTPClient ftpClient,String fileName) throws IOException {
    return ftpClient.exists(fileName);
    }

    public boolean uploadImg(FTPClient ftpClient,String diskSavePath, FtpParam param, InputStream inputStream) throws IOException {
    return ftpClient.uploadImg(diskSavePath, param, inputStream);
    }

    public boolean rename(FTPClient ftpClient,String from, String to) throws IOException {
    return ftpClient.rename(from, to);
    }


    public boolean deleteFile(FTPClient ftpClient,String file) throws IOException {
    return ftpClient.deleteFile(file);
    }

    public FTPFile[] listFiles(FTPClient ftpClient,String rootPath) throws IOException {
    return ftpClient.listFiles(rootPath);
    }

    public boolean isFile(FTPClient ftpClient,String file) throws IOException {
    return ftpClient.isFile(file);
    }

    public boolean setFileType(FTPClient ftpClient,int fileType) throws IOException {
    return ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    }

    public boolean setFileTransferMode(FTPClient ftpClient,int fileType) throws IOException {
    return ftpClient.setFileTransferMode(fileType);
    }

    public void setControlEncoding(FTPClient ftpClient,String charset) throws IOException {
    ftpClient.setControlEncoding(charset);
    }

    public InputStream retrieveFileStream(FTPClient ftpClient,String fileName) throws IOException {
    return ftpClient.retrieveFileStream(fileName);
    }

    public boolean retrieveFileStream(FTPClient ftpClient,String remote, OutputStream local) throws IOException {
    return ftpClient.retrieveFile(remote, local);
    }

    public void removeDirectory(FTPClient ftpClient,String parentDir,
    String currentDir) throws IOException {
    ftpClient.removeDirectory(parentDir, currentDir);
    }

    public long mdtm(FTPClient ftpClient,String fileName) throws IOException {
    return ftpClient.mdtm(fileName);
    }
    }

  • 相关阅读:
    ASP.NET MVC @helper使用说明
    asp.net批量发布博客到各大博客平台
    大型网站架构学习
    Js获取日期时间及其它操作
    Asp.net 后台添加Meta标签方法
    正则表达式排除特定字符串
    asp.net正则表达式过滤标签和数据提取
    <pages validateRequest="false"/>在.net4.0中无效的问题
    IE6、IE7、IE8中overflow:hidden无效问题
    如何在 Django 中保证并发的数据一致性
  • 原文地址:https://www.cnblogs.com/muliu/p/6508447.html
Copyright © 2020-2023  润新知