
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
package club.jiajiajia.bulider.service;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
/**
* ftp服务
* @author JIA_JIAJIA
* @website http://www.jiajiajia.club
* @da2019年6月3日
*
*/
@Service
public class FTPServer {
@Value("${ftp.ip}")
private String ip;
@Value("${ftp.port}")
private Integer port;
@Value("${ftp.username}")
private String username;
@Value("${ftp.password}")
private String password;
public boolean uploadFile( String basePath,
String filePath, String filename, InputStream input) {
FTPClient ftp= new FTPClient();
try {
ftp.connect(ip, port);// 连接FTP服务器
ftp.login(username, password);// 登录
} catch (SocketException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
boolean result = false;
try {
int reply;
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
//切换到上传目录
if (!ftp.changeWorkingDirectory(basePath+filePath)) {
//如果目录不存在创建目录
String[] dirs = filePath.split("/");
String tempPath = basePath;
for (String dir : dirs) {
if (null == dir || "".equals(dir)) continue;
tempPath += "/" + dir;
if (!ftp.changeWorkingDirectory(tempPath)) {
if (!ftp.makeDirectory(tempPath)) {
return result;
} else {
ftp.changeWorkingDirectory(tempPath);
}
}
}
}
ftp.setControlEncoding("ISO-8859-1");//设置编码集为GBK支持中文
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
conf.setServerLanguageCode("zh");
ftp.configure(conf);
//设置上传文件的类型为二进制类型
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();// 设为被动模式
filename= new String(filename.getBytes("GBK"),"ISO-8859-1");
String [] strs = ftp.listNames();
boolean b = true;
for (String str : strs){
if(str != null && str.equals(filename)){
b = false;
break;
}
}
if(b){
if (!ftp.storeFile(filename, input)) {
return result;
}
}
input.close();
result = true;
ftp.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.logout();
ftp.disconnect();
} catch (IOException e) {
}
}
}
return result;
}
public boolean deleteFile(String pathname,String filename) {
FTPClient ftp= new FTPClient();
try {
ftp.connect(ip, port);// 连接FTP服务器
ftp.login(username, password);// 登录
} catch (SocketException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
boolean flag = false;
try {
int replyCode = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
return flag;
}
// 切换FTP目录
ftp.changeWorkingDirectory(pathname);
ftp.dele(filename);
flag = true;
ftp.logout();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.logout();
ftp.disconnect();
} catch (IOException e) {
}
}
}
return flag;
}
}
@Value("${ftp.ip}")
private String ip;
@Value("${ftp.port}")
private Integer port;
@Value("${ftp.username}")
private String username;
@Value("${ftp.password}")
private String password;
分别是 ip地址,端口号,用户名,密码。