设为首页 收藏本站
查看: 1147|回复: 0

java做的比较完善的FTP上传下载文件服务器源码

[复制链接]

尚未签到

发表于 2015-5-27 09:29:31 | 显示全部楼层 |阅读模式
Filename:  ftp.java
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.
*******************************************************************************************************
字串7
*/
//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;
public class ftp  extends Applet
{
FtpClient aftp;
DataOutputStream outputs ;
TelnetInputStream ins;
TelnetOutputStream outs;
int ch;
public String a;
String hostname="";
private String path = "/";
public static void main(String[] args)
{
String hostname = "192.168.0.56";
int port = 2121;
String uid = "lee";
String pwd = "lee";
String RWFileDir = "D:\\smsftp\\";//文件目录
//连接ftp服务器
ftp ft = new  ftp();
ft.connect(RWFileDir,hostname,port,uid,pwd);
字串3

//下载文件
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 = "";
for(int i=0;i    FileName = sdir + (fdir.list());
ft.uploadFile(RWFileDir,FileName);
}
//System.out.println("成功上传的文件:");
//ft.showFileContents("subunsubfromsp\\");
  }
//删除subunsubfromsp目录下已经上传的文件文件 字串6
//  ft.deleFile(RWFileDir);
//断开服务器连接
  ft.stop(RWFileDir);
}
public FtpClient connect(String RWFileDir,String hostname,int port,String uid,String pwd)
{
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; 字串3
System.out.println(a);
//return false;
  }
catch(SecurityException e)
{
a="无权限与主机:"+hostname+"连接!请检查是否有访问权限:"+e;
System.out.println(a);
//return false;
  }
log(RWFileDir,a);
return aftp;
}
public void stop(String RWFileDir)
{
String message = "";
try {
if(aftp!=null){
aftp.closeServer();
message = "与主机"+hostname+"连接已断开!";
System.out.println(message);
log(RWFileDir,message);
}
}
catch(IOException e)
{
message = "与主机"+hostname+"断开连接失败!"+e; 字串2
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\\";
} 字串9
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(); 字串1
   
fget.close();
getFile.close();
//fc.closeServer();

message = "下载"+filepathname+"文件到"+baddir +"目录成功!";
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; 字串8
String message = "";
if (aftp != null)
{
System.out.println("正在上传文件"+filepathname+",请等待....");
try{
String fg =new  String("\\subunsubfromsp\\");
int index = filepathname.lastIndexOf(fg);
String filename = filepathname.substring(index+1);
File localFile = new File(filepathname) ;
RandomAccessFile sendFile = new RandomAccessFile(filepathname,"r");
//
    sendFile.seek(0);
//改名上传temp_
filename = filename.substring(0,15)+"temp_"+filename.substring(15,filename.length());
outs = aftp.put(filename);
outputs = new DataOutputStream(outs); 字串8
while (sendFile.getFilePointer() < sendFile.length() )
{
ch = sendFile.read();
outputs.write(ch);
}
rename(filename.substring(15,filename.length()),filename.substring(20,filename.length()));
outs.close();
sendFile.close();
message = "上传"+filepathname+"文件成功!";
System.out.println(message);
log(RWFileDir,message);
}
catch(IOException e){
message = "上传"+filepathname+"文件失败!"+e;
System.out.println(message);
log(RWFileDir,message);
result = false ;
字串1

}
}
else{
result = false;
}
return result;
}
public void rename(String oldName,String newName){
//aftp.renameTo(oldName,newName);
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);
  
}
public static void deleFile(String RWFileDir) {
//try {
//取得ReadFile目录下的txt文件
String sdir = RWFileDir + "subunsubfromsp\\";
File fdir = new File(sdir);
String FileName = "";
int j = fdir.list().length;
System.out.println(sdir+"目录下要删除的文件数:"+fdir.list().length);
字串6
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+"文件!");
}
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-71103-1-1.html 上篇帖子: 使用asp.net FtpWebRequest 实现FTP常用功能 下篇帖子: ftp限速设置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表