JDK1.7以上javaFTP上传删除文件的实现方法
实例如下:
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上传工具类
*
*
@authoryanzhou
*
@versionv1.0
*/
publicclass
FTPUtil{
privatestatic
FtpClientftpClient=null;
privatestatic
final
Stringurl;
privatestatic
final
int
port;
privatestatic
final
Stringuser;
privatestatic
final
Stringpassword;
privatestatic
final
StringremoteFilePath;
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
*
@throwsFtpProtocolException
*/
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
*
@paramfilefile文件,struts2从页面得到的File类型
*
@paramfilePath要保存在FTP上的路径(文件夹)
*
@paramfileName文件名(test001.jpg)
*
@return文件是否上传成功
*
@throwsException
*/
publicstatic
boolean
upload(Filefile,StringfilePath,StringfileName){
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制定目录下的文件
*
@paramfilePath文件在FTP存储的路径
*
@paramfileName要删除的文件名称
*
@return是否删除成功
*/
publicstatic
boolean
deleteFileFtp(StringfilePath,StringfileName){
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();
}
}
/**
*
检查文件夹是否存在
*
*
@paramdir
*
@paramftpClient
*
@return
*/
privatestatic
BooleanisDirExist(Stringdir){
try{
ftpClient.changeDirectory(dir);
}catch(Exception
e){
returnfalse;
}
returntrue;
}
/**
*
创建文件夹
*
*
@paramdir
*
@paramftpClient
*
@throwsException
*/
privatestatic
void
createDir(Stringdir)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.常量类,系统的路径分隔符
packagecom.itv.launcher.util;
publicinterface
Constants{
//路径分隔符
publicstatic
StringFILE_SEPARATOR=System.getProperty("file.separator");
}
3.FTP链接的配置properties文件,包括用户名密码一些信息
#FTP的IP地址 FTP_URL=127.0.0.1 #FTP端口号 FTP_PORT=1234 #用户名 FTP_USER=yanzhou #密码 FTP_PASSWORD=abcdefg12345 #FTP账号目录 FTP_REMOTE_FILEPATH=
以上这篇JDK1.7以上javaFTP上传删除文件的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。