用Java写个ftp传输类实现文件的上传和下载,用ikvmc转成dll
来源:互联网 发布:酒店网络所需设备清单 编辑:程序博客网 时间:2024/06/05 20:04
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); } } }}
0 0
- 用Java写个ftp传输类实现文件的上传和下载,用ikvmc转成dll
- Java 实现ftp文件的上传和下载
- ftp上传和下载文件的java实现
- ftp上传和下载文件的java实现
- ftp上传和下载文件的java实现
- java实现ftp的文件上传和下载
- FTP下载和上传文件的java实现
- Java连接FTP并且实现文件的上传和下载
- ftp实现文件的上传和下载
- java实现ftp的上传和下载
- linux下用java实现ftp上传、下载文件
- 用edtftpj实现ftp文件上传和下载
- Java ftp实现文件的上传和下载ftp,sftp sun.net.ftp.FtpProtocolException:Welcome message: SSH-2.0-OpenSSH_5.1
- JAVA Ftp实现文件上传于下载
- Java实现FTP文件上传与下载
- Java实现FTP文件上传与下载
- Java实现FTP文件上传与下载
- Java实现FTP文件上传与下载
- Django写的投票系统4(转)
- Java提供的enum详解
- Java字节流和字符流
- 用C语言如何编程实现从三个数组中各抽取几个数进行组合的问题?
- (转)ikvmc的使用
- 用Java写个ftp传输类实现文件的上传和下载,用ikvmc转成dll
- Java反射机制小例子
- Java使用反射机制优化工厂方法
- 关于Java中的final关键字
- SWT中的多线程(Invalid thread access)
- 线程间操作无效: 从不是创建控件的线程访问它.
- 构造器优化需要多个构造器的类(建造者模式)
- 看个泛型的例子
- Enum实现单例模式