• 大小: 9KB
    文件类型: .java
    金币: 1
    下载: 0 次
    发布日期: 2021-06-10
  • 语言: Java
  • 标签: FTPS  FTPSClient  Android  

资源简介

FTPS是上传方法以及下载(绝对福利帖子,经过验证完全OK)!

资源截图

代码片段和文件信息

package com.qwcrom.towngasprepayment.utils;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

/**
 * FTPS上传工具类
 *
 * @author J.query
 * @date 2018/11/8
 * @email j-query@foxmail.com
 */

public class FTPSUploadUtils {
    private static FTPSClient ftpsClient;

    /**
     * @param host
     * @param port
     * @param userName       用户名
     * @param password       密码
     * @param basePath       服务器文件根目录
     * @param localFileName  文件本地地址
     * @param remoteFileName 服务器实际目录
     * @return
     * @throws IOException
     * @throws NoSuchAlgorithmException
     * @throws KeyManagementException
     */
    public static boolean upLoadFile(String host int port String userName String password String basePath String localFileName
                                     String remoteFileName) throws IOException NoSuchAlgorithmException KeyManagementException {
        boolean result = false;

        final int dataTimeout = 10000;
        final int timeout = 100000;
        X509TrustManager x509m = new X509TrustManager() {

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

            @Override
            public void checkServerTrusted(X509Certificate[] chain
                                           String authType) throws CertificateException {
            }

            @Override
            public void checkClientTrusted(X509Certificate[] chain
                                           String authType) throws CertificateException {
            }
        };
        SSLContext sslcontext = SSLContext.getInstance(“SSL“);
        sslcontext.init(null new TrustManager[]{x509m}
                new java.security.SecureRandom());
        ftpsClient = new FTPSClient(false sslcontext);

        try {
            ftpsClient.setConnectTimeout(dataTimeout);
            ftpsClient.connect(host port);
            ftpsClient.setSoTimeout(timeout);
            // Set protection buffer size
            int reply = ftpsClient.getReplyCode();
            if (FTPReply.isPositiveCompletion(reply)) {
                // Login
                if (ftpsClient.login(userName password)) {
                    //此设置关键之关键,耗费了1天时间,大坑
                    ftpsClient.execPBSZ(0);
                    ftpsClient.execPROT(“P“);
                    ftpsClient.enterLocalPassiveMode();
                    ftpsClient.setFileType(FTPClien

评论

共有 条评论