Java FTP上传下载删除功能实例代码
在没给大家上完整代码之前先给大家说下注意点:
FTP上传下载,容易出现乱码,记得转换
packagecom.yinhai.team.action;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
importorg.apache.commons.net.ftp.FTPClient;
importorg.apache.commons.net.ftp.FTPFile;
importorg.apache.commons.net.ftp.FTPReply;
publicclassFavFTPUtil{
/**
*上传文件(可供Action/Controller层使用)未测试
*@paramhostnameFTP服务器地址
*@paramportFTP服务器端口号
*@paramusernameFTP登录帐号
*@parampasswordFTP登录密码
*@parampathnameFTP服务器保存目录
*@paramfileName上传到FTP服务器后的文件名称
*@paraminputStream输入文件流
*@return
*/
publicstaticbooleanuploadFile(Stringhostname,intport,Stringusername,Stringpassword,Stringpathname,StringfileName,InputStreaminputStream){
booleanflag=false;
FTPClientftpClient=newFTPClient();
ftpClient.setControlEncoding("UTF-8");
try{
//连接FTP服务器
ftpClient.connect(hostname,port);
//登录FTP服务器
ftpClient.login(username,password);
//是否成功登录FTP服务器
intreplyCode=ftpClient.getReplyCode();
if(!FTPReply.isPositiveCompletion(replyCode)){
returnflag;
}
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.makeDirectory(pathname);
ftpClient.changeWorkingDirectory(pathname);
ftpClient.storeFile(fileName,inputStream);
inputStream.close();
ftpClient.logout();
flag=true;
}catch(Exceptione){
e.printStackTrace();
}finally{
if(ftpClient.isConnected()){
try{
ftpClient.disconnect();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
returnflag;
}
/**
*上传文件(可对文件进行重命名)未测试
*@paramhostnameFTP服务器地址
*@paramportFTP服务器端口号
*@paramusernameFTP登录帐号
*@parampasswordFTP登录密码
*@parampathnameFTP服务器保存目录
*@paramfilename上传到FTP服务器后的文件名称
*@paramoriginfilename待上传文件的名称(绝对地址)
*@return
*/
publicstaticbooleanuploadFileFromProduction(Stringhostname,intport,Stringusername,Stringpassword,Stringpathname,Stringfilename,Stringoriginfilename){
booleanflag=false;
try{
InputStreaminputStream=newFileInputStream(newFile(originfilename));
flag=uploadFile(hostname,port,username,password,pathname,filename,inputStream);
}catch(Exceptione){
e.printStackTrace();
}
returnflag;
}
/**
*上传文件(不可以进行文件的重命名操作)未测试
*@paramhostnameFTP服务器地址
*@paramportFTP服务器端口号
*@paramusernameFTP登录帐号
*@parampasswordFTP登录密码
*@parampathnameFTP服务器保存目录
*@paramoriginfilename待上传文件的名称(绝对地址)
*@return
*/
publicstaticbooleanuploadFileFromProduction(Stringhostname,intport,Stringusername,Stringpassword,Stringpathname,Stringoriginfilename){
booleanflag=false;
try{
StringfileName=newFile(originfilename).getName();
InputStreaminputStream=newFileInputStream(newFile(originfilename));
flag=uploadFile(hostname,port,username,password,pathname,fileName,inputStream);
}catch(Exceptione){
e.printStackTrace();
}
returnflag;
}
/**
*删除文件未测试
*@paramhostnameFTP服务器地址
*@paramportFTP服务器端口号
*@paramusernameFTP登录帐号
*@parampasswordFTP登录密码
*@parampathnameFTP服务器保存目录
*@paramfilename要删除的文件名称
*@return
*/
publicstaticbooleandeleteFile(Stringhostname,intport,Stringusername,Stringpassword,Stringpathname,Stringfilename){
booleanflag=false;
FTPClientftpClient=newFTPClient();
try{
//连接FTP服务器
ftpClient.connect(hostname,port);
//登录FTP服务器
ftpClient.login(username,password);
//验证FTP服务器是否登录成功
intreplyCode=ftpClient.getReplyCode();
if(!FTPReply.isPositiveCompletion(replyCode)){
returnflag;
}
//切换FTP目录
ftpClient.changeWorkingDirectory(pathname);
ftpClient.dele(filename);
ftpClient.logout();
flag=true;
}catch(Exceptione){
e.printStackTrace();
}finally{
if(ftpClient.isConnected()){
try{
ftpClient.logout();
}catch(IOExceptione){
}
}
}
returnflag;
}
/**
*下载文件
*@paramhostnameFTP服务器地址
*@paramportFTP服务器端口号
*@paramusernameFTP登录帐号
*@parampasswordFTP登录密码
*@parampathnameFTP服务器文件目录
*@paramfilename文件名称
*@paramlocalpath下载后的文件路径
*@return
*/
publicstaticbooleandownloadFile(Stringhostname,intport,Stringusername,Stringpassword,Stringpathname,Stringfilename,Stringlocalpath){
booleanflag=false;
FTPClientftpClient=newFTPClient();
try{
//连接FTP服务器
ftpClient.connect(hostname,port);
//登录FTP服务器
ftpClient.login(username,password);
//验证FTP服务器是否登录成功
intreplyCode=ftpClient.getReplyCode();
if(!FTPReply.isPositiveCompletion(replyCode)){
returnflag;
}
//切换FTP目录
booleanb=ftpClient.changeWorkingDirectory(newString(pathname.getBytes(),"ISO-8859-1"));
System.out.println(b);
FTPFile[]ftpFiles=ftpClient.listFiles();
for(FTPFilefile:ftpFiles){
StringfName=newString(file.getName().getBytes("iso-8859-1"),"UTF-8");
System.out.println(fName);
if(filename.equalsIgnoreCase(fName)){
FilelocalFile=newFile(localpath+"/"+fName);
OutputStreamos=newFileOutputStream(localFile);
ftpClient.retrieveFile(file.getName(),os);
os.close();
}
}
ftpClient.logout();
flag=true;
}catch(Exceptione){
e.printStackTrace();
}finally{
if(ftpClient.isConnected()){
try{
ftpClient.logout();
}catch(IOExceptione){
}
}
}
returnflag;
}
}
以上所述是小编给大家介绍的JavaFTP上传下载删除功能实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!