📄 ftpclient.java
字号:
// Decompiled by DJ v3.9.9.91 Copyright 2005 Atanas Neshkov Date: 2007-6-22 16:24:33
// Home Page : http://members.fortunecity.com/neshkov/dj.html - Check often for new version!
// Decompiler options: packimports(3)
// Source File Name: FtpClient.java
package sun.net.ftp;
import java.io.*;
import java.net.*;
import java.security.AccessController;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import sun.misc.REException;
import sun.misc.RegexpPool;
import sun.net.*;
import sun.security.action.GetPropertyAction;
// Referenced classes of package sun.net.ftp:
// FtpLoginException, FtpProtocolException
public class FtpClient extends TransferProtocolClient
{
public static int getFtpProxyPort()
{
int ai[] = {
80
};
AccessController.doPrivileged(new Object(ai) /* anonymous class not found */
class _anm2 {}
);
return ai[0];
}
protected int readReply()
throws IOException
{
lastReplyCode = readServerResponse();
switch(lastReplyCode / 100)
{
case 1: // '\001'
replyPending = true;
// fall through
case 2: // '\002'
case 3: // '\003'
return FTP_SUCCESS;
case 5: // '\005'
if(lastReplyCode == 530)
if(!loggedIn)
throw new FtpLoginException("Not logged in");
else
return FTP_ERROR;
if(lastReplyCode == 550)
throw new FileNotFoundException(command + ": " + getResponseString());
// fall through
case 4: // '\004'
default:
return FTP_ERROR;
}
}
public FtpClient()
{
replyPending = false;
binaryMode = false;
loggedIn = false;
}
public void ascii()
throws IOException
{
issueCommandCheck("TYPE A");
binaryMode = false;
}
public void binary()
throws IOException
{
issueCommandCheck("TYPE I");
binaryMode = true;
}
public void cdUp()
throws IOException
{
issueCommandCheck("CDUP");
}
public void closeServer()
throws IOException
{
if(serverIsOpen())
{
issueCommand("QUIT");
super.closeServer();
}
}
protected void finalize()
throws IOException
{
if(serverIsOpen())
closeServer();
}
public void noop()
throws IOException
{
issueCommandCheck("NOOP");
}
public void reInit()
throws IOException
{
issueCommandCheck("REIN");
loggedIn = false;
}
public static boolean getUseFtpProxy()
{
return getFtpProxyHost() != null;
}
public static String getFtpProxyHost()
{
return (String)AccessController.doPrivileged(new Object() /* anonymous class not found */
class _anm1 {}
);
}
public String pwd()
throws IOException
{
issueCommandCheck("PWD");
String s = getResponseString();
if(!s.startsWith("257"))
throw new FtpProtocolException("PWD failed. " + s);
else
return s.substring(5, s.lastIndexOf('"'));
}
public String system()
throws IOException
{
issueCommandCheck("SYST");
String s = getResponseString();
if(!s.startsWith("215"))
throw new FtpProtocolException("SYST failed." + s);
else
return s.substring(4);
}
protected int issueCommand(String s)
throws IOException
{
command = s;
while(replyPending)
{
replyPending = false;
if(readReply() == FTP_ERROR)
throw new FtpProtocolException("Error reading FTP pending reply\n");
}
int i;
do
{
sendServer(s + "\r\n");
i = readReply();
} while(i == FTP_TRY_AGAIN);
return i;
}
public FtpClient(String s)
throws IOException
{
replyPending = false;
binaryMode = false;
loggedIn = false;
openServer(s, 21);
}
public void cd(String s)
throws IOException
{
if(s == null || "".equals(s))
{
return;
} else
{
issueCommandCheck("CWD " + s);
return;
}
}
protected void issueCommandCheck(String s)
throws IOException
{
if(issueCommand(s) != FTP_SUCCESS)
throw new FtpProtocolException(s + ":" + getResponseString());
else
return;
}
public void openServer(String s)
throws IOException
{
openServer(s, 21);
}
public static boolean matchNonProxyHosts(String s)
{
synchronized(sun.net.ftp.FtpClient.class)
{
String s1 = (String)AccessController.doPrivileged(new GetPropertyAction("ftp.nonProxyHosts"));
if(s1 == null)
nonProxyHostsPool = null;
else
if(!s1.equals(nonProxyHostsSource))
{
RegexpPool regexppool = new RegexpPool();
StringTokenizer stringtokenizer = new StringTokenizer(s1, "|", false);
try
{
while(stringtokenizer.hasMoreTokens())
regexppool.add(stringtokenizer.nextToken().toLowerCase(), Boolean.TRUE);
}
catch(REException reexception)
{
System.err.println("Error in http.nonProxyHosts system property: " + reexception);
}
nonProxyHostsPool = regexppool;
}
nonProxyHostsSource = s1;
}
if(nonProxyHostsPool == null)
return false;
return nonProxyHostsPool.match(s) != null;
}
public FtpClient(String s, int i)
throws IOException
{
replyPending = false;
binaryMode = false;
loggedIn = false;
openServer(s, i);
}
public void openServer(String s, int i)
throws IOException
{
super.openServer(s, i);
if(readReply() == FTP_ERROR)
throw new FtpProtocolException("Welcome message: " + getResponseString());
else
return;
}
protected Socket openPassiveDataConnection()
throws IOException
{
InetSocketAddress inetsocketaddress = null;
if(issueCommand("EPSV ALL") == FTP_SUCCESS)
{
if(issueCommand("EPSV") == FTP_ERROR)
throw new FtpProtocolException("EPSV Failed: " + getResponseString());
String s = getResponseString();
Pattern pattern = Pattern.compile("^229 .* \\(\\|\\|\\|(\\d+)\\|\\)");
Matcher matcher = pattern.matcher(s);
if(!matcher.find())
throw new FtpProtocolException("EPSV failed : " + s);
String s2 = matcher.group(1);
int i = Integer.parseInt(s2);
InetAddress inetaddress = serverSocket.getInetAddress();
inetsocketaddress = new InetSocketAddress(inetaddress, i);
} else
{
if(issueCommand("PASV") == FTP_ERROR)
throw new FtpProtocolException("PASV failed: " + getResponseString());
String s1 = getResponseString();
Pattern pattern1 = Pattern.compile("227 .* \\(?(\\d{1,3},\\d{1,3},\\d{1,3},\\d{1,3}),(\\d{1,3}),(\\d{1,3})\\)?");
Matcher matcher1 = pattern1.matcher(s1);
if(!matcher1.find())
throw new FtpProtocolException("PASV failed : " + s1);
int j = Integer.parseInt(matcher1.group(3)) + (Integer.parseInt(matcher1.group(2)) << 8);
String s3 = matcher1.group(1).replace(',', '.');
inetsocketaddress = new InetSocketAddress(s3, j);
}
Socket socket = new Socket();
if(defaultConnectTimeout > 0)
socket.connect(inetsocketaddress, defaultConnectTimeout);
else
socket.connect(inetsocketaddress);
if(defaultSoTimeout > 0)
socket.setSoTimeout(defaultSoTimeout);
return socket;
}
public TelnetInputStream list()
throws IOException
{
Socket socket = openDataConnection("LIST");
return new TelnetInputStream(socket.getInputStream(), binaryMode);
}
public void login(String s, String s1)
throws IOException
{
if(!serverIsOpen())
throw new FtpLoginException("not connected to host");
if(s == null || s.length() == 0)
return;
if(issueCommand("USER " + s) == FTP_ERROR)
throw new FtpLoginException("user " + s + " : " + getResponseString());
if(lastReplyCode == 331 && (s1 == null || s1.length() == 0 || issueCommand("PASS " + s1) == FTP_ERROR))
throw new FtpLoginException("password: " + getResponseString());
StringBuffer stringbuffer = new StringBuffer();
for(int i = 0; i < serverResponse.size(); i++)
{
String s2 = (String)serverResponse.elementAt(i);
if(s2 == null)
continue;
if(s2.length() >= 4 && s2.startsWith("230"))
s2 = s2.substring(4);
stringbuffer.append(s2);
}
welcomeMsg = stringbuffer.toString();
loggedIn = true;
}
public void rename(String s, String s1)
throws IOException
{
issueCommandCheck("RNFR " + s);
issueCommandCheck("RNTO " + s1);
}
protected Socket openDataConnection(String s)
throws IOException
{
Socket socket;
socket = openPassiveDataConnection();
if(issueCommand(s) == FTP_ERROR)
{
socket.close();
break MISSING_BLOCK_LABEL_30;
}
return socket;
IOException ioexception;
ioexception;
ServerSocket serversocket = new ServerSocket(0, 1);
InetAddress inetaddress = serversocket.getInetAddress();
if(inetaddress.isAnyLocalAddress())
inetaddress = getLocalAddress();
String s1 = "EPRT |" + ((inetaddress instanceof Inet6Address) ? "2" : "1") + "|" + inetaddress.getHostAddress() + "|" + serversocket.getLocalPort() + "|";
if(issueCommand(s1) == FTP_ERROR || issueCommand(s) == FTP_ERROR)
{
String s2 = "PORT ";
byte abyte0[] = inetaddress.getAddress();
for(int i = 0; i < abyte0.length; i++)
s2 = s2 + (abyte0[i] & 0xff) + ",";
s2 = s2 + (serversocket.getLocalPort() >>> 8 & 0xff) + "," + (serversocket.getLocalPort() & 0xff);
if(issueCommand(s2) == FTP_ERROR)
{
FtpProtocolException ftpprotocolexception = new FtpProtocolException("PORT :" + getResponseString());
serversocket.close();
throw ftpprotocolexception;
}
if(issueCommand(s) == FTP_ERROR)
{
FtpProtocolException ftpprotocolexception1 = new FtpProtocolException(s + ":" + getResponseString());
serversocket.close();
throw ftpprotocolexception1;
}
}
Socket socket1 = serversocket.accept();
if(defaultSoTimeout != -1)
socket1.setSoTimeout(defaultSoTimeout);
serversocket.close();
return socket1;
}
public TelnetInputStream get(String s)
throws IOException
{
Socket socket;
try
{
socket = openDataConnection("RETR " + s);
}
catch(FileNotFoundException filenotfoundexception)
{
StringTokenizer stringtokenizer = new StringTokenizer(s, "/");
String s1 = null;
do
{
if(!stringtokenizer.hasMoreElements())
break;
s1 = stringtokenizer.nextToken();
if(!stringtokenizer.hasMoreElements())
break;
try
{
cd(s1);
}
catch(FtpProtocolException ftpprotocolexception)
{
throw filenotfoundexception;
}
} while(true);
if(s1 != null)
socket = openDataConnection("RETR " + s1);
else
throw filenotfoundexception;
}
return new TelnetInputStream(socket.getInputStream(), binaryMode);
}
public TelnetInputStream nameList(String s)
throws IOException
{
Socket socket;
if(s != null)
socket = openDataConnection("NLST " + s);
else
socket = openDataConnection("NLST");
return new TelnetInputStream(socket.getInputStream(), binaryMode);
}
public TelnetOutputStream append(String s)
throws IOException
{
Socket socket = openDataConnection("APPE " + s);
TelnetOutputStream telnetoutputstream = new TelnetOutputStream(socket.getOutputStream(), binaryMode);
if(!binaryMode)
telnetoutputstream.setStickyCRLF(true);
return telnetoutputstream;
}
public TelnetOutputStream put(String s)
throws IOException
{
Socket socket = openDataConnection("STOR " + s);
TelnetOutputStream telnetoutputstream = new TelnetOutputStream(socket.getOutputStream(), binaryMode);
if(!binaryMode)
telnetoutputstream.setStickyCRLF(true);
return telnetoutputstream;
}
public static final int FTP_PORT = 21;
static int FTP_SUCCESS = 1;
static int FTP_TRY_AGAIN = 2;
static int FTP_ERROR = 3;
private boolean replyPending;
private boolean binaryMode;
private boolean loggedIn;
private static RegexpPool nonProxyHostsPool = null;
private static String nonProxyHostsSource = null;
String command;
int lastReplyCode;
public String welcomeMsg;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -