基于 Apache Commons Net 实现javaFTP上传
fmms
13年前
<div> <p><strong><span style="font-size:small;font-weight:normal;"><span style="font-size:12px;">使用Apache Commons Net包实现javaFTP来上传目录</span></span></strong></p> </div> <div> </div> <div> 复杂的地方体现在文件夹的上传。 </div> <div> </div> <div> 在前面层用Apache Commons Net工具包实现了Java FTP单个文件的上传下载功能,这次,实现的是对一个本地目录的上传,实现费了很大劲,虽然实现了 ,但是感觉还是对此封装不是非常满意,改用了其他的FTP客户端工具。将这个半成品放出来供各位研究吧。 </div> <div> </div> <div> ftpconfig.properties </div> <div> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:4px;line-height:16px;background-color:#eeeeee;padding-left:4px;width:98%;padding-right:4px;font-family:verdana,宋体;color:#000000;font-size:10pt;word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:4px;"> username=admin <br /> password=123 <br /> ip=192.168.14.117 <br /> port=21 </div> </div> <div> </div> <div> <div style="border-bottom:#cccccc 1px solid;border-left:#cccccc 1px solid;padding-bottom:4px;line-height:16px;background-color:#eeeeee;padding-left:4px;width:98%;padding-right:4px;font-family:verdana,宋体;color:#000000;font-size:10pt;word-break:break-all;border-top:#cccccc 1px solid;border-right:#cccccc 1px solid;padding-top:4px;"> <span style="color:#0000ff;">package</span> comftp; <br /> <br /> <span style="color:#0000ff;">import</span> org.apache.commons.logging.Log; <br /> <span style="color:#0000ff;">import</span> org.apache.commons.logging.LogFactory; <br /> <span style="color:#0000ff;">import</span> org.apache.commons.net.ftp.FTP; <br /> <span style="color:#0000ff;">import</span> org.apache.commons.net.ftp.FTPClient; <br /> <span style="color:#0000ff;">import</span> org.apache.commons.net.ftp.FTPClientConfig; <br /> <span style="color:#0000ff;">import</span> org.apache.commons.net.ftp.FTPReply; <br /> <br /> <span style="color:#0000ff;">import</span> java.io.*; <br /> <span style="color:#0000ff;">import</span> java.net.SocketException; <br /> <span style="color:#0000ff;">import</span> java.text.SimpleDateFormat; <br /> <span style="color:#0000ff;">import</span> java.util.Properties; <br /> <br /> <span style="color:#008000;">/** <br /> * Created by IntelliJ IDEA. <br /> * <br /> * @author leizhimin 2008-9-12 10:32:39 <br /> */</span> <br /> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> FtpTest { <br /> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> Log logger = LogFactory.getLog(FtpTest. <span style="color:#0000ff;">class</span>); <br /> <br /> <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> String userName; <span style="color:#008000;">//FTP 登录用户名 </span> <br /> <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> String password; <span style="color:#008000;">//FTP 登录密码 </span> <br /> <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> String ip; <span style="color:#008000;">//FTP 服务器地址IP地址 </span> <br /> <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">int</span> port; <span style="color:#008000;">//FTP 端口 </span> <br /> <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> Properties property = <span style="color:#0000ff;">null</span>; <span style="color:#008000;">//属性集 </span> <br /> <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> String configFile = <span style="color:#800000;">"E:\\test\\comftp\\ftpconfig.properties"</span>; <span style="color:#008000;">//配置文件的路径名 </span> <br /> <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> FTPClient ftpClient = <span style="color:#0000ff;">null</span>; <span style="color:#008000;">//FTP 客户端代理 </span> <br /> <span style="color:#008000;">//时间格式化 </span> <br /> <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> SimpleDateFormat dateFormat = <span style="color:#0000ff;">new</span> SimpleDateFormat( <span style="color:#800000;">"yyyy-MM-dd hh:mm"</span>); <br /> <span style="color:#008000;">//FTP状态码 </span> <br /> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">int</span> i = 1; <br /> <br /> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> main(String[] args) { <br /> connectServer(); <br /> setFileType(FTP.BINARY_FILE_TYPE); <span style="color:#008000;">// 设置传输二进制文件 </span> <br /> uploadManyFile( <span style="color:#0000ff;">new</span> File( <span style="color:#800000;">"C:\\ooo\\upx"</span>), <span style="color:#0000ff;">new</span> File( <span style="color:#800000;">"C:\\ooo\\upx"</span>), <span style="color:#800000;">"/admin/ttt"</span>); <br /> closeConnect(); <span style="color:#008000;">// 关闭连接 </span> <br /> } <br /> <br /> <span style="color:#008000;">/** <br /> * 上传单个文件,并重命名 <br /> * <br /> * @param localFile--本地文件路径 <br /> * @param distFolder--新的文件名,可以命名为空"" <br /> * @return true 上传成功,false 上传失败 <br /> */</span> <br /> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">boolean</span> uploadFile(File localFile, <span style="color:#0000ff;">final</span> File localRootFile, <span style="color:#0000ff;">final</span> String distFolder) { <br /> System.out.println( <span style="color:#800000;">" -------------------------"</span>); <br /> <span style="color:#0000ff;">boolean</span> flag = <span style="color:#0000ff;">true</span>; <br /> <span style="color:#0000ff;">try</span> { <br /> connectServer(); <br /> ftpClient.setFileType(FTP.BINARY_FILE_TYPE); <br /> ftpClient.enterLocalPassiveMode(); <br /> ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE); <br /> InputStream input = <span style="color:#0000ff;">new</span> FileInputStream(localFile); <br /> <span style="color:#008000;">// if (input == null) { </span> <br /> <span style="color:#008000;">// System.out.println("本地文件"+localFile.getPath()+"不存在!"); </span> <br /> <span style="color:#008000;">// } </span> <br /> <span style="color:#008000;">// if (newFileName.trim().equals("")) { </span> <br /> <span style="color:#008000;">// newFileName = localFile.getName(); </span> <br /> <span style="color:#008000;">// } </span> <br /> <br /> String furi1 = localFile.getParentFile().getAbsoluteFile().toURI().toString(); <br /> String furi2 = localRootFile.getParentFile().getAbsoluteFile().toURI().toString(); <br /> <br /> String objFolder = distFolder + File.separator + furi1.substring(furi2.length()); <br /> <br /> ftpClient.changeWorkingDirectory( <span style="color:#800000;">"/"</span>); <br /> ftpClient.makeDirectory(objFolder); <br /> System.out.println( <span style="color:#800000;">"a>>>>>>> : "</span> + distFolder + File.separator + localFile.getParent()); <br /> System.out.println( <span style="color:#800000;">"x>>>>>>> : "</span> + objFolder); <br /> ftpClient.changeWorkingDirectory(objFolder); <br /> <br /> System.out.println( <span style="color:#800000;">"b>>>>>>> : "</span> + localFile.getPath() + <span style="color:#800000;">" "</span> + ftpClient.printWorkingDirectory()); <br /> flag = ftpClient.storeFile(localFile.getName(), input); <br /> <span style="color:#0000ff;">if</span> (flag) { <br /> System.out.println( <span style="color:#800000;">"上传文件成功!"</span>); <br /> } <span style="color:#0000ff;">else</span> { <br /> System.out.println( <span style="color:#800000;">"上传文件失败!"</span>); <br /> } <br /> input.close(); <br /> } <span style="color:#0000ff;">catch</span> (IOException e) { <br /> e.printStackTrace(); <br /> logger.debug( <span style="color:#800000;">"本地文件上传失败!"</span>, e); <br /> } <span style="color:#0000ff;">catch</span> (Exception e) { <br /> e.printStackTrace(); <br /> } <br /> <span style="color:#0000ff;">return</span> flag; <br /> } <br /> <br /> <span style="color:#008000;">/** <br /> * 上传多个文件 <br /> * <br /> * @param localFile--本地文件夹路径 <br /> * @return true 上传成功,false 上传失败 <br /> */</span> <br /> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> String uploadManyFile(String localFile) { <br /> <span style="color:#0000ff;">boolean</span> flag = <span style="color:#0000ff;">true</span>; <br /> StringBuffer strBuf = <span style="color:#0000ff;">new</span> StringBuffer(); <br /> <span style="color:#0000ff;">try</span> { <br /> connectServer(); <br /> File file = <span style="color:#0000ff;">new</span> File(localFile); <span style="color:#008000;">// 在此目录中找文件 </span> <br /> File fileList[] = file.listFiles(); <br /> <span style="color:#0000ff;">for</span> (File f : fileList) { <br /> <span style="color:#0000ff;">if</span> (f.isDirectory()) { <span style="color:#008000;">// 文件夹中还有文件夹 </span> <br /> uploadManyFile(f.getAbsolutePath()); <br /> } <span style="color:#0000ff;">else</span> { <br /> } <br /> <span style="color:#0000ff;">if</span> (!flag) { <br /> strBuf.append(f.getName() + <span style="color:#800000;">"\r\n"</span>); <br /> } <br /> } <br /> System.out.println(strBuf.toString()); <br /> } <span style="color:#0000ff;">catch</span> (NullPointerException e) { <br /> e.printStackTrace(); <br /> } <span style="color:#0000ff;">catch</span> (Exception e) { <br /> e.printStackTrace(); <br /> logger.debug( <span style="color:#800000;">"本地文件上传失败!"</span>, e); <br /> } <br /> <span style="color:#0000ff;">return</span> strBuf.toString(); <br /> } <br /> <br /> <span style="color:#008000;">/** <br /> * 上传多个文件 <br /> * <br /> * @param localFile,--本地文件夹路径 <br /> * @param distFolder--目标路径 <br /> * @return true 上传成功,false 上传失败 <br /> */</span> <br /> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> String uploadManyFile(File localFile, <span style="color:#0000ff;">final</span> File localRootFile, <span style="color:#0000ff;">final</span> String distFolder) { <br /> System.out.println( <span style="color:#800000;">"-------------------------"</span>); <br /> <span style="color:#0000ff;">boolean</span> flag = <span style="color:#0000ff;">true</span>; <br /> StringBuffer strBuf = <span style="color:#0000ff;">new</span> StringBuffer(); <br /> <span style="color:#0000ff;">int</span> n = 0; <br /> <span style="color:#0000ff;">try</span> { <br /> connectServer(); <br /> ftpClient.makeDirectory(distFolder + File.separator + localFile.getParent()); <br /> File fileList[] = localFile.listFiles(); <br /> <span style="color:#0000ff;">for</span> (File upfile : fileList) { <br /> <span style="color:#0000ff;">if</span> (upfile.isDirectory()) { <span style="color:#008000;">// 文件夹中还有文件夹 </span> <br /> uploadManyFile(upfile, localRootFile, distFolder); <br /> } <span style="color:#0000ff;">else</span> { <br /> flag = uploadFile(upfile, localRootFile, distFolder); <br /> } <br /> <span style="color:#0000ff;">if</span> (!flag) { <br /> strBuf.append(upfile.getName() + <span style="color:#800000;">"\r\n"</span>); <br /> } <br /> } <br /> System.out.println(strBuf.toString()); <br /> } <span style="color:#0000ff;">catch</span> (NullPointerException e) { <br /> e.printStackTrace(); <br /> logger.debug( <span style="color:#800000;">"本地文件上传失败!找不到上传文件!"</span>, e); <br /> } <span style="color:#0000ff;">catch</span> (Exception e) { <br /> e.printStackTrace(); <br /> logger.debug( <span style="color:#800000;">"本地文件上传失败!"</span>, e); <br /> } <br /> <span style="color:#0000ff;">return</span> strBuf.toString(); <br /> } <br /> <br /> <span style="color:#008000;">/** <br /> * 下载文件 <br /> * <br /> * @param remoteFileName --服务器上的文件名 <br /> * @param localFileName--本地文件名 <br /> * @return true 下载成功,false 下载失败 <br /> */</span> <br /> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">boolean</span> loadFile(String remoteFileName, String localFileName) { <br /> <span style="color:#0000ff;">boolean</span> flag = <span style="color:#0000ff;">true</span>; <br /> connectServer(); <br /> <span style="color:#008000;">// 下载文件 </span> <br /> BufferedOutputStream buffOut = <span style="color:#0000ff;">null</span>; <br /> <span style="color:#0000ff;">try</span> { <br /> buffOut = <span style="color:#0000ff;">new</span> BufferedOutputStream( <span style="color:#0000ff;">new</span> FileOutputStream(localFileName)); <br /> flag = ftpClient.retrieveFile(remoteFileName, buffOut); <br /> } <span style="color:#0000ff;">catch</span> (Exception e) { <br /> e.printStackTrace(); <br /> logger.debug( <span style="color:#800000;">"本地文件下载失败!"</span>, e); <br /> } <span style="color:#0000ff;">finally</span> { <br /> <span style="color:#0000ff;">try</span> { <br /> <span style="color:#0000ff;">if</span> (buffOut != <span style="color:#0000ff;">null</span>) <br /> buffOut.close(); <br /> } <span style="color:#0000ff;">catch</span> (Exception e) { <br /> e.printStackTrace(); <br /> } <br /> } <br /> <span style="color:#0000ff;">return</span> flag; <br /> } <br /> <br /> <span style="color:#008000;">/** <br /> * 删除一个文件 <br /> */</span> <br /> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">boolean</span> deleteFile(String filename) { <br /> <span style="color:#0000ff;">boolean</span> flag = <span style="color:#0000ff;">true</span>; <br /> <span style="color:#0000ff;">try</span> { <br /> connectServer(); <br /> flag = ftpClient.deleteFile(filename); <br /> <span style="color:#0000ff;">if</span> (flag) { <br /> System.out.println( <span style="color:#800000;">"删除文件成功!"</span>); <br /> } <span style="color:#0000ff;">else</span> { <br /> System.out.println( <span style="color:#800000;">"删除文件失败!"</span>); <br /> } <br /> } <span style="color:#0000ff;">catch</span> (IOException ioe) { <br /> ioe.printStackTrace(); <br /> } <br /> <span style="color:#0000ff;">return</span> flag; <br /> } <br /> <br /> <span style="color:#008000;">/** <br /> * 删除目录 <br /> */</span> <br /> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> deleteDirectory(String pathname) { <br /> <span style="color:#0000ff;">try</span> { <br /> connectServer(); <br /> File file = <span style="color:#0000ff;">new</span> File(pathname); <br /> <span style="color:#0000ff;">if</span> (file.isDirectory()) { <br /> File file2[] = file.listFiles(); <br /> } <span style="color:#0000ff;">else</span> { <br /> deleteFile(pathname); <br /> } <br /> ftpClient.removeDirectory(pathname); <br /> } <span style="color:#0000ff;">catch</span> (IOException ioe) { <br /> ioe.printStackTrace(); <br /> } <br /> } <br /> <br /> <span style="color:#008000;">/** <br /> * 删除空目录 <br /> */</span> <br /> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> deleteEmptyDirectory(String pathname) { <br /> <span style="color:#0000ff;">try</span> { <br /> connectServer(); <br /> ftpClient.removeDirectory(pathname); <br /> } <span style="color:#0000ff;">catch</span> (IOException ioe) { <br /> ioe.printStackTrace(); <br /> } <br /> } <br /> <br /> <span style="color:#008000;">/** <br /> * 列出服务器上文件和目录 <br /> * <br /> * @param regStr --匹配的正则表达式 <br /> */</span> <br /> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> listRemoteFiles(String regStr) { <br /> connectServer(); <br /> <span style="color:#0000ff;">try</span> { <br /> String files[] = ftpClient.listNames(regStr); <br /> <span style="color:#0000ff;">if</span> (files == <span style="color:#0000ff;">null</span> || files.length == 0) <br /> System.out.println( <span style="color:#800000;">"没有任何文件!"</span>); <br /> <span style="color:#0000ff;">else</span> { <br /> <span style="color:#0000ff;">for</span> ( <span style="color:#0000ff;">int</span> i = 0; i < files.length; i++) { <br /> System.out.println(files[i]); <br /> } <br /> } <br /> } <span style="color:#0000ff;">catch</span> (Exception e) { <br /> e.printStackTrace(); <br /> } <br /> } <br /> <br /> <span style="color:#008000;">/** <br /> * 列出Ftp服务器上的所有文件和目录 <br /> */</span> <br /> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> listRemoteAllFiles() { <br /> connectServer(); <br /> <span style="color:#0000ff;">try</span> { <br /> String[] names = ftpClient.listNames(); <br /> <span style="color:#0000ff;">for</span> ( <span style="color:#0000ff;">int</span> i = 0; i < names.length; i++) { <br /> System.out.println(names[i]); <br /> } <br /> } <span style="color:#0000ff;">catch</span> (Exception e) { <br /> e.printStackTrace(); <br /> } <br /> } <br /> <br /> <span style="color:#008000;">/** <br /> * 关闭连接 <br /> */</span> <br /> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> closeConnect() { <br /> <span style="color:#0000ff;">try</span> { <br /> <span style="color:#0000ff;">if</span> (ftpClient != <span style="color:#0000ff;">null</span>) { <br /> ftpClient.logout(); <br /> ftpClient.disconnect(); <br /> } <br /> } <span style="color:#0000ff;">catch</span> (Exception e) { <br /> e.printStackTrace(); <br /> } <br /> } <br /> <br /> <span style="color:#008000;">/** <br /> * 设置配置文件 <br /> * <br /> * @param configFile <br /> */</span> <br /> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> setConfigFile(String configFile) { <br /> FtpTest.configFile = configFile; <br /> } <br /> <br /> <span style="color:#008000;">/** <br /> * 设置传输文件的类型[文本文件或者二进制文件] <br /> * <br /> * @param fileType--BINARY_FILE_TYPE、ASCII_FILE_TYPE <br /> * <br /> */</span> <br /> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> setFileType( <span style="color:#0000ff;">int</span> fileType) { <br /> <span style="color:#0000ff;">try</span> { <br /> connectServer(); <br /> ftpClient.setFileType(fileType); <br /> } <span style="color:#0000ff;">catch</span> (Exception e) { <br /> e.printStackTrace(); <br /> } <br /> } <br /> <br /> <span style="color:#008000;">/** <br /> * 扩展使用 <br /> * <br /> * @return ftpClient <br /> */</span> <br /> <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">static</span> FTPClient getFtpClient() { <br /> connectServer(); <br /> <span style="color:#0000ff;">return</span> ftpClient; <br /> } <br /> <br /> <span style="color:#008000;">/** <br /> * 设置参数 <br /> * <br /> * @param configFile --参数的配置文件 <br /> */</span> <br /> <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> setArg(String configFile) { <br /> property = <span style="color:#0000ff;">new</span> Properties(); <br /> BufferedInputStream inBuff = <span style="color:#0000ff;">null</span>; <br /> <span style="color:#0000ff;">try</span> { <br /> File file = <span style="color:#0000ff;">new</span> File(configFile); <br /> inBuff = <span style="color:#0000ff;">new</span> BufferedInputStream( <span style="color:#0000ff;">new</span> FileInputStream(file)); <br /> property.load(inBuff); <br /> userName = property.getProperty( <span style="color:#800000;">"username"</span>); <br /> password = property.getProperty( <span style="color:#800000;">"password"</span>); <br /> ip = property.getProperty( <span style="color:#800000;">"ip"</span>); <br /> port = Integer.parseInt(property.getProperty( <span style="color:#800000;">"port"</span>)); <br /> } <span style="color:#0000ff;">catch</span> (FileNotFoundException e1) { <br /> System.out.println( <span style="color:#800000;">"配置文件 "</span> + configFile + <span style="color:#800000;">" 不存在!"</span>); <br /> } <span style="color:#0000ff;">catch</span> (IOException e) { <br /> System.out.println( <span style="color:#800000;">"配置文件 "</span> + configFile + <span style="color:#800000;">" 无法读取!"</span>); <br /> } <br /> } <br /> <br /> <span style="color:#008000;">/** <br /> * 连接到服务器 <br /> * <br /> * @return true 连接服务器成功,false 连接服务器失败 <br /> */</span> <br /> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">boolean</span> connectServer() { <br /> <span style="color:#0000ff;">boolean</span> flag = <span style="color:#0000ff;">true</span>; <br /> <span style="color:#0000ff;">if</span> (ftpClient == <span style="color:#0000ff;">null</span>) { <br /> <span style="color:#0000ff;">int</span> reply; <br /> <span style="color:#0000ff;">try</span> { <br /> setArg(configFile); <br /> ftpClient = <span style="color:#0000ff;">new</span> FTPClient(); <br /> ftpClient.setControlEncoding( <span style="color:#800000;">"GBK"</span>); <br /> ftpClient.setDefaultPort(port); <br /> ftpClient.configure(getFtpConfig()); <br /> ftpClient.connect(ip); <br /> ftpClient.login(userName, password); <br /> ftpClient.setDefaultPort(port); <br /> <span style="color:#008000;">//System.out.print(ftpClient.getReplyString()); </span> <br /> reply = ftpClient.getReplyCode(); <br /> ftpClient.setDataTimeout(120000); <br /> <br /> <span style="color:#0000ff;">if</span> (!FTPReply.isPositiveCompletion(reply)) { <br /> ftpClient.disconnect(); <br /> System.err.println( <span style="color:#800000;">"FTP server refused connection."</span>); <br /> <span style="color:#008000;">// logger.debug("FTP 服务拒绝连接!"); </span> <br /> flag = <span style="color:#0000ff;">false</span>; <br /> } <br /> <span style="color:#008000;">// System.out.println(i); </span> <br /> i++; <br /> } <span style="color:#0000ff;">catch</span> (SocketException e) { <br /> flag = <span style="color:#0000ff;">false</span>; <br /> e.printStackTrace(); <br /> System.err.println( <span style="color:#800000;">"登录ftp服务器 "</span> + ip + <span style="color:#800000;">" 失败,连接超时!"</span>); <br /> } <span style="color:#0000ff;">catch</span> (IOException e) { <br /> flag = <span style="color:#0000ff;">false</span>; <br /> e.printStackTrace(); <br /> System.err.println( <span style="color:#800000;">"登录ftp服务器 "</span> + ip + <span style="color:#800000;">" 失败,FTP服务器无法打开!"</span>); <br /> } <br /> } <br /> <span style="color:#0000ff;">return</span> flag; <br /> } <br /> <br /> <span style="color:#008000;">/** <br /> * 进入到服务器的某个目录下 <br /> * <br /> * @param directory <br /> */</span> <br /> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> changeWorkingDirectory(String directory) { <br /> <span style="color:#0000ff;">try</span> { <br /> connectServer(); <br /> ftpClient.changeWorkingDirectory(directory); <br /> } <span style="color:#0000ff;">catch</span> (IOException ioe) { <br /> ioe.printStackTrace(); <br /> } <br /> } <br /> <br /> <span style="color:#008000;">/** <br /> * 返回到上一层目录 <br /> */</span> <br /> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> changeToParentDirectory() { <br /> <span style="color:#0000ff;">try</span> { <br /> connectServer(); <br /> ftpClient.changeToParentDirectory(); <br /> } <span style="color:#0000ff;">catch</span> (IOException ioe) { <br /> ioe.printStackTrace(); <br /> } <br /> } <br /> <br /> <span style="color:#008000;">/** <br /> * 重命名文件 <br /> * <br /> * @param oldFileName --原文件名 <br /> * @param newFileName --新文件名 <br /> */</span> <br /> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> renameFile(String oldFileName, String newFileName) { <br /> <span style="color:#0000ff;">try</span> { <br /> connectServer(); <br /> ftpClient.rename(oldFileName, newFileName); <br /> } <span style="color:#0000ff;">catch</span> (IOException ioe) { <br /> ioe.printStackTrace(); <br /> } <br /> } <br /> <br /> <span style="color:#008000;">/** <br /> * 设置FTP客服端的配置--一般可以不设置 <br /> * <br /> * @return ftpConfig <br /> */</span> <br /> <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> FTPClientConfig getFtpConfig() { <br /> FTPClientConfig ftpConfig = <span style="color:#0000ff;">new</span> FTPClientConfig(FTPClientConfig.SYST_UNIX); <br /> ftpConfig.setServerLanguageCode(FTP.DEFAULT_CONTROL_ENCODING); <br /> <span style="color:#0000ff;">return</span> ftpConfig; <br /> } <br /> <br /> <span style="color:#008000;">/** <br /> * 转码[ISO-8859-1 -> GBK] 不同的平台需要不同的转码 <br /> * <br /> * @param obj <br /> * @return "" <br /> */</span> <br /> <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> String iso8859togbk(Object obj) { <br /> <span style="color:#0000ff;">try</span> { <br /> <span style="color:#0000ff;">if</span> (obj == <span style="color:#0000ff;">null</span>) <br /> <span style="color:#0000ff;">return</span> ""; <br /> <span style="color:#0000ff;">else</span> <br /> <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">new</span> String(obj.toString().getBytes( <span style="color:#800000;">"iso-8859-1"</span>), <span style="color:#800000;">"GBK"</span>); <br /> } <span style="color:#0000ff;">catch</span> (Exception e) { <br /> <span style="color:#0000ff;">return</span> ""; <br /> } <br /> } <br /> <br /> <span style="color:#008000;">/** <br /> * 在服务器上创建一个文件夹 <br /> * <br /> * @param dir 文件夹名称,不能含有特殊字符,如 \ 、/ 、: 、* 、?、 "、 <、>... <br /> */</span> <br /> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">boolean</span> makeDirectory(String dir) { <br /> connectServer(); <br /> <span style="color:#0000ff;">boolean</span> flag = <span style="color:#0000ff;">true</span>; <br /> <span style="color:#0000ff;">try</span> { <br /> <span style="color:#008000;">// System.out.println("dir=======" dir); </span> <br /> flag = ftpClient.makeDirectory(dir); <br /> <span style="color:#0000ff;">if</span> (flag) { <br /> System.out.println( <span style="color:#800000;">"make Directory "</span> + dir + <span style="color:#800000;">" succeed"</span>); <br /> <br /> } <span style="color:#0000ff;">else</span> { <br /> <br /> System.out.println( <span style="color:#800000;">"make Directory "</span> + dir + <span style="color:#800000;">" <span style="color:#0000ff;">false</span>"</span>); <br /> } <br /> } <span style="color:#0000ff;">catch</span> (Exception e) { <br /> e.printStackTrace(); <br /> } <br /> <span style="color:#0000ff;">return</span> flag; <br /> } <br /> <br /> } </div> </div> <div> </div> <div> 运行结果表名,上传文件夹成功! </div> <div> 下载文件夹还没有实现。 </div>