您的位置:首页资讯教你一招 → java做的比较完善的FTP连接上传下载文件

java做的比较完善的FTP连接上传下载文件

时间:2004/10/15 1:20:00来源:本站整理作者:蓝点我要评论(0)

                   这是偶第一次写java程序实现公司要求的FTP数据同步,所有原代码如下,以供各位参考并提出宝贵意见!另外俺还有两个功能没有实现,有请高手指点一二:1、如何删除FTP服务器上已经下载过的文件?2、如何将上传的文件改名?上传时在文件名前加“temp_”,上传完毕后需要把文件名改回来。3、如何使用openPassiveDataConnection()方法将ftp连接更改为主动连接,因为要从unix系统配置的ftp服务器下载文件的话,就需要主动连接。 ******************************************************************************************************* Author: leetsing(elove)
Create date: 2004-08-30
Use: connect to FTP server,then upload and download file
Modify date: 2004-09-05 add to upload file
2004-09-13 add to download file
Copy right: Magisky Media Technology Co.,Ltd. */
//import cz.dhl.io.*;
//import cz.dhl.ftp.*;
import sun.net.ftp.*;
import sun.net.*;
import java.applet.*;
import java.io.*;
import java.io.IOException;
import java.util.StringTokenizer;
import sun.net.ftp.FtpClient;
import java.util.ArrayList; {
FtpClient aftp;
DataOutputStream outputs ;
TelnetInputStream ins;
TelnetOutputStream outs; public String a;
String hostname="";
private String path = "/"; {
String hostname = "192.168.0.56";
int port = 2121;
String uid = "lee";
String pwd = "lee";
String RWFileDir = "D:\\smsftp\\";//文件目录
ftp ft = new ftp();
ft.connect(RWFileDir,hostname,port,uid,pwd);
if (ft.aftp != null){
try {
ft.getNameList(RWFileDir);
}catch(IOException e)
{
System.out.println("下载文件出错:"+e);
}
} //上传文件
if (ft.aftp != null){
String sdir = RWFileDir + "subunsubfromsp\\";
File fdir = new File(sdir);
String FileName = ""; }
//System.out.println("成功上传的文件:");
//ft.showFileContents("subunsubfromsp\\");
}
// ft.deleFile(RWFileDir);
ft.stop(RWFileDir); {
this.hostname = hostname;
System.out.println("正在连接"+hostname+",请等待.....");
try{
aftp = new FtpClient(hostname,port);
aftp.login(uid,pwd);
aftp.binary();
//aftp.openPortDataConnection();
a = "连接主机:"+hostname+"成功!";
System.out.println(a);
}
catch(FtpLoginException e){
a="登陆主机:"+hostname+"失败!请检查用户名或密码是否正确:"+e;
System.out.println(a);
//return false;
}
catch (IOException e){
a="连接主机:"+hostname+"失败!请检查端口是否正确:"+e;
System.out.println(a);
//return false;
}
catch(SecurityException e)
{
a="无权限与主机:"+hostname+"连接!请检查是否有访问权限:"+e;
System.out.println(a);
//return false;
} return aftp;
} {
String message = "";
try {
if(aftp!=null){
aftp.closeServer();
message = "与主机"+hostname+"连接已断开!";
System.out.println(message);
log(RWFileDir,message);
}
}
catch(IOException e)
{
message = "与主机"+hostname+"断开连接失败!"+e;
System.out.println(message);
log(RWFileDir,message);
}
} public boolean downloadFile(String RWFileDir,String filepathname){
boolean result=true;
String message = "";
if (aftp != null)
{
System.out.println("正在下载文件"+filepathname+",请等待....");
String badfile = filepathname.substring(filepathname.length()-4,filepathname.length());
String badlog = filepathname.substring(filepathname.length()-7,filepathname.length());
String baddir = "";
if ((badfile.compareTo(".bad") != 0) && (badlog.compareTo(".badlog") != 0)){
baddir = "subunsubtosp\\";
}
else{
baddir = "bad\\";
}
String strdir = "subunsubtosp\\";
//System.out.println(RWFileDir + baddir + filepathname);
try{
//FtpClient fc=new FtpClient("192.168.0.56",2121);
//fc.login("lee","lee");
int ch;
File fi = new File(RWFileDir + baddir + filepathname);
//aftp.cd(strdir);
RandomAccessFile getFile = new RandomAccessFile(fi,"rw");
getFile.seek(0);
TelnetInputStream fget=aftp.get(strdir+filepathname);
DataInputStream puts = new DataInputStream(fget);
while ((ch = puts.read()) >= 0) {
getFile.write(ch); //s.delete(); getFile.close();
//fc.closeServer(); System.out.println(message);
log(RWFileDir,message);
}
catch(IOException e){
message = "下载"+filepathname+"文件到"+baddir +"目录失败!"+e;
System.out.println(message);
log(RWFileDir,message);
result = false ; }
else{
result = false;
}
return result;
} public boolean uploadFile(String RWFileDir,String filepathname){
boolean result=true;
String message = "";
if (aftp != null)
{
System.out.println("正在上传文件"+filepathname+",请等待...."); String fg =new String("\\subunsubfromsp\\");
int index = filepathname.lastIndexOf(fg);
String filename = filepathname.substring(index+1);
File localFile = new File(filepathname) ; //
sendFile.seek(0);
//改名上传temp_
filename = filename.substring(0,15)+"temp_"+filename.substring(15,filename.length());
outs = aftp.put(filename);
outputs = new DataOutputStream(outs);
while (sendFile.getFilePointer() < sendFile.length() )
{
ch = sendFile.read();
outputs.write(ch);
} outs.close();
sendFile.close(); System.out.println(message);
log(RWFileDir,message);
}
catch(IOException e){
message = "上传"+filepathname+"文件失败!"+e;
System.out.println(message);
log(RWFileDir,message);
result = false ; }
else{
result = false;
}
return result;
} File Old = new File(oldName); //oldName
File New = new File(newName); //newName
//aftp.renameTo(New);
//boolean Old.renameTo(File newName);
//System.out.println(Old);
//System.out.println(New); //try {
//取得ReadFile目录下的txt文件
String sdir = RWFileDir + "subunsubfromsp\\";
File fdir = new File(sdir);
String FileName = "";
int j = fdir.list().length; File file;
for(int i=0;i {
//删除subunsubfromsp中的txt文件
FileName = RWFileDir + "subunsubfromsp\\" + (fdir.list())[0];
file = new File(FileName);
file.delete();
System.out.println("已经成功删除"+FileName+"文件!");
}
//}
//catch (IOException e) {
// System.out.println("删除txt文件错误!");
// e.printStackTrace();
//}
} public void showFileContents(String strdir)
{
StringBuffer buf = new StringBuffer();
try {
aftp.cd(strdir);
ins= aftp.list();
while ((ch=ins.read())>=0){
buf.append((char)ch);
} }
catch(IOException e)
{
}
} BufferedReader dr = new BufferedReader(new InputStreamReader(aftp.list()));
ArrayList al = new ArrayList();
String s = "";
while ( (s = dr.readLine()) != null) {
al.add(s);
}
return al;
} if (aftp == null)
this.path = path;
else {
aftp.cd(path);
}
}
ArrayList al = new ArrayList();
String s = "";
while ( (s = dr.readLine()) != null) {
al.add(s);
s = s.substring(13,s.length());
isFile(s);
downloadFile(RWFileDir,s);
//String strFileDelF = aftp.nameList("subunsubtosp\\");
File fileDelF=new File(s);
fileDelF.delete();
}
return al;
//System.out.println(al.add(s));
}
return ( (String) parseLine(line).get(0)).indexOf("d") != -1;
} return!isDir(line);
}
ArrayList s1 = new ArrayList();
StringTokenizer st = new StringTokenizer(line, " ");
while (st.hasMoreTokens()) {
s1.add(st.nextToken());
}
return s1;
}
public static void log(String RWFileDir,String msg)
{
String message = "";
try {
java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.text.DateFormat dflog = new java.text.SimpleDateFormat("yyyyMMdd");
java.util.Date date = new java.util.Date() ;
String datestr = df.format(new java.util.Date()) ;
String datelog = dflog.format(new java.util.Date()) ;
//String datelog = datestr.substring(0,10);
//datelog = datelog.replace('-',' ');
FileWriter fwl = new FileWriter(RWFileDir + "CMSSftp"+datelog+".log",true);
PrintWriter outl = new PrintWriter(fwl);
outl.println(datestr + " " + msg);
outl.close();
fwl.close();
}catch (IOException e) {
message = "写log文件错误!"+e;
e.printStackTrace();
log(RWFileDir,message);
System.out.println(message);
}
}
}

相关阅读 Windows错误代码大全 Windows错误代码查询激活windows有什么用Mac QQ和Windows QQ聊天记录怎么合并 Mac QQ和Windows QQ聊天记录Windows 10自动更新怎么关闭 如何关闭Windows 10自动更新windows 10 rs4快速预览版17017下载错误问题Win10秋季创意者更新16291更新了什么 win10 16291更新内容windows10秋季创意者更新时间 windows10秋季创意者更新内容kb3150513补丁更新了什么 Windows 10补丁kb3150513是什么

文章评论
发表评论

热门文章 电视盒子怎么看百度云迅雷9怎么关闭右侧 迅迅雷99.99下载不完怎么网易云音乐怎么上传歌

最新文章 2022支付宝万能福扫福微信迎新春状态怎么设 微信小老虎头像怎么设置 微信ID后面设置小老抖音压岁钱卡怎么获得2022 抖音压岁钱获取方微博隐私保护怎么设置 微博隐私保护功能完善今日头条2022年集卡活动开启时间 今日头条2

人气排行 智学网怎么登录 智学网怎么查分数 智学网统教你怎么写电子邮箱格式?以163和QQ邮箱为例腾讯大王卡用了后悔怎么办 腾讯大王卡值得办缺少或丢失xinput1_3.dll解决方法,xinput1应用程序无法正常启动0xc000007b解决方法快播关闭怎么办?快播不能用了怎么看片将pdf文件转换为word文件的最简单方法如何破解QQ空间密码和权限