ftp上传

package com.br.sftpclient;

import com.br.sftpUtil.PropertiesUtil;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import sun.rmi.runtime.Log;

import java.io.*;
import java.net.MalformedURLException;
import java.util.logging.Logger;

/**
* @Company:
* @Author: shaobin.fu
* @Date: 2018/12/11 14:27
* @Description:
*/
public class UpToFtp2 {
private transient org.slf4j.Logger log = LoggerFactory.getLogger(this.getClass());

private FTPClient ftp;

/**
* @param path 上传到ftp服务器哪个路径下
* @param addr 地址
* @param port 端口号
* @param username 用户名
* @param password 密码
* @return
* @throws Exception
*/
public boolean connect(String path, String addr, int port, String username, String password) throws Exception {
boolean result = false;
log.info("start connect.....");
ftp = new FTPClient();
int reply;
ftp.connect(addr, port);
ftp.login(username, password);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
log.info("ftp has been connected.....");
CreateDirecroty(path);
ftp.changeWorkingDirectory(path);
result = true;
return result;
}
//改变目录路径
public boolean changeWorkingDirectory(String directory) {
boolean flag = true;
try {
flag = ftp.changeWorkingDirectory(directory);
if (flag) {
log.info("enter " + directory + " success!");

} else {
log.info("enter " + directory + " failed!start create directory!");
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
return flag;
}
//判断ftp服务器文件是否存在
public boolean existFile(String path) throws IOException {
boolean flag = false;
FTPFile[] ftpFileArr = ftp.listFiles(path);
if (ftpFileArr.length > 0) {
flag = true;
}
return flag;
}
//创建目录
public boolean makeDirectory(String dir) {
boolean flag = true;
try {
flag = ftp.makeDirectory(dir);
if (flag) {
log.info("create directory " + dir + " success!");

} else {
log.info("create directory " + dir + " failed!");
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
//创建多层目录文件,如果有ftp服务器已存在该文件,则不创建,如果无,则创建
public boolean CreateDirecroty(String remote) throws IOException {
boolean success = true;
String directory = remote + "/";
// 如果远程目录不存在,则递归创建远程服务器目录
if (!directory.equalsIgnoreCase("/") && !changeWorkingDirectory(new String(directory))) {
int start = 0;
int end = 0;
if (directory.startsWith("/")) {
start = 1;
} else {
start = 0;
}
end = directory.indexOf("/", start);
String path = "";
String paths = "";
while (true) {
String subDirectory = new String(remote.substring(start, end).getBytes("GBK"), "iso-8859-1");
path = path + "/" + subDirectory;
if (!existFile(path)) {
if (makeDirectory(subDirectory)) {
changeWorkingDirectory(subDirectory);
} else {
log.info("create directory " + subDirectory + " failed!");
changeWorkingDirectory(subDirectory);
}
} else {
changeWorkingDirectory(subDirectory);
}

paths = paths + "/" + subDirectory;
start = end + 1;
end = directory.indexOf("/", start);
// 检查所有目录是否创建完毕
if (end <= start) {
break;
}
}
}
return success;
}

/**
* @param file 上传的文件或文件夹
* @throws Exception
*/
public void upload(File file) throws Exception {
log.info("start upload,please waiting....");
if (file.isDirectory()) {
log.info("start upload,start upload directory....");
//ftp.makeDirectory(file.getName());
CreateDirecroty(file.getName());
ftp.changeWorkingDirectory(file.getName());
log.info("current pathname is:{}",file.getPath());
String[] files = file.list();
for (int i = 0; i < files.length; i++) {
log.info("file path:{}",file.getPath());
File file1 = new File(file.getPath() + "\\" + files[i]);
if (file1.isDirectory()) {
upload(file1);
ftp.changeToParentDirectory();
} else {
File file2 = new File(file.getPath() + "\\" + files[i]);
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
}
} else {
log.info("start upload,start upload file,wait a minute....");
File file2 = new File(file.getPath());
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
}

public static void main(String[] args) throws Exception {
UpToFtp2 t = new UpToFtp2();
t.connect("/huadong_fengkong/dir-name/", PropertiesUtil.getStringValue("hostname"), PropertiesUtil.getIntegerValue("port"),
PropertiesUtil.getStringValue("username"), PropertiesUtil.getStringValue("password"));
//String pathname = !StringUtils.isEmpty(args[0])?args[0]:PropertiesUtil.getStringValue("txt_path");
String pathname="E:\\opt\\SpringCloud";
File file = new File(pathname);
t.upload(file);
}
}
posted @ 2018-12-14 15:05  Call_Me_Tiger_Fu  阅读(294)  评论(0编辑  收藏  举报