java操作vaftpd实现上传、下载
1.配置文件conf/vsftpd.properties
(我是单独写了一个配置文件,你可以直接写在application中)
vsftpd.ip=192.168.**.**
vsftpd.user=wangwei
vsftpd.pwd=123456
vsftpd.port=21
#ftp服务器根路径
vsftpd.remote.base.path=/var/ftp/wangwei
#ftp服务器上的相对路径【文件路径 =/var/ftp/wangwei/images】
vsftpd.remote.file.path=/images
#文件下载到本地的目录位置
vsftpd.local.file.path=F:/ftp/
2.操作类FtpUtil.java
package com.ch.evaluation.common.vsftpd.util; import org.apache.commons.lang3.StringUtils;
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 org.apache.log4j.chainsaw.Main;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.io.*;
import java.util.Properties; /**
* @author wangwei
* @date 2018/11/04
* ftp服务器文件上传下载
*/
public class FtpUtil {
private static Logger LOGGER = LoggerFactory.getLogger(FtpUtil.class);
private static String host;
private static String port;
private static String username;
private static String password;
private static String basePath;
private static String filePath;
private static String localPath; /**
*读取配置文件信息
* @return
*/
public static void getPropertity(){
Properties properties = new Properties();
ClassLoader load = Main.class.getClassLoader();
InputStream is = load.getResourceAsStream("conf/vsftpd.properties");
try {
properties.load(is);
host=properties.getProperty("vsftpd.ip");//通用
port=properties.getProperty("vsftpd.port");//通用
username=properties.getProperty("vsftpd.user");//通用
password=properties.getProperty("vsftpd.pwd");//通用
basePath=properties.getProperty("vsftpd.remote.base.path");//服务器 基路径
filePath=properties.getProperty("vsftpd.remote.file.path");//服务器 文件路径
localPath=properties.getProperty("vsftpd.local.file.path");//本地 下载到本地的目录
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 上传重载
* @param filename 上传到服务器端口重命名
* @param sourceFilePathName 源文件【路径+文件】
* @return
*/
public static boolean uploadFile(String filename, String sourceFilePathName) {
getPropertity();
return uploadFile( host, port, username, password, basePath,
filePath, filename, sourceFilePathName);
} /**
* 上传重载
* @param filePath 上传到服务器的相对路径
* @param filename 上传到服务器端口重命名
* @param sourceFilePathName 源文件【路径+文件】
* @return
*/
public static boolean uploadFile(String filePath,String filename, String sourceFilePathName) {
getPropertity();
return uploadFile( host, port, username, password, basePath,
filePath, filename, sourceFilePathName);
} /**
*下载重载
* @param fileName 要下载的文件名
* @return
*/
public static boolean downloadFile(String fileName) {
getPropertity();
return downloadFile( host, port, username, password, basePath,
filePath, fileName, localPath, null);
} /**
*下载重载
* @param filePath 要下载的文件所在服务器的相对路径
* @param fileName 要下载的文件名
* @return
*/
public static boolean downloadFile(String filePath,String fileName) {
getPropertity();
return downloadFile( host, port, username, password, basePath,
filePath, fileName, localPath, null);
} /**
* 下载重载
* @param fileName 要下载的文件名
* @param filePath 要下载的文件所在服务器的相对路径
* @param rename 下载后重命名[null或空不进行重命名]
* @return
*/
public static boolean downloadFile(String filePath,String fileName, String rename) {
getPropertity();
return downloadFile( host, port, username, password, basePath,
filePath, fileName, localPath, rename);
} /**
* 下载重载
* @param fileName 要下载的文件名
* @param localPath 下载时,到本地的路径
* @param filePath 要下载的文件所在服务器的相对路径
* @param rename 下载后重命名[null或空不进行重命名]
* @return
*/
public static boolean downloadFile(String filePath,String fileName,String localPath, String rename) {
getPropertity();
return downloadFile( host, port, username, password, basePath,
filePath, fileName, localPath, rename);
} /**
* 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服务器上的文件名
* @return 成功返回true,否则返回false
*/
public static boolean uploadFile(String host, String port, String username, String password, String basePath,
String filePath, String filename, String sourceFilePathName) {
boolean result = false;
FTPClient ftp = new FTPClient();
try {
int portNum = Integer.parseInt(port);
int reply;
ftp.connect(host, portNum);// 连接FTP服务器
// 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
ftp.login(username, password);// 登录
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);
}
}
}
}
//为了加大上传文件速度,将InputStream转成BufferInputStream , InputStream input
FileInputStream input = new FileInputStream(new File(sourceFilePathName));
BufferedInputStream in = new BufferedInputStream(input);
//加大缓存区
ftp.setBufferSize(1024*1024);
//设置上传文件的类型为二进制类型
ftp.setFileType(FTP.BINARY_FILE_TYPE);
//上传文件
if (!ftp.storeFile(filename, in)) {
return result;
}
in.close();
ftp.logout();
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return result;
} /**
* Description: 从FTP服务器下载文件
* @param host FTP服务器hostname
* @param port FTP服务器端口
* @param username FTP登录账号
* @param password FTP登录密码
* @param basePath FTP服务器上的相对路径
* @param fileName 要下载的文件名
* @param localPath 下载后保存到本地的路径
* @return
*/
public static boolean downloadFile(String host, String port, String username, String password, String basePath,
String filePath, String fileName, String localPath, String rename) {
boolean result = false;
FTPClient ftp = new FTPClient();
try {
int portNum = Integer.parseInt(port);
int reply;
ftp.connect(host, portNum);
// 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
ftp.login(username, password);// 登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
//ftp.changeWorkingDirectory(basePath);// 转移到FTP服务器目录
if (!ftp.changeWorkingDirectory(basePath+filePath)) {
LOGGER.info("服务器端目录不存在...");
}
FTPFile[] fs = ftp.listFiles();
boolean flag = true;
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
flag = false;
rename = StringUtils.isEmpty(rename)?ff.getName():rename;
File localFile = new File(localPath + "/" + rename);
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
if(flag) LOGGER.info("服务器端文件不存在...");
ftp.logout();
result = true;
} catch (IOException e) {
LOGGER.info(e.getMessage());
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return result;
}
}
3.测试
import com.ch.evaluation.common.vsftpd.util.FtpUtil;
public class ftpController {
public static void main(String[] args) {
/* boolean flag = FtpUtil.uploadFile("192.168.**.**", "21", "wangwei", "123456",
"/var/ftp/wangwei","/images", "handsome3.jpg", "F:\\ftp\\handSome.JPG");*/
/* boolean flag = FtpUtil.uploadFile("/images1", "handsome.jpg", "F:\\ftp\\handSome.JPG");*/
/* boolean flag = FtpUtil.uploadFile("handsome.jpg", "F:\\ftp\\handSome.JPG");*/
/* boolean flag = FtpUtil.downloadFile("192.168.**.**", "21", "wangwei", "123456", "/var/ftp/wangwei/images",
"handsome2.jpg", "F:/ftp/","");*/
boolean flag = FtpUtil.downloadFile("handsome2.jpg", "F:/ftp/");
System.out.println(flag);
}
}
附gitHub源码:https://github.com/UncleWW/Shop
java操作vaftpd实现上传、下载的更多相关文章
- coding++:java操作 FastDFS(上传 | 下载 | 删除)
开发工具 IDEAL2017 Springboot 1.5.21.RELEASE --------------------------------------------------------- ...
- JAVA中使用FTPClient上传下载
Java中使用FTPClient上传下载 在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在c ...
- 在Windows上使用终端模拟程序连接操作Linux以及上传下载文件
在Windows上使用终端模拟程序连接操作Linux以及上传下载文件 [很简单,就是一个工具的使用而已,放这里是做个笔记.] 刚买的云主机,或者是虚拟机里安装的Linux系统,可能会涉及到在windo ...
- Java中实现文件上传下载的三种解决方案
第一点:Java代码实现文件上传 FormFile file=manform.getFile(); String newfileName = null; String newpathname=null ...
- Java FTPClient实现文件上传下载
在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在commons-net包中)实现上传下载文件 ...
- java实现多线程断点续传,上传下载
采用apache 的 commons-net-ftp-ftpclient import java.io.File; import java.io.FileOutputStream; import ja ...
- java客户端调用ftp上传下载文件
1:java客户端上传,下载文件. package com.li.utils; import java.io.File; import java.io.FileInputStream; import ...
- java中的文件上传下载
java中文件上传下载原理 学习内容 文件上传下载原理 底层代码实现文件上传下载 SmartUpload组件 Struts2实现文件上传下载 富文本编辑器文件上传下载 扩展及延伸 学习本门课程需要掌握 ...
- java+web+大文件上传下载
文件上传是最古老的互联网操作之一,20多年来几乎没有怎么变化,还是操作麻烦.缺乏交互.用户体验差. 一.前端代码 英国程序员Remy Sharp总结了这些新的接口 ,本文在他的基础之上,讨论在前端采用 ...
随机推荐
- Docker学习笔记之搭建Docker运行环境
0x00 概述 既然 Docker 是一款实用软件,我们就不得不先从它的安装说起,只有让 Docker 运行在我们的计算机上,才能更方便我们对 Docker 相关知识和使用方式的学习.得益于与商业性的 ...
- layui使用iconfont
layui的图标取自于阿里巴巴的矢量图标库 Iconfont,同样的,这篇教程也是基于Iconfont进行扩展. 第一步,通过浏览器打开 http://iconfont.cn/ ,访问阿里巴巴矢量图标 ...
- 利用cgi编程实现web版man手册
董老师前几天给我们布置了3道作业,第三道作业是cgi程序设计. 题目: web服务器cgi接口功能的实现 要求: 能调用cgi程序并得到返回结果: cgi程序能接受参数并得到返回结果: 使用两种或以上 ...
- 【题解】Luogu P5068 [Ynoi2015]我回来了
众所周知lxl是个毒瘤,Ynoi道道都是神仙题,这道题极其良心,题面好评 原题传送门 我们先珂以在\(O(n^2)\)的时间内bfs求出任意两点距离 我们考虑如何计算从一个点到所有点的最短路长度小于等 ...
- 【4Opencv】如何识别出轮廓准确的长和宽
问题来源: 实际项目中,需要给出识别轮廓的长度和宽度. 初步分析: 轮廓分析的例程为: int main( int argc, char** argv ){ //read the image ...
- 外键 Foreign keys
https://docs.microsoft.com/en-us/sql/relational-databases/tables/create-foreign-key-relationships?vi ...
- 论文笔记:Person Re-identification with Deep Similarity-Guided Graph Neural Network
Person Re-identification with Deep Similarity-Guided Graph Neural Network 2018-07-27 17:41:45 Paper: ...
- 《机器学习实战》之k-近邻算法(手写识别系统)
这个玩意和改进约会网站的那个差不多,它是提前把所有数字转换成了32*32像素大小的黑白图,然后转换成字符图(用0,1表示),将所有1024个像素点用一维矩阵保存下来,这样就可以通过knn计算欧几里得距 ...
- WebGIS前端地图显示之根据地理范围换算出瓦片行列号的原理(核心)
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.前言 在上一节中我们知道了屏幕上一像素等于实际中多少单位长度(米或 ...
- 【Ruby】【改gem源镜像】【Win10 + Jruby-9.1.2.0 + Rails 5.1.3 + gem 2.6.4 】
参考地址:https://ruby-china.org/topics/33843 (1)> gem sources --add http://gems.ruby-china.org 遇到问题: ...