实例如下:
packagecom.itv.launcher.util; Importjava.io.file; importjava.io.fileinputStream; importjava.io.filenotfoundexception; importjava.io.ioexception; importjava.net.inetsocketaddress; importjava.util.properties; importjava.util.stlingtokenizer; importsun.net.telnetoutputStream; importsun.net.ftp.ftpclient; importsun.net.ftp.ftpprotocolexception; /** *ftp上传工具类 * * @author yanzhou * @version v1.0 */publicClassftputil {privatestaticftpclient ftpclient = null; privatestaticFinal String URL; privatestaticFinal intポート; privateStaticFinal Stringユーザー; privatestaticFinal文字列パスワード。 privatestaticFinal String remotefilepath; static {properties ftppro = readftpproperties.getmsgfrompro(); url = ftppro.getProperty( "ftp_url"); port = integer.parseint(ftppro.getProperty( "ftp_port")); user = ftppro.getProperty( "ftp_user");パスワード= ftppro.getProperty( "ftp_password"); remotefilepath = ftppro.getProperty( "ftp_remote_filepath"); } / ** *链接ftp * @throws ftpprotocolexception * / privatestaticvoid connectftp()throwsftpprotocolexception {try {ftpclient = ftpclient.create(); ftpclient.connect(newinetsocketAddress(url、port)); ftpclient.login(user、password.tochararray()); ftpclient.setBinaryType(); if(! ""。equals(remotefilepath)&& remotefilepath!= null){ftpclient.changedirectory(remotefilepath); }} catch(ioexception e){e.printstacktrace(); }} / ** *关闭ftp }} catch(ioexception e){e.printstacktrace(); }} / ** *上传文件到ftp * @paramファイルファイル文件、struts2从页面得到的ファイル * @param filepath fileinputStream fi = null; filepath = remotefilepath + constants.file_separator + filepath; try {if(file!= null){connectftp(); if(!isdirexist(filepath.replace( "//"、 "/"))){recutedir(filepath.replace( "//"、 "/")); ftpclient.changedirectory(filepath.replace( "//"、 "/")); } fi = newFileInputStream(file); to =(telnetoutputStream)ftpclient.putfilestream(filename、true); byte [] bytes = newbyte [1024]; inti = fi.read(bytes); while(i!= -1){to.write(bytes); i = fi.read(bytes); }} returnTrue; } catch(filenotfoundexception e1){returnfalse; } catch(ioException e2){returnfalse; } catch(例外e){returnfalse; }最後に{if(fi!= null){try {fi.close(); } catch(ioexception e){e.printstacktrace(); }} if(to!= null){try {to.flush(); to.close(); } catch(ioexception e){e.printstacktrace(); }} closeftp(); }} / ** *删除ftp制定目录下的文件 * @param filepath filepath = remotefilepath + constants.file_separator + filepath + constants.file_separator; if(!isdirexist(filepath.replace( "//"、 "/"))){returnfalse; } ftpclient.changedirectory(filepath.replace( "//"、 "/")); ftpclient.deletefile(filename); returntrue; } catch(Exception e){e.printstacktrace(); ReturnFalse; }最後に{closeftp(); }} / ** * * * * @param dir * @param ftpclient * @return * / privatestaticboolean isdirexist(string dir){try {ftpclient.changedirectory(dir); } catch(例外e){returnfalse; } returntrue; } / ** *创建文件夹 * * @param dir * @param ftpclient * @throws例外 * / privatestaticvoid recutedir(string dir)throwsexception {ftpclient.setasciitype(); StringTokenizer S = NewStringTokenizer(dir、 "/"); // sign s.counttokens(); string pathname = ""; while(s.hasmoreElements()){pathname = pathname + "/" +(string)s.nextelement(); try {ftpclient.makedirectory(pathname); } catch(例外e){e = null; }} ftpclient.setBinaryType(); }}2。常量类、系统的路径分隔符
packagecom.itv.launcher.util; publicInterfaceconStants {//3。FTP链接的配置プロパティ文件、包括用户名密码一些信息
#ftp
以上这篇jdk1.7