博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FTP
阅读量:6367 次
发布时间:2019-06-23

本文共 5732 字,大约阅读时间需要 19 分钟。

public class FtpUtil {

private static FTPClient ftpClient = new FTPClient();

private static String encoding = System.getProperty("file.encoding");

private static String defaultPath;

/**

* FTP初始化连接

*/

public static boolean connect(String host,Integer port,String userName,String password) throws IOException{

return connect(host,port,userName,password,null);

}

/**

* FTP初始化连接

*/

public static boolean connect(String host,Integer port,String userName,String password,String remotePath) throws IOException{

boolean result=false;

try {

ftpClient.connect(host,port);

// 登录

ftpClient.login(userName, password);

ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);

// 检验是否连接成功

int reply = ftpClient.getReplyCode();

if (!FTPReply.isPositiveCompletion(reply)) {//连接失败

ftpClient.disconnect();

result =false;

}else{//如果连接成功

result = true;

if(remotePath != null){

defaultPath = remotePath;

result =ftpClient.changeWorkingDirectory(new String(remotePath.getBytes(encoding),"iso-8859-1"));

if(result == false){

System.err.println("ftp changeWorkingDirectory to ".concat(remotePath).concat(" error"));

}

}

System.out.println("ftp connect successfully.");

}

} catch (SocketException e) {

System.err.println("ftp connect failed: " + e.getMessage());

throw e;

} catch (IOException e) {

System.err.println("ftp connect failed: " + e.getMessage());

throw e;

}

return result;

}

/**

* FTP是否连接

*/

public static boolean isConnected(){

return ftpClient.isConnected();

}

/**

* 上传文件

*/

public static boolean uploadFile(String filename, InputStream input) throws IOException {

return uploadFile(null,filename,input);

}

/**

* 上传文件

*/

public static boolean uploadFile(String remotePath, String filename, InputStream input) throws IOException {

boolean result = false;

if (!ftpClient.isConnected()) {

System.err.println("ftp uploadfile error : no ftpserver is connected");

return false;

}

try {

// 转移工作目录至指定目录下

if(remotePath != null){

ftpClient.changeWorkingDirectory(new String(remotePath.getBytes(encoding),"iso-8859-1")); 

}

 

ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

result = ftpClient.storeFile(new String(filename.getBytes(encoding),"iso-8859-1"), input);

} catch (IOException e) {

System.err.println("ftp uploadfile error : "+e.getMessage());

throw e;

} finally {

try {

if (input != null) {

input.close();

}

} catch (IOException e) {

System.err.println("ftp uploadfile close error : "+e.getMessage());

throw e;

}

}

System.out.println("ftp uploadfile successfully.");

return result;

}

/**

* 下载FTP文件

*/

public static boolean downFile(String fileName, String localPath) throws IOException {

return downFile(null,fileName,localPath);

}

/**

* 下载FTP文件

*/

public static boolean downFile( String remotePath, String fileName, String localPath) throws IOException {

boolean result = false;

if (!ftpClient.isConnected()) {

System.err.println("ftp download file error : no ftpserver is connected");

return false;

}

try {

 

// 转移工作目录至指定目录下

if(remotePath != null){

ftpClient.changeWorkingDirectory(new String(remotePath.getBytes(encoding),"iso-8859-1"));        

}

 

System.out.println("ftp start download file :" + fileName);

 

// 获取文件列表

FTPFile[] fs = ftpClient.listFiles();

for (FTPFile ff : fs) {

if(fileName != null && !fileName.equals(ff.getName())){

continue;

}

 

File localFile = new File(localPath + "/" + ff.getName());

OutputStream os = new FileOutputStream(localFile);

ftpClient.retrieveFile(ff.getName(), os);

os.close();

 

System.out.println("ftp download file finished :"+ff.getName());

result = true;

}

 

if(result == false){

System.err.println("ftp download error cannot find file : " + fileName);

}

} catch (IOException e) {

System.err.println("ftp download error : "+e.getMessage());

throw e;

return result;

}

/**

* 关闭当前连接

*/

public static void close(){

try {

ftpClient.logout();

ftpClient.disconnect();

} catch (IOException e) {

System.err.println("ftp ftpserver close error : "+e.getMessage());

}

}

/**

* 返回连接默认路径

*/

public static void cdRoot() throws Exception{

try {

if (!ftpClient.isConnected()) {

System.err.println("ftp cd root error : no ftpserver is connected");

}

ftpClient.changeWorkingDirectory(new String("/".getBytes(encoding),"iso-8859-1"));

if(defaultPath != null){

ftpClient.changeWorkingDirectory(new String(defaultPath.getBytes(encoding),"iso-8859-1"));   

}

} catch (Exception e) {

System.err.println("ftp change directory to defaultpath error : "+e.getMessage());

throw e;

}

}

/**

* 工作路径切换

*/

public static void cd(String path) throws Exception{

try {

if (!ftpClient.isConnected()) {

System.err.println(" ftp cd " + path + " : no ftpserver is connected");

}

ftpClient.changeWorkingDirectory(new String(path.getBytes(encoding),"iso-8859-1"));

} catch (Exception e) {

System.err.println("ftp cd '" + path + "' error : "+e.getMessage());

throw e;

}

}

/**

* 创建工作路径

*/

public static void mkdir(String path) throws IOException{

try {

if (!ftpClient.isConnected()) {

System.err.println("ftp mkdir error : no ftpserver is connected");

}

ftpClient.makeDirectory(path);

} catch (IOException e) {

System.err.println("ftp mkdir '" + path + "' error : "+e.getMessage());

throw e;

}

}

/**

* 查找文件是否存在

*/

public static FTPFile findFile(String filename) throws IOException{

return findFile(null,filename);

}

/**

* 查找文件是否存在

*/

public static FTPFile findFile(String remotePath, String filename) throws IOException{

try {

if (!ftpClient.isConnected()) {

System.err.println("ftp findfile error : no ftpserver is connected");

return null;

}

FTPFile[] fs = ftpClient.mlistDir(remotePath);

for (FTPFile ff : fs) {

if(filename != null && !filename.equals(ff.getName())){

continue;

}

return ff;

}

} catch (IOException e) {

System.err.println("ftp find file '" + filename + "' error : "+e.getMessage());

throw e;

}

return null;

}

}

转载于:https://www.cnblogs.com/xyzq/p/6816270.html

你可能感兴趣的文章
Entity Framework 数据生成选项DatabaseGenerated
查看>>
jquery 兼容的滚轮事件
查看>>
模板小例子
查看>>
告诉你html5比普通html多了哪些东西?
查看>>
十倍交叉验证 10-fold cross-validation
查看>>
Windows无法删除文件 提示找不到该项目怎么办
查看>>
js 数组
查看>>
R语言中的字符串处理函数
查看>>
平方和公式
查看>>
【Unity游戏开发】浅谈 NGUI 中的 UIRoot、UIPanel、UICamera 组件
查看>>
内存模型
查看>>
table边框设置
查看>>
IOS开发之实现App消息推送(最新)
查看>>
C++ 资源管理之 RAII
查看>>
UVA11234 Expressions
查看>>
(原創) char s[]字串和char *s字串有什麼差別? (C/C++) (C)
查看>>
(原創) 如何讓泛型支援多個interface? (.NET) (C/C++) (C#) (template) (C++/CLI)
查看>>
(筆記) 如何使用$skew? (SOC) (Verilog)
查看>>
信息系统开发平台OpenExpressApp - AutoUI自动生成界面
查看>>
(筆記) 如何使ModelSim與nLint並存? (SOC) (ModelSim) (nLint)
查看>>