Package com. northking. dataManager. util;
Import sun.net. ftp .*;
Import sun.net .*;
Import java. io. *; jsp file upload/download program
/**
* Use the sun.net. ftp toolkit for ftp upload and download
* @ Author maochangming
* @ Date 2008-6-20 13:09:29
* @ Description:
*/
Public class FtpTool {
String ip;
Int port;
String user;
String pwd;
String remotePath;
String localPath;
FtpClient ftpClient;
/**
* Connect to the ftp server
* @ Param ip
* @ Param port
* @ Param user
* @ Param pwd
* @ Return
* @ Throws Exception
*/
Public boolean connectServer (String ip, int port, String user, String pwd)
Throws Exception {
Boolean isSuccess = false;
Try {
FtpClient = new FtpClient ();
FtpClient. openServer (ip, port );
FtpClient. login (user, pwd );
IsSuccess = true;
} Catch (Exception ex ){
Throw new Exception ("Connect ftp server error:" + ex. getMessage ());
}
Return isSuccess;
}
/**
* Download an object
* @ Param remotePath
* @ Param localPath
* @ Param filename
* @ Throws Exception
*/
Public void downloadFile (String remotePath, String localPath, String filename) throws Exception {
Try {
If (connectServer (getIp (), getPort (), getUser (), getPwd ())){
If (remotePath. length ()! = 0)
FtpClient. cd (remotePath );
FtpClient. binary ();
TelnetInputStream is = ftpClient. get (filename );
File file_out = new File (localPath + File. separator + filename );
FileOutputStream OS = new FileOutputStream (file_out );
Byte [] bytes = new byte [1, 1024];
Int c;
While (c = is. read (bytes ))! =-1 ){
OS. write (bytes, 0, c );
}
Is. close ();
OS. close ();
FtpClient. closeServer ();
}
} Catch (Exception ex ){
Throw new Exception ("ftp download file error:" + ex. getMessage ());
}
}
/**
* Upload a file
* @ Param remotePath
* @ Param localPath
* @ Param filename
* @ Throws Exception
*/
Public void uploadFile (String remotePath, String localPath, String filename) throws Exception {
Try {
If (connectServer (getIp (), getPort (), getUser (), getPwd ())){
If (remotePath. length ()! = 0)
FtpClient. cd (remotePath );
FtpClient. binary ();
TelnetOutputStream OS = ftpClient. put (filename );
File file_in = new File (localPath + File. separator + filename );
FileInputStream is = new FileInputStream (file_in );
Byte [] bytes = new byte [1, 1024];
Int c;
While (c = is. read (bytes ))! =-1 ){
OS. write (bytes, 0, c );
}
Is. close ();
OS. close ();
FtpClient. closeServer ();
}
} Catch (Exception ex ){
Throw new Exception ("ftp upload file error:" + ex. getMessage ());
}
}
/**
* @ Return
*/
Public String getIp (){
Return ip;
}
/**
* @ Return
*/
Public int getPort (){
Return port;
}
/**
* @ Return
*/
Public String getPwd (){
Return pwd;
}
/**
* @ Return
*/
Public String getUser (){
Return user;
}
/**
* @ Param string
*/
Public void setIp (String string ){
Ip = string;
}
/**
* @ Param I
*/
Public void setPort (int I ){
Port = I;
}
/**
* @ Param string
*/
Public void setPwd (String string ){
Pwd = string;
}
/**
* @ Param string
*/
Public void setUser (String string ){
User = string;
}
/**
* @ Return
*/
Public FtpClient getFtpClient (){
Return ftpClient;
}
/**
* @ Param client
*/
Public void setFtpClient (FtpClient client ){
FtpClient = client;
}
/**
* @ Return
*/
Public String getRemotePath (){
Return remotePath;
}
/**
* @ Param string
*/
Public void setRemotePath (String string ){
RemotePath = string;
}
/**
* @ Return
*/
Public String getLocalPath (){
Return localPath;
}
/**
* @ Param string
*/
Public void setLocalPath (String string ){
LocalPath = string;
}
}
Download test
Package com. northking. dataManager. dataimport. parse. test;
Import com. northking. dataManager. util. FtpTool;
Import junit. framework. TestCase;
/**
* @ Author maochangming
* @ Date 2008-6-20 13:09:11
* @ Description:
*/
Public class FtpToolTest extends TestCase {
FtpTool ftpTool;
/**
* Constructor for FtpToolTest.
* @ Param arg0
*/
Public FtpToolTest (String arg0 ){
Super (arg0 );
}
Public static void main (String [] args ){
Junit. textui. TestRunner. run (FtpToolTest. class );
}
Public void testDownLoadFile () throws Exception {
FtpTool. downloadFile (ftpTool. getRemotePath (), "c:/downloads", "JBFImgMng. CAB ");
}
/*
* @ See TestCase # setUp ()
*/
Protected void setUp () throws Exception {
FtpTool = new FtpTool ();
FtpTool. setIp ("10.164.12.70 ");
FtpTool. setPort (2100 );
FtpTool. setUser ("share ");
FtpTool. setPwd ("share ");
FtpTool. setRemotePath ("/paeams ");
Super. setUp ();
}
/*
* @ See TestCase # tearDown ()
*/
Protected void tearDown () throws Exception {
Super. tearDown ();
}
}