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=; 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=;
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=;
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/", );
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/", );
try
{
Boolean flag = ftpTransmission.fileDownload("ctest.txt", "F://NB");
Console.Write(flag);
}
catch (Exception e)
{
// TODO: handle exception
Console.Write(e.Message);
} }
}
}

用Java写个ftp传输类实现文件的上传和下载,用ikvmc转成dll的更多相关文章

  1. java实现ftp文件的上传与下载

    最近在做ftp文件的上传与下载,基于此,整理了一下资料.本来想采用java自带的方法,可是看了一下jdk1.6与1.7的实现方法有点区别,于是采用了Apache下的框架实现的... 1.首先引用3个包 ...

  2. 初学Java Web(7)——文件的上传和下载

    文件上传 文件上传前的准备 在表单中必须有一个上传的控件 <input type="file" name="testImg"/> 因为 GET 方式 ...

  3. java web(四):request、response一些用法和文件的上传和下载

    上一篇讲了ServletContent.ServletCOnfig.HTTPSession.request.response几个对象的生命周期.作用范围和一些用法.今天通过一个小项目运用这些知识.简单 ...

  4. java实现文件的上传和下载

    1. servlet 如何实现文件的上传和下载? 1.1上传文件 参考自:http://blog.csdn.net/hzc543806053/article/details/7524491 通过前台选 ...

  5. Java中文件的上传与下载

    文件的上传与下载主要用到两种方法: 1.方法一:commons-fileupload.jar  commons-io.jar apache的commons-fileupload实现文件上传,下载 [u ...

  6. java 文件的上传和下载

    主要介绍使用 smartupload.jar 包中的方法对文件的上传和下载.上传时文件是存放在服务器中,我用的是tamcat. 首先建立一个servlet 类,对文件的操作 package com.d ...

  7. win7下利用ftp实现华为路由器的配置文件上传和下载

    win7下利用ftp实现华为路由器的配置文件上传和下载 1.  Win7下ftp的安装和配置 (1)开始—>控制面板—>程序—>程序和功能—>打开或关闭Windows功能 (2 ...

  8. java客户端文件的上传和下载

    java客户端文件的上传和下载 //上传 public JTable upload(String id){ JTable table=new JTable(); System.out.println( ...

  9. C#实现FTP文件的上传、下载功能、新建目录以及文件的删除

    本来这篇博文应该在上周就完成的,可无奈,最近工作比较忙,没有时间写,所以推迟到了今天.可悲的是,今天也没有太多的时间,所以决定给大家贴出源码,不做详细的分析说明,如果有不懂的,可以给我留言,我们共同讨 ...

随机推荐

  1. 博弈问题之SG函数博弈小结

    SG函数: 给定一个有向无环图和一个起始顶点上的一枚棋子,两名选手交替的将这枚棋子沿有向边进行移动,无法移 动者判负.事实上,这个游戏可以认为是所有Impartial Combinatorial Ga ...

  2. 基于visual Studio2013解决面试题之0604O(1)时间复杂度删除链表节点

     题目

  3. 刚写好的读取多网卡IP地址的函数

    虽然现在一机多网卡已经很普遍(像Notebook带有线.无线.蓝芽等),但是找一个现成的能够一次过读出所有网卡IP地址的函数实在是难,无奈自己写了一个,好东西谁用谁知道. //uses WinSock ...

  4. 14.2.1 MySQL and the ACID Model

    14.2 InnoDB Concepts and Architecture InnoDB 概念和结构体系: 14.2.1 MySQL and the ACID Model 14.2.2 InnoDB ...

  5. C++建立动态二维数组

    C++建立动态二维数组主要有两种方法: 1.使用数组指针,分配一个指针数组,将其首地址保存在b中,然后再为指针数组的每个元素分配一个数组                           int * ...

  6. Swift - 类型判断is 与 类型转换as

    在Swift中,通常使用is和as操作符来实现类型检查和转换.下面通过样例来演示使用方法,首先定义几个类. 1 2 3 4 5 6 7 8 9 10 11 //基类,人类 class Human{ } ...

  7. 配置BeanUtils包,同时也是对导入第三包的步骤说明

    BeanUtils是由Apache公司开发的针对操作JavaBean的工具包. 对于JavaBean,简单的来说,就是要有一个空参的构造器和对属性的getXXX方法和setXXX方法. 在由JDK提供 ...

  8. 旧版QT的名称:qt-win-commercial-4.4.3-vc60.exe

    qt-win-commercial-4.4.3-vc60.exeqt-vsaddin-collection-2.1.4.exeqt-win-commercial-4.4.3-v2005.exeqt-v ...

  9. Dom4j SAXReader Constructors

    Dom4j读取xml:eg1: package xml; import java.io.File; import org.dom4j.DocumentException; import org.dom ...

  10. Android 进行单元測试难在哪-part3

    原文链接 : HOW TO MAKE OUR ANDROID APPS UNIT TESTABLE (PT. 1) 原文作者 : Matthew Dupree 译文出自 : 开发技术前线 www.de ...