中国开发网: 论坛: Java/J2ME: 贴子 228235
空山新雨: FtpLocalClient.java (NOT by me)
/*
* FTPLocalClient.java
*
* Created on April 29, 2002, 10:12 AM
*/

package com.bnpparibas.utils;
import sun.net.ftp.FtpClient;
import sun.net.*;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
//import java.lang.reflect.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Calendar;
import java.util.StringTokenizer;
//import java.util.Vector;

/**
*
* @author F_Coupez
*
* entry point : location login password destinationFolder ascii/binary get/put date
* ex : file://172.29.188.32/murex/export/extract/extr232_stonykarb#yyyy#mm#dd.txt remote milor42 c:/temp/ ascii get -1
* ex : ftp://bnpparba:t0ny213@10.110.239.161/outbound/2500pgs/p2573.dat bnpparba t0ny213 c:/temp/ binary get 0
*
* location : could be an FTP site or specific location
* date : number of day to format the location and destination.
*
* This class could be use also by a programm and receive directly in a BufferedReader the data without going through a file. See method DoFtp()
*/


public class FTPLocalClient {
String m_szServer, m_szLogin, m_szPassword, m_szLocalPath, m_szFile, m_szRemotePath, m_szTran, m_szPutGet, m_szFileRemote;
int m_lDayBefore=0;
FtpClient m_ftp;
InputStream m_in;

/** Creates new FTPLocalClient */
public FTPLocalClient(String szServer, String szLogin, String szPassword, String szLocalPath, String szFile, String szRemotePath, String szRemoteFile, String szTran, String szPutGet, int lDayBeforeLocal, int lDayBeforeRemote) {
m_szServer =szServer;
m_szLogin =szLogin;
m_szPassword =szPassword;
m_szLocalPath = StringUtils.getFormatedString(szLocalPath,lDayBeforeLocal,StringUtils.SHIFT_DAY_BEFORE);
m_szFile=StringUtils.getFormatedString(szFile,lDayBeforeLocal,StringUtils.SHIFT_DAY_BEFORE);
m_szFileRemote=StringUtils.getFormatedString(szRemoteFile,lDayBeforeRemote,StringUtils.SHIFT_DAY_BEFORE);
m_szRemotePath=StringUtils.getFormatedString(szRemotePath,lDayBeforeRemote,StringUtils.SHIFT_DAY_BEFORE);
m_szTran=szTran;
m_szPutGet = szPutGet;
System.out.println(m_szServer + " " + m_szLogin + " " + m_szLocalPath + " " + m_szFile + " " + m_szRemotePath + " " + m_szFileRemote + " " + m_szTran + " " + m_szPutGet);
}
public FTPLocalClient(String url, String szLogin, String szPassword, String szLocalPath, String szLocalFile, String szTran, String szPutGet, int lDayBeforeLocal, int lDayBeforeRemote) {
URL urlConnection=null;
try {
urlConnection = new URL(StringUtils.getFormatedString(url,lDayBeforeRemote,StringUtils.SHIFT_DAY_BEFORE));
} catch (MalformedURLException e) {e.printStackTrace();System.exit(1);}
StringTokenizer st = new StringTokenizer(urlConnection.getPath() ,"/");
m_szServer =urlConnection.getHost() ;
m_szLogin =szLogin;
m_szPassword =szPassword;
m_szLocalPath = StringUtils.getFormatedString(szLocalPath,lDayBeforeLocal,StringUtils.SHIFT_DAY_BEFORE) + StringUtils.getFormatedString(szLocalFile,lDayBeforeLocal,StringUtils.SHIFT_DAY_BEFORE);
m_szRemotePath="/";
m_szFile="";
int max = st.countTokens()-1;
for (int i = 0 ; i<=max;i++){
if (i!= max)
m_szRemotePath+=st.nextToken() + "/";
else
m_szFileRemote=st.nextToken();
}
m_szTran=szTran;
m_szPutGet = szPutGet;
System.out.println(m_szServer + " " + m_szLogin + " " + m_szLocalPath + " " + " " + m_szRemotePath + " " + m_szFileRemote + " " + m_szTran + " " + m_szPutGet);
}
public BufferedReader DoFtp(boolean returnBuffer)throws IOException{
BufferedReader sb=null;
String szRemoteFilePath, szLocalFilePath;
byte [] readByte = new byte[1024];
int d = 0;

m_ftp = new FtpClient(m_szServer, 21);
//m_ftp.openServer(m_szServer);
m_ftp.login(m_szLogin, m_szPassword);
System.out.println(m_ftp.welcomeMsg);
if ( m_szTran.equals("ascii") )
m_ftp.ascii();
else
m_ftp.binary();

szRemoteFilePath=m_szRemotePath + m_szFileRemote;
szLocalFilePath=m_szLocalPath + m_szFile;
if (m_szPutGet.equals("put")){
TelnetOutputStream out;
System.out.println("it's : " + Calendar.getInstance().getTime());
System.out.println("try put to " + szRemoteFilePath + " from " + szLocalFilePath + " it's : " + Calendar.getInstance().getTime());
out = m_ftp.put(szRemoteFilePath);
m_in = new FileInputStream(szLocalFilePath);
while( ( d = m_in.read( readByte, 0, 1024 )) != -1 )
out.write( readByte, 0, d );
out.flush();
out.close();
}
else{
OutputStream out=null;
System.out.println(" try get from " + szRemoteFilePath + " to " + szLocalFilePath + " it's : " + Calendar.getInstance().getTime() );
m_in = m_ftp.get(szRemoteFilePath);
if (returnBuffer == false){
out = new FileOutputStream(szLocalFilePath);
while( ( d = m_in.read( readByte, 0, 1024 )) != -1 )
out.write( readByte, 0, d );
out.flush();
out.close();
closeConnection();
}else
//while( ( d = in.read( readByte, 0, 1024 )) != -1 )
//sb.append(byteArrayToCharArray(readByte) );
sb = new BufferedReader(new InputStreamReader(m_in));
}
return sb;
}
public void closeConnection() throws IOException{
m_in.close() ;
m_ftp.closeServer();
}
public static char[] byteArrayToCharArray( byte[] uncastArray ) {

char[] castArray = new char[ uncastArray.length ];

for ( int i = 0; i < castArray.length; ++i ) {
castArray[ i ] = (char) uncastArray[ i ];
} // end for -- every byte

return castArray;
} // end byteArrayToCharArray( )


public static void main(String[] arg){
//172.29.188.32 remote milor42 c:/temp mx_allcontracts_stonykbkt#yyyy#mm#dd.pdl /murex/export/pdl toto.pdl ascii get 0 0
//file://172.29.188.32/murex/export/extract/extr232_stonykarb#yyyy#mm#dd.txt remote milor42 c:/temp/ output.pdl ascii get -1 0
//10.119.58.237 twsadmin admeqd c:/temp/ forcehkom_FH_HKOM.txt /tibco/log/FH_HKOM/ forcehkom_FH_HKOM_#yy#mm#dd.txt ascii get 0 0
// file://10.119.58.237/tibco/log/FH_HKOM/forcehkom_FH_HKOM_#yy#mm#dd.txt twsadmin admeqd c:/temp/ forcehkom_FH_HKOM.txt ascii get 0 0
FTPLocalClient Ftp;
if(arg.length>9)
Ftp = new FTPLocalClient( arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],arg[7],arg[8],Integer.valueOf(arg[9]).intValue(),Integer.valueOf(arg[10]).intValue());
else
Ftp = new FTPLocalClient( arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],Integer.valueOf(arg[7]).intValue(), Integer.valueOf(arg[8]).intValue());
try{
Ftp.DoFtp(false);
}catch (java.io.IOException e){ System.out.println(e); System.exit(1);}
}
}

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录