1.Java类:
package com.wjy.ftp.transmission; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.io.StringBufferInputStream; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; public class FtpTransmission { private String serverUrl=null; private String userName=null; private String password=null; private String storePath=null; private int port=0; public FtpTransmission(String serverUrl, String userNameString, String password, String storePath, int port) { super(); this.serverUrl = serverUrl; this.userName = userNameString; this.password = password; this.storePath = storePath; this.port = port; } public String getServerUrl() { return serverUrl; } public void setServerUrl(String serverUrl) { this.serverUrl = serverUrl; } public String getUserNameString() { return userName; } public void setUserNameString(String userNameString) { this.userName = userNameString; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getStorePath() { return storePath; } public void setStorePath(String storePath) { this.storePath = storePath; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } /** * Description:实现ftp的文件上传 * @author 王吉元 * @Version 1.0 Dec 16,2013 * @param fileName:上传的文件名称 * @param fileContent:文件的内容 * @return 成功返回ture,失败返回false */ public boolean fileUpLoad(String fileName,String contents){ boolean isSuccessed=false; FTPClient ftpClient=new FTPClient(); StringBufferInputStream input=new StringBufferInputStream(contents); try { int reply=0; ftpClient.connect(serverUrl,port); ftpClient.login(userName, password); reply=ftpClient.getReplyCode(); if(!FTPReply.isPositiveCompletion(reply)){ ftpClient.disconnect(); return isSuccessed; } ftpClient.changeWorkingDirectory(storePath); ftpClient.storeFile(fileName, input); input.close(); ftpClient.logout(); isSuccessed=true; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ if(ftpClient.isConnected()){ try { ftpClient.disconnect(); } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); } } } return isSuccessed; } /** * Description:实现ftp的文件下载 * @author 王吉元 * @Version 1.0 Dec 16,2013 * @param fileName:下载的文件名称 * @param localPath:下载后保存在本地的路径 * @return 成功返回ture,失败返回false */ public boolean fileDownload(String fileName,String localPath){ boolean isSuccessed=false; FTPClient ftpClient=new FTPClient(); try { int reply=0; ftpClient.connect(serverUrl,port); ftpClient.login(userName, password); reply=ftpClient.getReplyCode(); if(!FTPReply.isPositiveCompletion(reply)){ ftpClient.disconnect(); return isSuccessed; } ftpClient.changeWorkingDirectory(storePath); FTPFile[] files=ftpClient.listFiles(); for(FTPFile file : files){ if(file.getName().equals(fileName)){ File localFile=new File(localPath+"/"+file.getName()); OutputStream outputStream=new FileOutputStream(localFile); ftpClient.retrieveFile(file.getName(), outputStream); outputStream.close(); } } ftpClient.logout(); isSuccessed=true; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ if(ftpClient.isConnected()){ try { ftpClient.disconnect(); } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); } } } return isSuccessed; } }
2.Java的jar测试:(被注释掉的部分是上传测试代码)
package com.wjy.main; import java.io.File; import java.io.FileInputStream; import com.wjy.ftp.transmission.FtpTransmission; public class TestMain { // public static void main(String args[]){ // FtpTransmission ftpTransmission=new FtpTransmission("10.13.30.22", "wjy", "wjywjy", "./tools/", 21); // try { // StringBuilder stringBuilder=new StringBuilder(); // FileInputStream fileInputStream=new FileInputStream(new File("E://testmodel.cld")); // int cc; // while((cc=fileInputStream.read())!=-1){ // stringBuilder.append((char)cc); // } // System.out.println(stringBuilder); // boolean flag=ftpTransmission.fileUpLoad("testmodel.cld", stringBuilder.toString()); // System.out.println(flag); // } catch (Exception e) { // // TODO: handle exception // e.printStackTrace(); // } // } public static void main(String args[]){ FtpTransmission ftpTransmission=new FtpTransmission("10.13.30.22", "wjy", "wjywjy", "./tools/", 21); try { boolean flag=ftpTransmission.fileDownload("ctest.txt","F://NB"); System.out.println(flag); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }
3.C#的dll(由ikvmc转成)测试:(被注释掉的部分是上传测试代码)
using System; using System.IO; using System.Collections.Generic; using System.Text; using com.wjy.ftp.transmission; namespace FTPTransJarTest { class Program { //static void Main(string[] args) //{ //FtpTransmission ftpTransmission=new FtpTransmission("10.13.30.22", "wjy", "wjywjy", "./tools/", 21); //try { // StringBuilder stringBuilder=new StringBuilder(); // StreamReader file = new StreamReader("E://testmodel.cld"); // int cc; // while((cc=file.Read())!=-1){ // stringBuilder.Append((char)cc); // } // Boolean flag = ftpTransmission.fileUpLoad("ctest.cld",stringBuilder.ToString()); // Console.Write(flag); //} catch (Exception e) { // // TODO: handle exception // Console.Write(e.Message); //} //} static void Main(string[] args) { FtpTransmission ftpTransmission = new FtpTransmission("10.13.30.22", "wjy", "wjywjy", "./tools/", 21); try { Boolean flag = ftpTransmission.fileDownload("ctest.txt", "F://NB"); Console.Write(flag); } catch (Exception e) { // TODO: handle exception Console.Write(e.Message); } } } }