JDK1.7以上javaFTP上传删除文件的实现方法
程序员文章站
2023-12-13 19:57:58
实例如下:
packagecom.itv.launcher.util;
importjava.io.file;
importjava.io.filein...
实例如下:
packagecom.itv.launcher.util; importjava.io.file; importjava.io.fileinputstream; importjava.io.filenotfoundexception; importjava.io.ioexception; importjava.net.inetsocketaddress; importjava.util.properties; importjava.util.stringtokenizer; importsun.net.telnetoutputstream; importsun.net.ftp.ftpclient; importsun.net.ftp.ftpprotocolexception; /** * ftp上传工具类 * * @author yanzhou * @version v1.0 */ publicclass ftputil { privatestatic ftpclient ftpclient = null; privatestatic final string url; privatestatic final int port; privatestatic final string user; privatestatic final string password; privatestatic final string remotefilepath; static{ properties ftppro = readftpproperties.getmsgfrompro(); 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 */ privatestatic void 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链接 */ publicstatic void closeftp() { try{ if(ftpclient != null) { ftpclient.close(); } }catch(ioexception e) { e.printstacktrace(); } } /** * 上传文件到ftp * @param file file文件,struts2从页面得到的file类型 * @param filepath 要保存在ftp上的路径(文件夹) * @param filename 文件名(test001.jpg) * @return 文件是否上传成功 * @throws exception */ publicstatic boolean upload(file file, string filepath, string filename) { telnetoutputstream to = null; fileinputstream fi = null; filepath = remotefilepath + constants.file_separator + filepath; try{ if(file != null) { connectftp(); if(!isdirexist(filepath.replace("\\","/"))) { createdir(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(exception e) { returnfalse; }finally{ 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 文件在ftp存储的路径 * @param filename 要删除的文件名称 * @return 是否删除成功 */ publicstatic boolean 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(filename); returntrue; }catch(exception e) { e.printstacktrace(); returnfalse; }finally{ closeftp(); } } /** * 检查文件夹是否存在 * * @param dir * @param ftpclient * @return */ privatestatic boolean isdirexist(string dir) { try{ ftpclient.changedirectory(dir); }catch(exception e) { returnfalse; } returntrue; } /** * 创建文件夹 * * @param dir * @param ftpclient * @throws exception */ privatestatic void createdir(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(exception e) { e = null; } } ftpclient.setbinarytype(); } }
2. 常量类,系统的路径分隔符
packagecom.itv.launcher.util; publicinterface constants { //路径分隔符 publicstatic string file_separator = system.getproperty("file.separator"); }
3. ftp链接的配置properties文件,包括用户名密码一些信息
#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上传删除文件的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。