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. Spring MVC Cookie example

    In this post we will see how to access and modify http cookies of a webpage in Spring MVC framework. ...

  2. SRM 582 Div II Level Two SpaceWarDiv2

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12556 #include <iostream> # ...

  3. servlet后台怎样接收对象參数

    主要思想是用js把对象转换成json.然后把json提交到后台去,后台把这个json字符串转换成map对象 <script type="text/javascript"> ...

  4. mysql出现Waiting for table metadata lock的原因及解决方案

    最近经常遇到mysql数据库死锁,郁闷死, show processlist; 时 Waiting for table metadata lock 能一直锁很久 下面有官网的一段话,可以理解下 htt ...

  5. jquery 动态添加和删除 ul li列表

    今天需要实现一个jquery动态添加和删除  ul li列表中的li行,自己简单的实现乐一个,分享一下 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...

  6. javascript 自己主动绑定JS callback 的方法函数

    自己写的一个javascript 智能绑定callback 而且调用运行的函数.主要用于异步请求的 ajax中: <!DOCTYPE html> <html> <head ...

  7. Raspberry pi raspbain系统下使用vim

    一开始 apt-get install vim不好用. 在putty中执行这条命令就可以了. sudo apt-get update && sudo apt-get install v ...

  8. asp于Server.MapPath用法

    总是忘记Server.MapPath的用法,以下记录了,以后使用: 总注:Server.MapPath获得的路径都是server上的物理路径,也就是常说的绝对路径 1.Server.MapPath(& ...

  9. ZeroMQ:云计算时代最好的通讯库

    还在学socket编程吗?还在研究为什么epoll比select更好吗? 噢,不必了! 在复杂的云计算环境中,我们面临的难题远比这个复杂得多. 庞大的服务器集群作为计算云,对来来看或许只是一个简单的搜 ...

  10. Jetty:配置安全

    用${jetty.home}和${jetty.base}配置安全 Jetty 9.1中:  1)${jetty.home}是jetty公布(二进制)的文件夹路径:  2)${jetty.base}是用 ...