win10本地ftp服务器搭建
参考:https://blog.csdn.net/Ilson_/article/details/97818689
本地用户不要用ftp
本地使用jdk1.8环境,实现ftp文件的下载,测试代码实现单个文件或者多个文件的下载到本地。
使用pom.xml导入jar
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.8.0</version>
</dependency>
package org.jeecg.modules.ain.util;
import java.io.*;
import java.net.SocketException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.google.api.client.util.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
/**
* ftp工具方法 使用commons-net包
*/
@Slf4j
public class FtpUtil {
/**
* 本地字符编码
*/
private static String LOCAL_CHARSET = "GBK";
// FTP协议里面,规定文件名编码为iso-8859-1
private static String SERVER_CHARSET = "ISO-8859-1";
/**
* ftp链接
*
* @param ip ftp地址
* @param port ftp端口
* @param username 账号
* @param password 密码
* @return
* @throws IOException
*/
public static FTPClient ftpConnection(String ip, String port, String username, String password) throws IOException {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(ip, Integer.parseInt(port));
ftpClient.login(username, password);
log.info("FTP服务器字符类型init:" + ftpClient.getCharset().name());
if (FTPReply.isPositiveCompletion(ftpClient.sendCommand(
"OPTS UTF8", "ON"))) {// 开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就使用本地编码(GBK).
LOCAL_CHARSET = "UTF-8";
}
int replyCode = ftpClient.getReplyCode(); //是否成功登录服务器
if (!FTPReply.isPositiveCompletion(replyCode)) {
ftpClient.disconnect();
log.error("--ftp连接失败--");
System.exit(1);
}
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);//解决linux服务器下载文件大小不一致的问题
ftpClient.enterLocalPassiveMode();//这句最好加告诉对面服务器开一个端口
log.info("FTP服务器字符类型:" + ftpClient.getCharset().name());
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return ftpClient;
}
/**
* 下载方法(支持原文件带路径)
*
* @param ftpClient FTPClient对象
* @param ftpFileName 原文件名
* @param pathDir 本地路径
* @return 本地文件路径
* @throws IOException
*/
public static String ftpDownload(FTPClient ftpClient, String ftpFileName, String pathDir) throws IOException {
//创建本地文件
String localFilePath = pathDir + "/" + ftpFileName;
File localFile = new File(localFilePath);
//根目录
/*FTPFile[] ftpFiles = ftpClient.listDirectories();
for (int i = 0; i < ftpFiles.length; i++) {
log.info((i + 1) + "目录:" + ftpFiles[i].getName());
}*/
//通过ftpFileName判断获取目录
String directory = "/";//缺省根目录
if (ftpFileName.contains("/")) {
directory = ftpFileName.substring(0, ftpFileName.lastIndexOf("/"));
ftpFileName = ftpFileName.substring(ftpFileName.lastIndexOf("/") + 1);
log.info("子目录:" + directory);
directory = new String(directory.getBytes(LOCAL_CHARSET), SERVER_CHARSET);
log.info("子目录 转码:" + directory);
}
ftpClient.changeWorkingDirectory(directory);
log.info("当前工作目录:" + ftpClient.printWorkingDirectory());
/*FTPFile[] files = ftpClient.listFiles();
for (FTPFile file : files) {
log.info("文件名称:" + file.getName());
log.info("文件名称 转码:" + new String(file.getName().getBytes(SERVER_CHARSET), LOCAL_CHARSET));
}
log.info("ftpFileName:" + ftpFileName);
log.info("ftpFileName:" + new String(ftpFileName.getBytes(LOCAL_CHARSET), SERVER_CHARSET));*/
InputStream inputStream = ftpClient.retrieveFileStream(new String(ftpFileName.getBytes(LOCAL_CHARSET), SERVER_CHARSET));
FileUtils.copyInputStreamToFile(inputStream, localFile);
inputStream.close();
return localFilePath;
}
/**
* 获取文件方法(支持原文件带路径)
*
* @param ftpClient FTPClient对象
* @param ftpFileName 原文件名
* @param pathDir 本地路径
* @return 文件对象
* @throws IOException
*/
public static File getFtpFile(FTPClient ftpClient, String ftpFileName, String pathDir) throws IOException {
//创建本地文件
String localFilePath = pathDir + "/" &