1、最新java做的比较完善的FTP上传下载文件服务器源码【最新】java做的比较完善的FTP上传下载文件服务器源码 Filename: ftp.javaAuthor: leetsing(elove)Create date: 2004-08-30Use: connect to FTP server,then upload and download fileModify date: 2004-09-05 add to upload file 2004-09-13 add to download fileCopy right: Magisky Media Technology Co.,Ltd.*字串7
2、*/import cz.dhl.io.*;/import cz.dhl.ftp.*;import .ftp.*;import .*;import java.applet.*;import java.io.*;import java.io.IOException;import java.util.StringTokenizer;import .ftp.FtpClient;import java.util.ArrayList;public class ftp extends AppletFtpClient aftp;DataOutputStream outputs ;TelnetInputStre
3、am 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(RWFileDi
4、r,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()
5、i; 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 =
6、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); /retu
7、rn 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
8、(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) boole
9、an 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 (pareT
10、o(.bad) != 0) & (pareTo(.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 + filepathn
11、ame); /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
12、.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 boolea
13、n 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
14、 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.getFilePo
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1