实例如下 :
packagecom.itv.launcher.util; importjava.io.file; importjava.io.fileinputstream; importjava.io.filenotfoundexception; importjava.io.ioexception; importjava.net.inetSocketAddress; importjava.util.properties; impormava.util.StringKeenCekingRess; 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 Port; Пользователь PrivateStaticfinal String; PrivateStaticfinal String Password; PrivateStaticfinal String remoteFilePath; static {свойства 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, порт)); ftpclient.login (user, password.thararray ()); ftpclient.setBinaryType (); if (! "". equals (remotefilepath) && remotefilepath! = null) {ftpclient.changedirectory (remotefilepath); }} catch (ioException e) {e.printstacktrace (); }} / ** * 关闭 ftp 链接 * / publicstaticvoid hailfftp () {try {if (ftpclient! = Null) {ftpclient.close (); }} catch (ioException e) {e.printstacktrace (); }} / ** * 上传文件到 ftp * @param файл файла 文件 , struts2 从页面得到的 файл 类型 * @param filepath 要保存在 ftp 上的路径 (文件夹) * @param filename 文件名 (test001.jpg) * @return 文件是否上传成功 * @Throws Exception * / publicStaticboolean upload (file file, string, string, string) нулевой; FileInputStream fi = null; filePath = remoteFilePath + constants.file_separator + filePath; попробуйте {if (file! = null) {connectftp (); if (! Isdirexist (filePath.replace ("//", "/"))) {cenectir (filePath.replace ("//", "/")); ftpclient.changedirectory (filepath.replace ("//", "/")); } fi = newFileInputStream (file); to = (telnetOutputStream) ftpClient.putFileStream (имя файла, true); Byte [] bytes = newbyte [1024]; inti = fi.read (байты); while (i! = -1) {to.write (байты); i = fi.read (байты); }} returntrue; } catch (filenotFoundException e1) {returnfalse; } catch (ioException e2) {returnfalse; } catch (Exception 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 (); }} haillftp (); }} / ** * 删除 ftp 制定目录下的文件 * @param filePath 文件在 ftp 存储的路径 * @param filename 要删除的文件名称 * @return 是否删除成功 * / publicstaticboolean deletefileftp (String filePath, String FileName) {try {connectftp (); filePath = remoteFilePath + constants.file_separator + filepath + constants.file_separator; if (! Isdirexist (filepath.replace ("//", "/"))) {returnfalse; } ftpclient.changedirectory (filepath.replace ("//", "/")); ftpclient.deletefile (имя файла); returntrue; } catch (Exception e) {e.printstackTrace (); returnfalse; } наконец {haillftp (); }} / ** * 检查文件夹是否存在 * * @param dir * @param ftpclient * @return * / privatestaticboolean isdirexist (string dir) {try {ftpclient.changedirectory (dir); } catch (Exception e) {returnfalse; } returntrue; } / ** * 创建文件夹 * * @param dir * @param ftpclient * @throhs exception * / privateStaticvoid creativeir (String dir) thropSexception {ftpclient.setasciitype (); StringTokenizer s = NewStringTokenizer (dir, "/"); // подписать S.CountTokens (); String pathname = ""; while (s.hasmoreelements ()) {pathname = pathname + "/" + (string) s.nextelement (); try {ftpclient.makedirectory (pathname); } catch (Exception 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 上传删除文件的实现方法就是小编分享给大家的全部内容了 , 希望能给大家一个参考 也希望大家多多支持武林网。 也希望大家多多支持武林网。 也希望大家多多支持武林网。