org.apache.commons.net工具下 对ftp的简单操作
/** * 连接ftp服务器 * @param ip IP地址 * @param port 端口号 * @param username 用户名 * @param password 密码 * @param ftpPath ftp子目录 * @throws IOException */ public int connectServer(String ip, int port, String username, String password, String ftpPath) throws IOException { system.out.println(this, "=============all_attributes==========="+"ip_"+ip+"port_"+port+"username_"+username+"password_"+password+"ftpPath_"+ftpPath); int reply; int connectFlag = 1; try { ftpClient.connect(ip, port);//连接FTP服务器 //如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器 ftpClient.login(username, password); reply = ftpClient.getReplyCode(); if(!FTPReply.isPositiveCompletion(reply)){ ftpClient.disconnect(); connectFlag = 0; return connectFlag; } ftpClient.changeWorkingDirectory(ftpPath);//转移到FTP服务器目录 system.out.println(this, "=============connet_ftp_is_ok_falg_"+connectFlag+"==========="); } catch (Exception e) { system.out.println(this, "=============connet_ftp_is_error"+connectFlag+"==========="); } return connectFlag; } ---------------------------------------------------------------------------------------------------------------------------------------- /** * upload 上传文件 * * @throws java.lang.Exception * @return -1 文件不存在 -2 文件内容为空 >0 成功上传,返回文件的大小 * @param newname 上传后的新文件名 * @param filePath 上传的文件的路径 */ public long upload(String localPath, String newFileName) throws Exception { long result = 0; try { File file = new File(localPath); if (!file.exists()) { result = -1;//文件不存在 } if (file.length() == 0) { result = -2;//文件内容为空 } // result = file.length(); is = new FileInputStream(file); ftpClient.storeFile(newFileName, is); ftpClient.logout(); system.out.println(this, "========="+newFileName+"文件上传成功=========="); }catch (Exception e) { e.printStackTrace(); system.out.println(this, "========="+newFileName+"文件上传失败=========="); }finally{ } return result; } --------------------------------------------------------------------------------- /** * 通过文件路径上传 * @param filePath * @return * @throws Exception */ public long upload(String filePath) throws Exception { String newname = ""; if (filePath.indexOf("/") > -1) { newname = filePath.substring(filePath.lastIndexOf("/") + 1); } else { newname = filePath; } return upload(filePath, newname); } ---------------------------------------------- /** * * closeServer * * 断开与ftp服务器的链接 * * @throws java.io.IOException */ public void closeServer() throws IOException { try { if (is != null) { is.close(); } if (ftpClient != null) { ftpClient.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } ------------------------------------------------- /** * 读取文本文件. * */ public static String readTxtFile(File filePathAndName){ String read; FileReader fileread; try { fileread = new FileReader(filePathAndName); bufread = new BufferedReader(fileread); try { while ((read = bufread.readLine()) != null) { readStr = readStr + read+ "\r\n"; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("文件内容是:"+ "\r\n" + readStr); return readStr; } ------------------------------------------------------ /** * 写文件. * */ public static void writeTxtFile(String newStr,File filePathAndName) throws IOException{ //先读取原有文件内容,然后进行写入操作 String filein = newStr + "\r\n" + readStr + "\r\n"; RandomAccessFile mm = null; try { mm = new RandomAccessFile(filePathAndName, "rw"); mm.writeBytes(filein); } catch (IOException e1) { // TODO 自动生成 catch 块 e1.printStackTrace(); } finally { if (mm != null) { try { mm.close(); } catch (IOException e2) { // TODO 自动生成 catch 块 e2.printStackTrace(); } } } } ---------------------------------------------------- /** * * downloadFile * * 下载ftp服务器的资源 * @param remoteFileName 待下载文件名称 * @param localDires 本里存放路径 * @param ftpPath ftp服务器存放要下载文件的子目录 * * @throws java.io.IOException */ public void downloadFile(String remoteFileName,String localDires , String ftpPath ) throws IOException{ String strfilepath = localDires +"/"+ remoteFileName; //写入地址 文件保存路径 BufferedOutputStream bf = null; boolean success=false; try { ftpClient.changeWorkingDirectory(ftpPath); bf= new BufferedOutputStream(new FileOutputStream(strfilepath)); system.out.println(this, "========="+remoteFileName+"开始下载=========="); success = ftpClient.retrieveFile(remoteFileName,bf); //下载操作 // ftpClient.logout(); if(success==true) { system.out.println(this, "========="+remoteFileName+"成功下载到=========="+localDires); } } catch (IOException e1) { // TODO 自动生成 catch 块 e1.printStackTrace(); } finally { if (null != bf) { try { bf.flush(); bf.close(); } catch (IOException e2) { // TODO 自动生成 catch 块 e2.printStackTrace(); } } } if(success=false) { system.out.println(this, "========="+remoteFileName+"下载失败=========="); } } -----------------------------------------
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。