ftps上传下载文件

ftps上传下载文件

该项目需要导入jar包 ftp4j-1.7.2.jar

以下为代码部分

package ftpstest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

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

import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;

public class Ftpstest {

	public static void main(String[] args) throws Exception {
		Ftpstest ftpstest = new Ftpstest();
		FTPClient client = ftpstest.getFtpsClient();
		//上传
		File file1 = new File("D:/a.txt");
		client.upload(file1);
		//下载
		File file2 = new File("D:/b.txt");
		client.download("a.txt", file2);
	}

	public FTPClient getFtpsClient() {
		try {
			TrustManager[] trustManager = new TrustManager[] { new X509TrustManager() {
				public X509Certificate[] getAcceptedIssuers() {
					return null;
				}

				public void checkClientTrusted(X509Certificate[] certs, String authType) {
				}

				public void checkServerTrusted(X509Certificate[] certs, String authType) {
				}
			} };
			String host = "192.168.234.12"; //主机名
			int port = 990; //端口号
			String username = "ftps"; //用户名
			String password = "ftps"; //密码
			SSLContext sslContext = null;
			sslContext = SSLContext.getInstance("SSL");
			sslContext.init(null, trustManager, new SecureRandom());
			SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
			FTPClient client = new FTPClient();
			client.setSSLSocketFactory(sslSocketFactory);
			client.setSecurity(FTPClient.SECURITY_FTPS);
			client.connect(host, port);
			client.login(username, password);
			return client;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
}

所需jar包,提取码:5n5i
ftp4j-1.7.2.jar

你可能感兴趣的:(ftps上传下载文件)