近日在做FTP的上傳下載功能,此功能幾經周折,終于實現單線程和多線程文件的上傳和下載,我現在把代碼上傳,算是一個OK的demo
package com.cotel.service.gather;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Properties;
import org.apache.log4j.*;
import com.cotel.parse.ParseXML;
import com.cotel.util.FileFinder;

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



/** *//**
*
* 功能描述:FTP上傳和下載
*
* @author <a href="mailto:zhanghhui@126.com">KenZhang</a>
* @version 1.0
* Creation date: 2007-8-22 - 下午04:57:32
*/

public class GatherServiceImpl implements IGatherService
{
//引入日志
private static Logger log = (Logger)Logger.getLogger(GatherServiceImpl.class);

/** *//**
* 類的初始化,建立ftp的連接,用戶登錄,指定ftp的傳輸流
* @param host
* @param port
* @param user
* @param psw
*/
// 建立一條與指定主機、指定端口上的FTP服務器的連接
private FtpClient aftp = new FtpClient();
private DataOutputStream outputs;
private TelnetOutputStream outs;
private TelnetInputStream inps;
//此路徑只應用于在java Application里面進行調試
private static String path = System.getProperty("user.dir") + "\\WebRoot\\WEB-INF\\classes\\charging\\";

//在tomcat里面運行時用下面路徑----
// private static String path = GatherServiceImpl.class.getClass().getResource("/").getPath()+ "charging";

public void FTPClass(String host,String port,String user,String psw,String url)
{

try
{
// 注冊到FTP服務器
aftp.openServer(host);
log.debug("登陸
.");
aftp.login(user,psw);
log.debug("登錄FTP服務器成功!");
aftp.binary();

}catch(IOException e)
{
log.debug("連接FTP服務器失敗!");
e.printStackTrace();
}
}

/** *//**
* 通過ftp上傳文件到服務器上
* @param localFile 本地所要上傳的文件
* @param remoteFile 傳到服務器上的文件名稱
*/

public boolean upFile(String localFile,String remoteFile)
{
boolean result = true;

if(aftp != null)
{
log.debug("正在上傳文件"+localFile+",請稍等
.");

try
{
File file = new File(localFile);
outs = aftp.put(remoteFile);
FileInputStream in = new FileInputStream(file);
byte[] bytes = new byte[1024];
int c;

while((c = in.read(bytes))!=-1)
{
outs.write(bytes,0,c);
}
outs.close();
in.close();
log.debug("上傳文件"+localFile+"成功!");
log.debug("上傳文件所在目錄:"+remoteFile+"");

}catch(Exception e)
{
e.printStackTrace();
log.debug("上傳文件"+localFile+"失敗!");
result = false;
}

}else
{
result = false;
}
return result;
}

/** *//**
* 下載FTP服務器上的文件
* @param localFile 本地文件名
* @param remoteFile 遠程服務器文件名
*/

public boolean downFile(String remoteFile,String localFile)
{
boolean result = true;
log.debug("begin");

if(aftp!=null)
{
log.debug("正在下載文件"+remoteFile+",請等待
");

try
log.debug("===" + remoteFile);
inps = aftp.get(remoteFile);
FileInputStream in = null;
FileOutputStream os;

if(inps != null)
{
os = new FileOutputStream(localFile);
byte[] bytes = new byte [1024];
int c ;

while ((c = inps.read(bytes))!=-1)
{
os.write(bytes,0,c);
log.debug(c);
}
inps.close();
os.close();
}else
log.debug("file not exist!");
inps.close();
log.debug("下載文件"+localFile+"成功!");
log.debug("下載文件所在目錄:"+localFile+"");

}catch(Exception e)
{
e.printStackTrace();
log.debug("下載文件"+localFile+"失敗!");
result = false;
}
}
return false;
}

/** *//**
* 斷開ftp連接
* @throws IOException
*
*/

public void disconnect() throws IOException
{
aftp.closeServer();
log.debug("FTP服務器連接斷開!");
}
// 返回當前目錄的所有文件及文件夾

public ArrayList getFileList() throws IOException
{
BufferedReader dr = new BufferedReader(new InputStreamReader(aftp.nameList("*.txt")));
ArrayList al = new ArrayList();
String s = "";

while ((s=dr.readLine())!=null)
{
al.add(s);
}
return al;
}


/** *//** main方法測試
* @param args
* @throws IOException
* @author <a href="mailto:zhanghhui@126.com">KenZhang</a>
* Creation date: 2007-8-23 - 上午10:11:03
*/

public static void main(String[] args) throws IOException
{
GatherServiceImpl gatherService = new GatherServiceImpl();
log.debug("begin");
//從配置文件中獲得監聽端口和發送端口
Properties p = new Properties();
//讀取配置文件
InputStream in = GatherServiceImpl.class.getResourceAsStream(
"/config.properties");
p.load(in);
in.close();
String ftpHostIP = p.getProperty("ftpHostIP");
String ftpPort = p.getProperty("ftpPort");
String ftpUserName = p.getProperty("ftpUserName");
String ftpPassWord = p.getProperty("ftpPassWord");
String ftpPath = p.getProperty("ftpPath");
gatherService.FTPClass(ftpHostIP, ftpPort, ftpUserName, ftpPassWord, ftpPath);
//文件批量下載,把擴展名為.txt的所有文件一次下載到服務器
ArrayList list = gatherService.getFileList();

for(int i = 0; i < list.size(); i++)
{
String fileName = null;
fileName = (String)list.get(i);
//System.out.println("-------"+fileName);
gatherService.downFile(fileName,path+fileName);
}
//文件進行批量上傳,把擴展名為.txt的所有文件一次上傳到服務器
FileFinder fileFinder = new FileFinder();
File[] files = fileFinder.getFilesBySuffix(".txt", path);

for (int i = 0; i < files.length; i++)
{
System.out.println("-------"+files[i].getName());
gatherService.upFile(path+files[i].getName(),files[i].getName());
}
//從ftp服務器下載文件
// gatherService.downFile("地標信息.txt",path+"地標信息.txt");
//上傳文件到ftp服務器
//ftpClient.upFile(path+"地標信息.txt", "ftproot/地標信息.txt");
//與FTP服務器連接斷開
gatherService.disconnect();

}
代碼改動很多,才實現了我想要的需求。用java Application運行的時候是不是有緩存呢?有的話,這個緩存怎么去呢?我都是再把程序重新編譯一次,再執行。好像起點作用!
posted on 2007-08-31 16:01
kenzhang 閱讀(647)
評論(1) 編輯 收藏