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. Zookeeper从入门到精通(开发详解,案例实战,Web界面监控)

    ZooKeeper是Hadoop的开源子项目(Google Chubby的开源实现),它是一个针对大型分布式系统的可靠协调系统,提供的功能包括:配置维护.命名服务.分布式同步.组服务等. Zookee ...

  2. webdynpro tree控件使用

    1.  首先创建一个TREE控件 2.  在Tree下面创建一个TREE  NODE  TYPE ,node type 是可以继续展开的,而Item type是无法展开的. 3. 创建node.,下面 ...

  3. 如何使用Gmail的别名功能?

    Gmail真的是一个很好的邮箱,一直是我的最爱!它有一个很独特的功能-别名,用这个功能,我们就可以把一个 邮箱当成很多个邮箱来使用了! 1.Gmail 不提供传统别名 ,但是你可以收到发送到 your ...

  4. Ubuntu下装QQ2012,让linux小白们不怕脱离windows

    嘿嘿,很多人可能跟我一样,QQ上同学群里会通知一些事项,所以我们希望可以在linux下开QQ,但是QQ官网做的QQ For Linux, 实在是烂的不行 那么怎么在linux下装我们平时在window ...

  5. android在eclipse中打包(签名包)方法及常见问题解决

    打包成apk 右键单击项目名称,选择"Android Tools".再选择"Export Signed Application Package-",例如以下图所 ...

  6. Swift - 计算次方(2的N次方,2的随机次方)

    1,使用<<计算2的N次方 1 2 var value = 1<<4  //2的4次方 var value = 1<<Int(arc4random_uniform( ...

  7. android.graphics.Color

    该类定义的都是些static常量和函数,这些函数都是为了创建和转化成int型的color.颜色是由int型的数表示,由4个字节组成,分别是A R  G  B,这个int型的值是确定的,透明度的值只能存 ...

  8. Xamarin.forms 自定义dropdownview控件

    一 基本说明 想用xamarin做个像美团这样的下拉列表进行条件选择的功能,但是但是找了半天好像没有现成的,也没有其他类似的控件可以走走捷径,再则也没有找到popwindow之类的东东,这里只好使用s ...

  9. vc 加载bmp位图并显示的方法

    方法一.显示位图文件 HBITMAP hBitmap=(HBITMAP)LoadImage(NULL,_T(“xxx.bmp”),Image_Bitmap,0,0,Lr_CreateDibSectio ...

  10. CentOS5.6下安装Oracle10G软件 【保留报错经验】

    CentOS5.6下安装Oracle10G ****************************************************************************** ...