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. 北京出dell s2740显示器 1900 - V2EX

    水木社区归档站 北京出dell s2740显示器 1900 - V2EX 北京出dell s2740显示器 1900 By kekex · 6 小时 6 分钟前 · 188 次点击 购于今年4月份 镜 ...

  2. 怎样使用 App Studio 高速定制你自己的 Universal Windows App

    今天之所以在写一篇关于 App Studio 的文章是由于,App Studio 经过了几次升级功能得到了明显提升还能够调用系统功能了.而且能够更方便的和应用商店关联公布 Universal Wind ...

  3. Win7安装vs2010失败

    提示: ------------------------------------------------------------------------------------------------ ...

  4. JQuery+AJax - 无刷新使用验证码

    最终效果: 项目目录: Default.aspx前端代码: <%@ Page Language="C#" AutoEventWireup="true" C ...

  5. [LeetCode] Print All Combinations of a Number as a Sum of Candidate Numbers

    题目连接:http://leetcode.com/2010/09/print-all-combinations-of-number-as-sum.html 题目分析: 由于这里说明了输入是升序的,当然 ...

  6. 自定义NavgationBa返回按钮

    iOS  上UINavigationController视图压栈形式,可以在当前视图无限制push许多视图,然而一些会觉得自带的push按钮不够美观,而且当上的上一个页面title很长的时候,那个返回 ...

  7. OWAP Top 10

    2013 Top 10 List   A1-Injection Injection flaws, such as SQL, OS, and LDAP injection occur when untr ...

  8. 通过OpenSSL解码X509证书文件

    在Windows平台下.假设要解析一个X509证书文件,最直接的办法是使用微软的CryptoAPI. 可是在非Windows平台下,就仅仅能使用强大的开源跨平台库OpenSSL了.一个X509证书通过 ...

  9. String[255]在高版本Delphi里还是被解释成Byte,总体长度256,使用StrPCopy可以给Array String拷贝字符串(内含许多实验测试)

    学了好多不了解的知识: procedure TForm1.Button1Click(Sender: TObject); var s1 : String; s2 : String[]; begin s1 ...

  10. Swift - 给表格添加编辑功能(删除,插入)

    1,下面的样例是给表格UITableView添加编辑功能: (1)给表格添加长按功能,长按后表格进入编辑状态 (2)在编辑状态下,第一个分组处于删除状态,第二个分组处于插入状态 (3)点击删除图标,删 ...