用Java写个ftp传输类实现文件的上传和下载,用ikvmc转成dll
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的更多相关文章
- java实现ftp文件的上传与下载
最近在做ftp文件的上传与下载,基于此,整理了一下资料.本来想采用java自带的方法,可是看了一下jdk1.6与1.7的实现方法有点区别,于是采用了Apache下的框架实现的... 1.首先引用3个包 ...
- 初学Java Web(7)——文件的上传和下载
文件上传 文件上传前的准备 在表单中必须有一个上传的控件 <input type="file" name="testImg"/> 因为 GET 方式 ...
- java web(四):request、response一些用法和文件的上传和下载
上一篇讲了ServletContent.ServletCOnfig.HTTPSession.request.response几个对象的生命周期.作用范围和一些用法.今天通过一个小项目运用这些知识.简单 ...
- java实现文件的上传和下载
1. servlet 如何实现文件的上传和下载? 1.1上传文件 参考自:http://blog.csdn.net/hzc543806053/article/details/7524491 通过前台选 ...
- Java中文件的上传与下载
文件的上传与下载主要用到两种方法: 1.方法一:commons-fileupload.jar commons-io.jar apache的commons-fileupload实现文件上传,下载 [u ...
- java 文件的上传和下载
主要介绍使用 smartupload.jar 包中的方法对文件的上传和下载.上传时文件是存放在服务器中,我用的是tamcat. 首先建立一个servlet 类,对文件的操作 package com.d ...
- win7下利用ftp实现华为路由器的配置文件上传和下载
win7下利用ftp实现华为路由器的配置文件上传和下载 1. Win7下ftp的安装和配置 (1)开始—>控制面板—>程序—>程序和功能—>打开或关闭Windows功能 (2 ...
- java客户端文件的上传和下载
java客户端文件的上传和下载 //上传 public JTable upload(String id){ JTable table=new JTable(); System.out.println( ...
- C#实现FTP文件的上传、下载功能、新建目录以及文件的删除
本来这篇博文应该在上周就完成的,可无奈,最近工作比较忙,没有时间写,所以推迟到了今天.可悲的是,今天也没有太多的时间,所以决定给大家贴出源码,不做详细的分析说明,如果有不懂的,可以给我留言,我们共同讨 ...
随机推荐
- hdu1087Super Jumping! Jumping! Jumping!(最大递增序列和)
题意:棋牌游戏如今,一种被称为“超级跳!跳!跳!“HDU是非常流行的.也许你是个好孩子,这个游戏知之甚少,所以我介绍给你吧. 可以玩游戏由两个或两个以上的球员 .它由一个棋盘(棋盘)和一些棋子(棋子) ...
- Cordys BOP 4平台开发入门实战演练——Webservices开发(0基础)
0.文章导读 本文档针对Cordys BOP-4 WS-AppServer基础功能进行验证和高速开发指导.(高级实践文档请參考兴许文档). 0.1.WS-AppServer概述 WS-AppServe ...
- 【实战】静默安装-oracle 11.2.0.3 on centos 5.10
发现网上静默安装的文章非常多,乱七八糟,五花八门!来个扫盲的! centos 5.10 下安装oracle 11g_r2 ************************************* ...
- HDU 4931 Happy Three Friends(水)
HDU 4931 Happy Three Friends 题目链接 题意:6个数字,一个取两个,妹子取三个,问最后谁会赢 思路:排个序,推断前两个和3 - 5个的和谁更大就可以 代码: #includ ...
- SetWindowLong
SetWindowLong函数介绍 收藏 SetWindowLong函数介绍 SetWindowLong Unicode 函数原型 LONG SetWindowLong(hwnd,nIndex,lNe ...
- 如何在使用摩托罗拉上的RSS阅读器应用进行一次订阅
订阅一个CSDN的RSS为例. 1.打开RSS阅读器. 2.设置->新增订阅->手动新增 订阅URL:输入http://articles.csdn.net/api/rss.php?tid= ...
- 开发人员福利!ChromeSnifferPlus 插件正式登陆 Chrome Web Store
今天(2014-10-30)下午,ChromeSnifferPlus 插件正式登陆 Chrome Web Store. 在线安装地址: https://chrome.google.com/websto ...
- javascript (十四) dom
通过 HTML DOM,可访问 JavaScript HTML 文档的所有元素. HTML DOM (文档对象模型) 当网页被加载时,浏览器会创建页面的文档对象模型(Document Object M ...
- MySQL 执行计划里的rows
<pre name="code" class="html">SQL> alter session set statistics_level=a ...
- [51daifan]来吧,一起书写51daifan的成长史吧-让一部分人先安全起来
对新创项目而言,是idea更重要,还是执行力更重要?在没有用户时,我们该如何冷启动?团队.人.技术.产品.推广和拜春哥,哪一个更重要?到底是什么决定了一个项目的生存或者毁灭? 来吧,一起书写51dai ...