use of org.apache.commons.net.ftp.FTPClient in project blog by liqianggh.
the class FTPUtil method connectFtpServer.
/**
* @Description:连接文件服务器(使用默认端口)
* Created by Jann Lee on 2018/1/18 18:54.
*/
public boolean connectFtpServer(String ip, String username, String password, Integer port) {
ftpClient = new FTPClient();
boolean flag = false;
try {
ftpClient.connect(ip);
// 登录
flag = ftpClient.login(username, password);
log.info("登录/连接服务器结果:" + flag);
} catch (IOException e) {
log.error("连接/登录文件服务器报错!", e);
}
return flag;
}
use of org.apache.commons.net.ftp.FTPClient in project assembly64fx by freabemania.
the class FTPService method getFileInternal.
private File getFileInternal(String remote, String file, File target, boolean updateGui) throws Exception {
LOGGER.info("Fetching file " + file);
CancelableTask cancelTask = CancelableTask.of();
boolean isUpgrading = GlobalRepoService.getInstance().contains("upgrading");
FTPClient client = getNewClient();
try {
String remotePath = remote + "/" + file;
long filesize = Arrays.stream(client.listFiles(remote)).filter(item -> item.getName().equals(file)).findFirst().get().getSize();
long ctr = 0;
OutputStream outputStream2 = new BufferedOutputStream(new FileOutputStream(target));
InputStream inputStream = client.retrieveFileStream(remotePath);
byte[] bytesArray = new byte[8192];
int bytesRead = -1;
int updateGuiCtr = 0;
while ((bytesRead = inputStream.read(bytesArray)) != -1 && cancelTask.isRunning()) {
outputStream2.write(bytesArray, 0, bytesRead);
ctr += bytesRead;
if (updateGui && !isUpgrading) {
if (updateGuiCtr % 50 == 0) {
double finished = (double) ((double) (((double) ctr / (double) filesize)));
ProgressControlWrapper.getInstance().setProgressLabel("Fetching file " + target.getName() + " ( " + df2.format((double) (finished * 100)).replace(",", ".") + "% )");
ProgressControlWrapper.getInstance().increaseCurrentProgressStep(finished);
updateGuiCtr = 0;
}
updateGuiCtr++;
}
}
outputStream2.close();
inputStream.close();
if (!cancelTask.isCancelled()) {
client.completePendingCommand();
if (updateGui) {
ProgressControlWrapper.getInstance().setProgressLabel("Downloaded file " + target.getName(), true);
}
} else {
if (updateGui) {
ProgressControlWrapper.getInstance().setProgressLabel("Cleaning up, please wait");
}
FileUtils.forceDelete(target);
}
return target;
} finally {
registerDownload(file, DOWNLOAD_STATUS.REMOVE);
close(client);
}
}
use of org.apache.commons.net.ftp.FTPClient in project javautils by jiadongpo.
the class FTPtest method main.
public static void main(String[] args) throws IOException {
FTPUtil ftp = FTPFactory.getInstance("FTPTest");
String remoteFile = "test/22.txt";
ftp.downLoad(remoteFile);
// PrintUtil.print(ftp.listFile("./"));
System.out.println("远程文件是否存在:" + ftp.isExists(remoteFile));
System.out.println("获取当前的工作目录:" + ftp.getWorkDir());
// ftp.downLoadDir("");
System.out.println("创建目录:" + ftp.mkDir("test01"));
System.out.println("创建目录:" + ftp.mkDir("test02/"));
System.out.println("创建目录:" + ftp.mkDir("test03/test1"));
System.out.println("创建目录:" + ftp.mkDir("test04/test1/"));
String uploadFile = "c:/windows/system.ini";
System.out.println("上传文件:" + ftp.putFile(uploadFile, "windows/system.ini", true));
String deleteFile = "22.txt";
System.out.println("删除远程文件:" + ftp.deleteFile(deleteFile));
String deleteDir = "checkbox";
System.out.println("删除远程目录:" + ftp.deleteDir(deleteDir));
PrintUtil.print(FTPConstant.REPLYCODE);
System.out.println("上传目录:" + ftp.putDir("C:\\Python27", "python27"));
// test FTPClient
FTPClient client = ftp.client();
System.out.println(client.makeDirectory("test"));
System.out.println(client.makeDirectory("test1/"));
System.out.println(client.makeDirectory("test/test1"));
System.out.println(client.makeDirectory("test2/test2"));
FTPFile[] list = client.listFiles();
System.out.println(list);
String[] list1 = client.listNames();
System.out.println(list1);
System.out.println("执行命令/" + client.sendCommand("mkdir testssss"));
int ss = client.sendCommand("ls");
ftp.destory();
}
use of org.apache.commons.net.ftp.FTPClient in project alliance by codice.
the class NsiliSource method getIorStringFromFtpSource.
/**
* Uses FTPClient to obtain the IOR string from the provided URL via FTP.
*/
private void getIorStringFromFtpSource() {
URI uri = null;
try {
uri = new URI(iorUrl);
} catch (URISyntaxException e) {
LOGGER.debug("{} : Invalid URL specified for IOR string: {}", sourceId, iorUrl, e);
return;
}
FTPClient ftpClient = new FTPClient();
try {
if (uri.getPort() > 0) {
ftpClient.connect(uri.getHost(), uri.getPort());
} else {
ftpClient.connect(uri.getHost());
}
if (!ftpClient.login(serverUsername, serverPassword)) {
LOGGER.debug("{} : FTP server log in unsuccessful.", sourceId);
} else {
int timeoutMsec = clientTimeout * 1000;
ftpClient.setConnectTimeout(timeoutMsec);
ftpClient.setControlKeepAliveReplyTimeout(timeoutMsec);
ftpClient.setDataTimeout(timeoutMsec);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
InputStream inputStream = ftpClient.retrieveFileStream(uri.getPath());
iorString = IOUtils.toString(inputStream, StandardCharsets.ISO_8859_1.name());
// Remove leading/trailing whitespace as the CORBA init can't handle that.
iorString = iorString.trim();
}
} catch (Exception e) {
LOGGER.debug("{} : Error retrieving IOR file for {}.", sourceId, iorUrl, e);
}
}
use of org.apache.commons.net.ftp.FTPClient in project dubidubi by lzzzz4.
the class FtpUtil method uploadFile.
/**
* Description: 向FTP服务器上传文件
*
* @param host
* FTP服务器hostname
* @param port
* FTP服务器端口
* @param username
* FTP登录账号
* @param password
* FTP登录密码
* @param basePath
* FTP服务器基础目录
* @param filePath
* FTP服务器文件存放路径。例如分日期存放:/2015/01/01。文件的路径为basePath+filePath
* @param filename
* 上传到FTP服务器上的文件名
* @param input
* 输入流
* @return 成功返回true,否则返回false
*/
public static boolean uploadFile(String host, int port, String username, String password, String basePath, String filePath, String filename, InputStream input) {
boolean result = false;
FTPClient ftp = new FTPClient();
try {
int reply;
// 连接FTP服务器
ftp.connect(host, port);
// 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
// 登录
ftp.login(username, password);
reply = ftp.getReplyCode();
// System.out.println(reply);
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
// 切换到上传目录
if (!ftp.changeWorkingDirectory(basePath + filePath)) {
// 如果目录不存在创建目录
System.out.println("创建");
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);
}
}
}
}
// System.out.println("创建完成");
// 设置上传文件的类型为二进制类型
ftp.setFileType(FTP.BINARY_FILE_TYPE);
// 上传文件
if (!ftp.storeFile(filename, input)) {
return result;
}
input.close();
ftp.logout();
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return result;
}
Aggregations