: :
packagecom.itv.launcher.util ؛ importjava.io.file ؛ importjava.io.fileInputStream ؛ importjava.io.filenotfoundException ؛ importjava.io.ioException ؛ importjava.net.inetsocketaddress ؛ importjava.util.properties Importsun.net.telnetOutputStream ؛ Importsun.net.ftp.ftpclient ؛ isirtsun.net.ftp.ftpprotocolexception ؛ /** * ftp 上传工具类 * * author yanzhou * version v1.0 */publicClassftputil {privatestaticftpclient ftpclient = null ؛ عنوان URL الخاص بالسلسلة الخاصة بالسلسلة. PrivatestaticFinal Int Port ؛ مستخدم سلسلة الخصوصية. كلمة مرور سلسلة PrivatestaticFinal ؛ سلسلة privatestaticfinal RemoteFilePath ؛ ثابت {properties ftppro = reghfproperties.getMsgfropROPRO () ؛ url = ftppro.getProperty ("ftp_url") ؛ port = integer.parseint (ftppro.getProperty ("ftp_port")) ؛ user = ftppro.getProperty ("ftp_user") ؛ password = ftppro.getProperty ("ftp_password") ؛ RemoteFilePath = ftppro.getProperty ("ftp_remote_filepath") ؛ } / ** * 链接 ftp * throws ftpprotocolexception * / privatestaticvoid connectftp () throwsftprotocolexception {try {ftpclient = ftpclient.create () ؛ ftpclient.connect (newInetSocketAddress (url ، port)) ؛ ftpclient.login (المستخدم ، password.tochararray ()) ؛ ftpclient.setBinaryType () ؛ if (! " }} catch (ioException e) {E.PrintStackTrace () ؛ }} / ** * 关闭 ftp 链接 * / publicStaticVoid closeftp () {try {if (ftpclient! = null) {ftpclient.close () ؛ }} catch (ioException e) {E.PrintStackTrace () ؛ } ستر باطل؛ fileInputStream fi = null ؛ FilePath = RemoteFilePath + Constants.file_separator + filepath ؛ حاول {if (file! = null) {connectftp () ؛ if (! isDirexist (filePath.replace ("//" ، "/"))) {createir (filePath.replace ("//" ، "/")) ؛ ftpClient.Changedirectory (filepath.replace ("//" ، "/")) ؛ } fi = newFileInputStream (ملف) ؛ إلى = (telnetOutputStream) ftpclient.putfilestream (اسم الملف ، صحيح) ؛ Byte [] bytes = newByte [1024] ؛ inti = fi.read (bytes) ؛ بينما (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 () ؛ }} clootftp () ؛ }} / ** * 删除 ftp 制定目录下的文件 * param filepath 文件在 ftp 存储的路径 * param filename 要删除的文件名称 * regurn 是否删除成功 * / publicstaticboolean deletefileftp (String filepath ، string filename) {try {connectfp () ؛ FilePath = RemoteFilePath + Constants.file_separator + filepath + constants.file_separator ؛ if (! isDirexist (filePath.replace ("//" ، "/"))) {returnfalse ؛ } ftpClient.Changedirectory (filepath.replace ("//" ، "/")) ؛ ftpclient.deletefile (اسم الملف) ؛ عودة } catch (استثناء e) {E.PrintStackTrace () ؛ ReturnFalse } أخيرًا {clootftp () ؛ }} / ** * 检查文件夹是否存在 * * param dir * param ftpclient * return * / privatestaticBoolean isDirexist (string dir) {try {ftpclient.changedirectory (dir) ؛ } catch (استثناء e) {returnfalse ؛ } returntrue ؛ } / ** * 创建文件夹 * * param dir * param ftpclient * throws استثناء * / privatestaticvoid createir (سلسلة dir) StringTokenizer s = newstringTokenizer (dir ، "/") ؛ // sign s.counttokens () ؛ string pathname = "" ؛ بينما (s.hasmoreElements ()) {pathName = pathName + "/" + (string) s.NextElement () ؛ جرب {ftpclient.makedirectory (pathName) ؛ } catch (استثناء e) {e = null ؛ }} ftpclient.setBinaryType () ؛ }}2. 常量类 , 系统的路径分隔符
packagecom.itv.launcher.util ؛ publicInterFaceConstants {// 路径分隔符 publicstaticstring file_separator = system.getProperty ("file.separator") ؛}3. خصائص FTP 链接的配置 文件 , 包括用户名密码一些信息
#ftp 的 ip 地址 ftp_url = 127.0.0.1#ftp 端口号 ftp_port = 1234#用户名 ftp_user = yanzhou#密码 ftp_password = abcdefg12345#ftp 账号目录 ftp_remote_filepath =
以上这篇 jdk1.7 以上 javaftp 上传删除文件的实现方法就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。