1. 引入jar
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
2. 上传 下载 类
package com.mv.common.ftp;
import com.mv.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import java.io.*;
import java.net.SocketException;
@Slf4j
public class FtpConfig {
private static FTPClient ftpClientCommon = null;
public static FTPClient ftpClient(){
if(null != ftpClientCommon){
return ftpClientCommon;
}
FTPClient ftpClient = new FTPClient();
//设置连接超时时间
ftpClient.setConnectTimeout(30000);
ftpClient.setControlEncoding("utf-8");
//设置被动模式,文件传输端口设置 通知服务器开通给一个端口,防止挂死
ftpClient.enterLocalPassiveMode();
try {
ftpClient.setDefaultPort(21);
ftpClient.connect("192.168.9.100", FTP.DEFAULT_PORT);
ftpClient.login("root","root");
int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)){
ftpClient.disconnect();
log.error("connect error");
return null;
}else {
log.info("connect success");
ftpClientCommon = ftpClient;
ftpClient.changeWorkingDirectory("/");
//设置ftp字符集二进制 默认ASCII时 上传文件损坏
try {
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
} catch (IOException e) {
e.printStackTrace();
return null;
}
return ftpClient;
}
} catch (SocketException e) {
log.error(e.getMessage());
return null;
} catch (IOException e) {
log.error(e.getMessage());
return null;
}
}
/**
* 上传文件
*/
private static void uploadFile() {
FTPClient ftpClient = ftpClient();
if (ftpClient == null){
return;
}
try {
FTPFile[] ftpFiles = ftpClient.listFiles("/");
Boolean flag = false;
//遍历当前目录下的文件,判断要读取的文件是否在当前目录下
for (FTPFile ftpFile:ftpFiles){
if (ftpFile.getName().equals("LinuxBAMS.db")){
flag = true;
}
}
if(flag){
FileOutputStream outputStream = new FileOutputStream(new File("d:/1/" + DateUtils.getCurrentDateyyyyMMddHHmmss() + ".db"));
ftpClient.retrieveFile(new String("/LinuxBAMS.db".getBytes("utf-8"),"ISO-8859-1"),outputStream);
outputStream.flush();
outputStream.close();
log.info("success");
}else {
log.info("error");
}
ftpClient.logout();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (ftpClient.isConnected()){
try {
ftpClient.disconnect();
} catch (IOException e) {
log.error("disconnect fail ------->>>{}",e.getCause());
}
}
}
}
/**
* 上传文件
* @param inputStream 输入流
* @param originName 文件名
* @param remoteDir 根目录 "/"
*/
private static boolean uploadFile(InputStream inputStream, String originName, String remoteDir){
FTPClient ftpClient = FtpConfig.ftpClient();
if (ftpClient == null){
return false;
}
try {
Boolean isSuccess = ftpClient.storeFile(originName,inputStream);//保存文件
if(isSuccess){
log.info("upload success");
}else {
log.info("upload error!");
}
ftpClient.logout();
return true;
} catch (IOException e) {
log.error(e.getMessage());
return false;
}finally {
if (ftpClient.isConnected()){
try {
ftpClient.disconnect();
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
}
public static void main(String[] args) throws Exception{
File f = new File("d:/xx/xx.db");
FileInputStream inputStream = new FileInputStream(f);
uploadFile(inputStream,"xx.db","/");
}
}
最后
以上就是调皮大白最近收集整理的关于Ftp Client 上传 下载 文件的全部内容,更多相关Ftp内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复