概述:
本文章主要是基于内容发布,通过FTPClient 将文件复制到另一台机器涉及多线程调用,和怎么上传文件和文件夹,及FTPClient 中的语法说明。
1.用到的jar
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
2. 构造方法
public class ftpsynchronization {
private FTPClient ftp;
private final int port = 21;
private ThreadLocal<ftpsynchronization> ftpLocal = new ThreadLocal<ftpsynchronization>();
public void connectFtp(Map<String,Object> map) throws Exception {
ftp = new FTPClient();
int reply;
Integer targetPort = CMSUtil.intFormat(CMSUtil.stringFormat(map.get("targetPort")));
if (targetPort==0) {
ftp.connect(CMSUtil.stringFormat(map.get("targetServer")),port);
} else {
ftp.connect(CMSUtil.stringFormat(map.get("targetServer")), targetPort);
}
ftp.login(CMSUtil.stringFormat(map.get("loginUser")), CMSUtil.stringFormat(map.get("loginPwd")));
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
}
}
public boolean isChannel() {
try {
if (ftp.isConnected()) {
return true;
}
} catch (Exception e) {
logger.info(e.getMessage());
}
return false;
}
public void closeFtp(ftpsynchronization ftpUtil) {
if (ftpUtil.isChannel()) {
ftpLocal.set(null);
if (ftp != null && ftp.isConnected()) {
try {
ftp.logout();
ftp.disconnect();
} catch (IOException e) {
}
}
}
}
public static boolean systemtype() {
boolean flag = false;
String os = System.getProperty("os.name");
if (os.toLowerCase().startsWith("win")) {
flag = true;
}
return flag;
}
private ftpsynchronization(Map<String,Object> map) throws Exception {
super();
connectFtp(map);
}
public ftpsynchronization getftpUtil(Map<String,Object> map) throws Exception {
ftpsynchronization ftpUtil = ftpLocal.get();
if (null == ftpUtil || !ftpUtil.isChannel()) {
ftpLocal.set(new ftpsynchronization(map));
}
return ftpLocal.get();
}
public void fileupload(File file, String url) throws Exception {
FileInputStream input = new FileInputStream(file);
boolean b = ftp.storeFile(new String((url + file.getName()).getBytes("UTF-8"), ftp.DEFAULT_CONTROL_ENCODING), input);
if (!b) {
logger.error("文件上传:" + b + ",上传文件指向路径错误:" + url);
}
input.close();
}
private void uploadDirectory(File file,String url) throws Exception{
ftp.changeWorkingDirectory(url);
if(file.isDirectory()){
ftp.makeDirectory(file.getName());
ftp.changeWorkingDirectory(file.getName());
String[] files = file.list();
for (int i = 0; i < files.length; i++) {
File file1 = new File(file.getPath()+"\\"+files[i] );
if(file1.isDirectory()){
uploadDirectory(file1,url+"/"+file1.getParent());
ftp.changeToParentDirectory();
}else{
File file2 = new File(file.getPath()+"\\"+files[i]);
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
}
}
}
public static void publishdistri(Map<String, Object> map, String localFile) {
ftpsynchronization ftpUtil=null;
try {
String dataPath = CMSUtil.stringFormat(map.get("dataPath"));
String newdatapah =dataPath+localFile.split("pub")[1];
ftpsynchronization ftp = new ftpsynchronization();
ftpUtil = ftp.getftpUtil(map);
ftpUtil.copyFileNew(newdatapah, localFile);
} catch (Exception e) {
logger.error("仅发布站点/栏目首页FTP站分发错误"+e.getMessage());
} finally {
ftpUtil.closeFtp(ftpUtil);
}
}
public void copyFileNew(String newFilePath, String url) throws Exception {
File file = new File(url);
if(file.exists()) {
ftp.enterLocalPassiveMode();
String[] dirs = null;
String realPath = "";
if (systemtype()) {
newFilePath = newFilePath.replace("/", "\\");
realPath = newFilePath.substring(0, newFilePath.lastIndexOf("\\") + 1);
dirs = realPath.split("\\\\");
} else {
newFilePath = newFilePath.replace("\\", "/");
realPath = newFilePath.substring(0, newFilePath.lastIndexOf("/") + 1);
dirs = realPath.split("/");
}
String tempPath = "";
for (String dir : dirs) {
tempPath = dir;
boolean changeWorkingDirectory = ftp.changeWorkingDirectory(new String(tempPath.getBytes("UTF-8"), ftp.DEFAULT_CONTROL_ENCODING));
if (!changeWorkingDirectory) {
if (null == dir || "".equals(dir)) {
continue;
} else {
boolean makeDirectory = ftp.makeDirectory(tempPath);
if (!makeDirectory) {
logger.error("创建文件夹失败!");
}
boolean WorkingDirectory = ftp.changeWorkingDirectory(new String(tempPath.getBytes("UTF-8"), ftp.DEFAULT_CONTROL_ENCODING));
if (!WorkingDirectory) {
logger.error("进入当前目录失败!");
}
}
} else {
continue;
}
}
ftp.changeWorkingDirectory("/");
fileupload(new File(url), realPath);
}else{
logger.info("文件"+url+"找不到,FTP站点分发失败");
}
}
}