用java实现ftp的功能

由于要在文件生成之后马上传送到ftp服务器上,只得在java程序中实现了。

本来提了一个使用ftp定时传送文件的方案,客户不接受,没辙,硬给客户逼着写了一个。

配置文件:

configure.properties:

#ftp定义ftp.host=ftp_server
ftp.user=hxd
ftp.password=hxd
filename.fg=ftp.path=c:test

FtpUtil.java:

package com.test.gfportal.util.ftp;

import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
import sun.net.ftp.FtpLoginException;

import java.io.*;
import java.util.Date;

/**
* Description : Ftp工具类
* Author : husthxd
* 来自www.itpub.net
* Date: 2005-6-30
* Time: 12:56:29
* Version 1.0
*/
public class FtpUtil {
FtpClient aftp;
DataOutputStream outputs;
DataInputStream inputs;
TelnetInputStream ins;
TelnetOutputStream outs;
int ch;
public String strInfo = "没有连接主机";
String hostname = "";

/**
*
*/
public void stop() {
try {
aftp.closeServer();
} catch (IOException e) {
}
}

/**
* @param hostname
* @param uid
* @param pwd
* @return
*/
public boolean connectToHost(String hostname, String uid, String pwd) {
this.hostname = hostname;
try {
aftp = new FtpClient(hostname);
aftp.login(uid, pwd);
aftp.binary();
} catch (FtpLoginException e) {
strInfo = "无权限与主机:" + hostname + "连接!";
return false;
} catch (IOException e) {
strInfo = "连接主机:" + hostname + "失败!";
return false;
} catch (SecurityException e) {
strInfo = "无权限与主机:" + hostname + "连接!";
return false;
}
return true;
}

/**
* @throws Exception
*/
public void init() throws Exception {
//连接到主机
String host = FtpHelper.getProp("ftp.host");
String user = FtpHelper.getProp("ftp.user");
String password = FtpHelper.getProp("ftp.password");
connectToHost(host, user, password);
}
/**
* @param filepathname
* @return
*/
public boolean sendFile(String filepathname) throws Exception {
//系统初始化
init();
//结果
boolean result = true;
//ftp对象
if (aftp != null) {
try {
//信息
strInfo = "传送成功!";
//分隔符
String fg = FtpHelper.getProp("filename.fg");
System.out.println("分隔符:" + fg);
//获取文件名
int index = filepathname.lastIndexOf(fg);
String filename = filepathname.substring(index + 1);
//传送文件
File localFile = new File(filepathname);
RandomAccessFile sendFile = new RandomAccessFile(filepathname, "r");
sendFile.seek(0);
//输出流
outs = aftp.put(filename);
outputs = new DataOutputStream(outs);
//写文件
while (sendFile.getFilePointer() < sendFile.length()) {
ch = sendFile.read();
outputs.write(ch);
}
sendFile.close();
} catch (IOException e) {
strInfo = "传送失败!";
result = false;
} finally {
//关闭资源
outs.close();
}
} else {
result = false;
}
return result;
}
}

FtpHelper.java:

package com.test.gfportal.util.ftp;

import java.text.FieldPosition;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.ResourceBundle;

/**
* Description : ftp助手类
* Author : husthxd
* 来自www.itpub.net
* Date: 2005-6-30
* Time: 14:51:34
* Version 1.0
*/
public class FtpHelper {

static ResourceBundle bundle = null;

/**
* @param type
* @return 取其中的属性值(String类型)
*/
public static String getProp(String type) throws Exception {
if (bundle == null) {
try {
//获取bundle
bundle =
ResourceBundle.getBundle("com.foresee.gfportal.util.ftp.configure",
Locale.getDefault());
} catch (Exception e) {
System.out.println("获取资源出错!" + e);
throw new Exception("获取资源出错!" + e);
}
}
//返回
return bundle.getString(type);
}
}


2-3个小时完成的东西,大家就不要要求太高了:)

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/6906/viewspace-21843/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/6906/viewspace-21843/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值