java1.7以上连接ftp_JDK1.7以上javaFTP上传删除文件的实现方法

JDK1.7以上javaFTP上传删除文件的实现方法

发布于 2020-6-25|

复制链接

下面小妖就为大家分享一篇JDK1.7以上javaFTP上传删除文件的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小妖过来看看吧

实例如下:

```xhtml

packagecom.itv.launcher.util;

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.FileNotFoundException;

importjava.io.IOException;

importjava.net.InetSocketAddress;

importjava.util.Properties;

importjava.util.StringTokenizer;

importsun.net.TelnetOutputStream;

importsun.net.ftp.FtpClient;

importsun.net.ftp.FtpProtocolException;

/**

*

FTP上传工具类

*

*

@author yanzhou

*

@version v1.0

*/

publicclass

FTPUtil {

privatestatic

FtpClient ftpClient = null;

privatestatic

final

String url;

privatestatic

final

int

port;

privatestatic

final

String user;

privatestatic

final

String password;

privatestatic

final

String remoteFilePath;

static{

Properties

FTPPro = ReadFTPProperties.getMsgFromPro();

url

= FTPPro.getProperty("FTP_URL");

port

= Integer.parseInt(FTPPro.getProperty("FTP_PORT"));

user

= FTPPro.getProperty("FTP_USER");

password

= FTPPro.getProperty("FTP_PASSWORD");

remoteFilePath

= FTPPro.getProperty("FTP_REMOTE_FILEPATH");

}

/**

*

链接FTP

*

@throws FtpProtocolException

*/

privatestatic

void

connectFTP() throwsFtpProtocolException

{

try{

ftpClient

= FtpClient.create();

ftpClient.connect(newInetSocketAddress(url,

port));

ftpClient.login(user,

password.toCharArray());

ftpClient.setBinaryType();

if(!"".equals(remoteFilePath)

&& remoteFilePath != null)

{

ftpClient.changeDirectory(remoteFilePath);

}

}catch(IOException

e) {

e.printStackTrace();

}

}

/**

*

关闭FTP链接

*/

publicstatic

void

closeFTP() {

try{

if(ftpClient

!= null)

{

ftpClient.close();

}

}catch(IOException

e) {

e.printStackTrace();

}

}

/**

*

上传文件到FTP

*

@param file file文件,struts2从页面得到的File类型

*

@param filePath 要保存在FTP上的路径(文件夹)

*

@param fileName 文件名(test001.jpg)

*

@return 文件是否上传成功

*

@throws Exception

*/

publicstatic

boolean

upload(File file, String filePath, String fileName) {

TelnetOutputStream

to = null;

FileInputStream

fi = null;

filePath

= remoteFilePath + Constants.FILE_SEPARATOR + filePath;

try{

if(file

!= null)

{

connectFTP();

if(!isDirExist(filePath.replace("\\","/")))

{

createDir(filePath.replace("\\","/"));

ftpClient.changeDirectory(filePath.replace("\\","/"));

}

fi

= newFileInputStream(file);

to

= (TelnetOutputStream) ftpClient.putFileStream(fileName, true);

byte[]

bytes = newbyte[1024];

inti

= fi.read(bytes);

while(i

!= -1)

{

to.write(bytes);

i

= fi.read(bytes);

}

}

returntrue;

}catch(FileNotFoundException

e1) {

returnfalse;

}catch(IOException

e2) {

returnfalse;

}catch(Exception

e) {

returnfalse;

}finally{

if(fi

!= null)

{

try{

fi.close();

}catch(IOException

e) {

e.printStackTrace();

}

}

if(to

!= null)

{

try{

to.flush();

to.close();

}catch(IOException

e) {

e.printStackTrace();

}

}

closeFTP();

}

}

/**

*

删除FTP制定目录下的文件

*

@param filePath 文件在FTP存储的路径

*

@param fileName 要删除的文件名称

*

@return 是否删除成功

*/

publicstatic

boolean

deleteFileFtp(String filePath, String fileName){

try{

connectFTP();

filePath

= remoteFilePath + Constants.FILE_SEPARATOR + filePath + Constants.FILE_SEPARATOR;

if(!isDirExist(filePath.replace("\\","/")))

{

returnfalse;

}

ftpClient.changeDirectory(filePath.replace("\\","/"));

ftpClient.deleteFile(fileName);

returntrue;

}catch(Exception

e) {

e.printStackTrace();

returnfalse;

}finally{

closeFTP();

}

}

/**

*

检查文件夹是否存在

*

*

@param dir

*

@param ftpClient

*

@return

*/

privatestatic

Boolean isDirExist(String dir) {

try{

ftpClient.changeDirectory(dir);

}catch(Exception

e) {

returnfalse;

}

returntrue;

}

/**

*

创建文件夹

*

*

@param dir

*

@param ftpClient

*

@throws Exception

*/

privatestatic

void

createDir(String dir) throwsException

{

ftpClient.setAsciiType();

StringTokenizer

s = newStringTokenizer(dir,

"/");//

sign

s.countTokens();

String

pathName = "";

while(s.hasMoreElements())

{

pathName

= pathName + "/"+

(String) s.nextElement();

try{

ftpClient.makeDirectory(pathName);

}catch(Exception

e) {

e

= null;

}

}

ftpClient.setBinaryType();

}

}

```

2. 常量类,系统的路径分隔符

```xhtml

packagecom.itv.launcher.util;

publicinterface

Constants {

//路径分隔符

publicstatic

String FILE_SEPARATOR = System.getProperty("file.separator");

}

```

3. FTP链接的配置properties文件,包括用户名密码一些信息

```xhtml

#FTP的IP地址

FTP_URL=127.0.0.1

#FTP端口号

FTP_PORT=1234

#用户名

FTP_USER=yanzhou

#密码

FTP_PASSWORD=abcdefg12345

#FTP账号目录

FTP_REMOTE_FILEPATH=

```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值