您的位置 首页 趣闻

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

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

这是偶第一次写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+”文件!”);
}
/

免责声明:文章内容不代表本站立场,本站不对其内容的真实性、完整性、准确性给予任何担保、暗示和承诺,仅供读者参考,文章版权归原作者所有。如本文内容影响到您的合法权益(内容、图片等),请及时联系本站,我们会及时删除处理。

作者: dawei

【声明】:135手机网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

为您推荐

OPPO Find N展开一刻,两大创新技术打破折痕路上意难平

荣耀发布了荣耀60系列的新品,特别是荣耀60 Pro的5000万像素超感知AI前置镜头和1亿像素超清后置主摄,是生活和游玩记录的好帮手。多镜录像功能在荣耀50系列上就已开始引入,在荣耀60系列上还新增了AI手势识别,Vlog隔空换镜这一创新功能,对拍摄Vlog来说相当

敢为天下后,OPPO Find N折叠屏的底气何在?

聊起OPPO,给我的第一印象就是本分,而在刚刚发布的OPPO Find N折叠屏手机上,我看到的是OPPO敢为天下后的底气。 当大家还沉浸在前一天发布的马里亚纳MariSilicon X影像NPU芯片时,OPPO又在12月15日推出了旗下首款折叠屏手机产品OPPO Find N。这是一款传闻已

苹果iPhone性能为何逆天强?A系列芯片知道一下

苹果如期发布了iPhone 13系列,他们身上有着不少让竞争对手难以企及的闪光点:比如iPhone 13 Pro Max凭着比很多安卓旗舰还要小的电池容量,却实现了鹤立鸡群般的续航成绩,此前能做到这种表现的,也只有一部分大电池且性能较低的中端机型;而且iPhone 13系列

像素并非越高越好,为何5000万像素手机镜头倍受青欢迎

500万、800万、1200万像素,智能手机摄像头像素在早年发展的步伐相对还是比较缓慢,近来2000万、4800万、6400万、1.08亿像素接踵而来,手机摄像头的像素规格呈现出十分迅猛的迭代更新速度。然而在2019年首推量产1.08亿像素的智能手机后,至今手机上单颗传感

想拍更远?汇总今年搭载潜望式长焦摄像头的手机

部分手机摄影发烧友已不再满足普通的2x长焦摄像头,他们需要拍的更远,画质也不能妥协,于是能拍更远的潜望式长焦摄像头就应运而生了。不过当镜头焦距上去了,后置镜头模组的厚度就难以控制,如果强行塞下更长焦段的镜头,模组厚度将达到1~2cm,这样的手机估

返回顶部