📄 jmyftpclient.java.bak
字号:
/*
* Created on 2005-6-28
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
/**
* @author CN2WW0D0
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
import java.io.*;
import java.net.*;
import java.util.*;
import sun.net.smtp.SmtpClient;
//import sun.net.*;
//import sun.net.ftp.*;
public class JMyFtpClient
{
/**
*
*/
public JMyFtpClient()
{
super();
// TODO Auto-generated constructor stub
m_FtpClient = null;
}
public static void main(String[] args) throws ClassNotFoundException, IOException
{
System.out.println( "JMyFtpClient Started" );
SmtpClient client = new SmtpClient( "172.16.64.21" );
String from="orion_wang@sohu.com";
String to="wang.wei.ext@siemens.com";
System.out.println( from + " " + to );
/*
try{
client.openServer( "172.16.64.21" , 0);
client.from(from);
client.to(to);
PrintStream message = client.startMessage();
message.println("To: " + to);
message.println("Subject: Sending email from JSP!");
message.println("This was sent from a JSP page!");
message.println();
message.println("Cool! ");
message.println();
message.println("Good Boy");
message.println("I'm in genius.com");
message.println();
client.closeServer();
}
catch (IOException e)
{
e.printStackTrace();
}
*/
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
JMyFtpClient ftp = new JMyFtpClient();
if ( false == ftp.Open( "172.16.64.21" , 21 , "application" , "application" ) )
{
System.out.println( "OpenFail" );
return;
}
String strListFile = ftp.ListFile();
trace( "List : " + strListFile );
int nByteDown = ftp.DownloadFile( "3.txt" , "C:\\12.txt" );
if ( nByteDown < 0 )
System.out.println( "Download File Fail" );
int nByteUp = ftp.UploadFile( "C:\\1.txt" , "1.txt" );
System.out.println( "Upload File Fail" );
ftp.Close();
System.out.println( "JMyFtpClient Stopped" );
}
/*Open Connection*/
public boolean Open( String strServIPAddr , int nServPort ,
String strUserName , String strPassword )
{
try
{
m_FtpClient = new FTPClientDrv( strServIPAddr , nServPort ); //Connect to Server);
m_FtpClient.login( strUserName , strPassword );
m_FtpClient.setType(new FTPTransferType());
}
catch (IOException e)
{
trace( "Open Ftp Server Fail, IOException" );
e.printStackTrace();
return false;
} catch (FTPException e)
{
// TODO Auto-generated catch block
trace( "Open Ftp server Fail, Ftp Exception" );
e.printStackTrace();
return false;
}
trace( "Login FTP Server Ok" );
return true;
}
public String ListFile()
{
String strList = "";
try
{
strList = m_FtpClient.list( "" , true );
}
catch ( IOException e )
{
e.printStackTrace();
return "";
}
catch ( FTPException e)
{
e.printStackTrace();
return "";
}
return strList;
}
/*
* Download File
*
* */
public int DownloadFile( String strSrcFile , String strDstFile )
{
trace( "Begin Downloading File " + strSrcFile + " to " + strDstFile );
int nRead = 0;
int nTmpRead = 0;
byte [] abyBuf = new byte[1024*32];
File f = new File( strDstFile );
f.mkdirs();
try
{
m_FtpClient.get( strSrcFile , strDstFile );
}
catch (IOException e)
{
e.printStackTrace();
return -1;
}
catch (FTPException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
return nRead;
}
/*
* UploadFile
* */
public int UploadFile( String strSrcFile , String strDstFile )
{
int nWrite = 0;
try
{
// m_FtpClient.mkdir( strDstFile );
nWrite = m_FtpClient.put( strSrcFile , strDstFile );
}
catch( IOException e)
{
return -1;
}
catch (FTPException e)
{
e.printStackTrace();
return -1;
}
return nWrite;
}
/*Close Connection*/
public void Close()
{
if ( null != m_FtpClient )
{
try
{
m_FtpClient.quit();
m_FtpClient = null;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch ( FTPException eFtp )
{
}
}
}
public static void trace( String s )
{
System.out.println( s );
}
/*Members*/
private FTPClientDrv m_FtpClient;
}
/**/
class StringTrimHelper
{
public static String TrimLastSplitter( String strSrc )
{
strSrc.trim();
if ( strSrc.lastIndexOf("\\") == strSrc.length()-1 )
return strSrc;
return strSrc.substring(0 , strSrc.lastIndexOf("\\"));
}
public static String GetPathName( String strSrc )
{
String strTmp = TrimLastSplitter( strSrc );
return strTmp.substring( 0, strTmp.lastIndexOf("\\"));
}
}
/**
* Supports client-side FTP. Most common
* FTP operations are present in this class.
* Lots to do, but works ok.
*
* @author Bruce Blackshaw
* @version $Revision: 1.1.1.1 $
*
*/
class FTPClientDrv
{
public static void trace( String s )
{
System.out.println( s );
}
/**
* Revision control id
*/
private static String cvsId = "$Id: FTPClient.java,v 1.1.1.1 2001/11/06 10:36:52 idanso Exp $";
/**
* Socket responsible for controlling
* the connection
*/
private FTPControlSocket control = null;
/**
* Socket responsible for transferring
* the data
*/
private Socket data = null;
public short tmode=-1; // Data transfer mode. 0=GET 1=PUT -1=none
public long tbytes=0; // Total bytes to transfer
public long sbytes=0; // Number of bytes transferred
/**
* Record of the transfer type
*/
private FTPTransferType transferType = null;
/**
* Constructor. Creates the control
* socket
*
* @param remoteHost the remote hostname
*/
public FTPClientDrv(String remoteHost)
throws IOException, FTPException {
control = new FTPControlSocket(remoteHost);
}
/**
* Constructor. Creates the control
* socket
*
* @param remoteHost the remote hostname
* @param controlPort port for control stream
*/
public FTPClientDrv(String remoteHost, int controlPort)
throws IOException, FTPException {
control = new FTPControlSocket(remoteHost, controlPort);
}
/**
* Constructor. Creates the control
* socket
*
* @param remoteAddr the address of the
* remote host
*/
public FTPClientDrv(InetAddress remoteAddr)
throws IOException, FTPException {
control = new FTPControlSocket(remoteAddr);
}
/**
* Constructor. Creates the control
* socket. Allows setting of control port (normally
* set by default to 21).
*
* @param remoteAddr the address of the
* remote host
* @param controlPort port for control stream
*/
public FTPClientDrv(InetAddress remoteAddr, int controlPort)
throws IOException, FTPException {
control = new FTPControlSocket(remoteAddr, controlPort);
}
/**
* Login into an account on the FTP server. This
* call completes the entire login process
*
* @param user user name
* @param password user's password
*/
public void login(String user, String password)
throws IOException, FTPException {
String response = control.sendCommand("USER " + user);
control.validateReply(response, "331");
response = control.sendCommand("PASS " + password);
control.validateReply(response, "230");
}
/**
* Supply the user name to log into an account
* on the FTP server. Must be followed by the
* password() method - but we allow for
*
* @param user user name
* @param password user's password
*/
public void user(String user)
throws IOException, FTPException {
String reply = control.sendCommand("USER " + user);
// we allow for a site with no password - 230 response
String[] validCodes = {"230", "331"};
control.validateReply(reply, validCodes);
}
/**
* Supplies the password for a previously supplied
* username to log into the FTP server. Must be
* preceeded by the user() method
*
* @param user user name
* @param password user's password
*/
public void password(String password)
throws IOException, FTPException {
String reply = control.sendCommand("PASS " + password);
// we allow for a site with no passwords (202)
String[] validCodes = {"230", "202"};
control.validateReply(reply, validCodes);
}
/**
* Set up SOCKS v4 proxy settings. This can be used if there
* is a SOCKS proxy server in place that must be connected thru.
*
* @param port SOCKS proxy port
* @param host SOCKS proxy hostname
*/
public void initSOCKS(String port, String host) {
Properties props = System.getProperties();
props.put("socksProxyPort", port);
props.put("socksProxyHost", host);
System.setProperties(props);
}
/**
* Get the name of the remote host
*
* @return remote host name
*/
String getRemoteHostName() {
return control.getRemoteHostName();
}
/**
* Issue arbitrary ftp commands to the FTP server.
*
* @param command ftp command to be sent to server
* @param validCodes valid return codes for this command
*/
public void quote(String command, String[] validCodes)
throws IOException, FTPException {
String reply = control.sendCommand(command);
// allow for no validation to be supplied
if (validCodes != null && validCodes.length > 0)
control.validateReply(reply, validCodes);
}
/*
* PutFile Directly
* */
public int put( String strSrcFile , String strDstFile ) throws IOException, FTPException
{
trace( "Entering put function......" );
// get an output stream
data = control.createDataSocket();
DataOutputStream out = new DataOutputStream(data.getOutputStream());
// send the command to store
String cmd = "STOR ";
String reply = control.sendCommand(cmd + strDstFile);
trace( "Sending to Server Cmd : " + cmd + strDstFile );
// Can get a 125 or a 150
String[] validCodes1 = {"125", "150"};
control.validateReply(reply, validCodes1);
trace( "Validated Reply, Begin Upload File..." );
byte[] buf=new byte[1024*4];
int nRead = 0;
int nTmpRead = 0;
RandomAccessFile fSrc = new RandomAccessFile( strSrcFile , "r");
// write stream
nTmpRead = fSrc.read( buf );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -