用java實現ftp的功能

husthxd發表於2005-07-01
由於要在檔案生成之後馬上傳送到ftp伺服器上,只得在java程式中實現了。

本來提了一個使用ftp定時傳送檔案的方案,客戶不接受,沒轍,硬給客戶逼著寫了一個。

配置檔案:

configure.properties:

#ftp定義


filename.fg=

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("
");
String user = FtpHelper.getProp("
");
String password = FtpHelper.getProp("
");
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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章