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实现上传、下载的更多相关文章

  1. coding++:java操作 FastDFS(上传 | 下载 | 删除)

    开发工具  IDEAL2017  Springboot 1.5.21.RELEASE --------------------------------------------------------- ...

  2. JAVA中使用FTPClient上传下载

    Java中使用FTPClient上传下载 在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在c ...

  3. 在Windows上使用终端模拟程序连接操作Linux以及上传下载文件

    在Windows上使用终端模拟程序连接操作Linux以及上传下载文件 [很简单,就是一个工具的使用而已,放这里是做个笔记.] 刚买的云主机,或者是虚拟机里安装的Linux系统,可能会涉及到在windo ...

  4. Java中实现文件上传下载的三种解决方案

    第一点:Java代码实现文件上传 FormFile file=manform.getFile(); String newfileName = null; String newpathname=null ...

  5. Java FTPClient实现文件上传下载

    在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在commons-net包中)实现上传下载文件 ...

  6. java实现多线程断点续传,上传下载

    采用apache 的 commons-net-ftp-ftpclient import java.io.File; import java.io.FileOutputStream; import ja ...

  7. java客户端调用ftp上传下载文件

    1:java客户端上传,下载文件. package com.li.utils; import java.io.File; import java.io.FileInputStream; import ...

  8. java中的文件上传下载

    java中文件上传下载原理 学习内容 文件上传下载原理 底层代码实现文件上传下载 SmartUpload组件 Struts2实现文件上传下载 富文本编辑器文件上传下载 扩展及延伸 学习本门课程需要掌握 ...

  9. java+web+大文件上传下载

    文件上传是最古老的互联网操作之一,20多年来几乎没有怎么变化,还是操作麻烦.缺乏交互.用户体验差. 一.前端代码 英国程序员Remy Sharp总结了这些新的接口 ,本文在他的基础之上,讨论在前端采用 ...

随机推荐

  1. IOS 苹果手机fiddler抓包时出现了tunnel to 443 解决方案,亲测有效

    先上一张捉取成功图[版本需4.0以上,并非所有https数据可抓取,具体原因未知] 1.先对Fiddler进行设置[打开Fiddler ——> Options .然后打开的对话框中,选择HTTP ...

  2. python的zipfile、tarfile模块

    zipfile.tarfile的用法 先占个位置,后续再实际操作和补充 参考 https://www.cnblogs.com/MnCu8261/p/5494807.html

  3. 简单的windows窗口创建实例

    #include<windows.h> #include<tchar.h> LRESULT CALLBACK WndProc(HWND hwnd,UINT umsg,WPARA ...

  4. noip 2013 提高组 day1

    1.转圈游戏: 解析部分略,快速幂就可以过 Code: #include<iostream> #include<fstream> using namespace std; if ...

  5. spring启动后立即执行方法

    1.方法所属的类继承InitializingBean接口. 2.重写afterPropertiesSet()方法. afterPropertiesSet方法会在bean被初始化时执行. 当bean的作 ...

  6. C# 反射小结

    废话不多说,直接上代码. 1.typeof(类名):它是一个运算符 eg_1:Type type = typeof(int) ; eg_2:public class Student { Type ty ...

  7. ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) A

    2018-02-19 A. Palindromic Supersequence time limit per test 2 seconds memory limit per test 256 mega ...

  8. Bootstrap3基础 dropdown divider 下拉列表中的分割线

      内容 参数   OS   Windows 10 x64   browser   Firefox 65.0.2   framework     Bootstrap 3.3.7   editor    ...

  9. android 系统 不深度休眠【转】

    本文转载自:https://blog.csdn.net/fmc088/article/details/80401405 1.分析解析 android系统有earlysuspend和suspend两种休 ...

  10. Print a file's last modified date in Bash

    date -r <filename> #!/usr/bin/env bash for i in /var/log/*.out; do stat -f "%Sm" -t ...