import java.net.*;
import java.io.*;
import java.util.*;
class FtpClient {
static final boolean debug = false;
public static final int FTP_PORT = 21;
static int FTP_SUCCESS = 1;
static int FTP_TRY_AGAIN = 2;
static int FTP_ERROR = 3;
/** socket for data transfer */
private Socket dataSocket = null;
private boolean replyPending = false;
private boolean binaryMode = false;
private boolean passiveMode = false;
/** user name for login */
String user = null;
/** password for login */
String password = null;
/** last command issued */
String command;
/** The last reply code from the ftp daemon. */
int lastReplyCode;
/** Welcome message from the server, if any. */
public String welcomeMsg;
/** Array of strings (usually 1 entry) for the last reply
from the server. */
protected Vector serverResponse = new Vector(1);
/** Socket for communicating with server. */
protected Socket serverSocket = null;
/** Stream for printing to the server. */
public PrintWriter serverOutput;
/** Buffered stream for reading replies from server. */
public InputStream serverInput;
/** Return server connection status */
public boolean serverIsOpen() {
return serverSocket != null;
}
/**Set Passive mode Trasfers*/
public void setPassive(boolean mode) {
passiveMode = mode;
}
public int readServerResponse() throws IOException {
StringBuffer replyBuf = new StringBuffer(32);
int c;
int continuingCode = -1;
int code = -1;
String response;
if (debug) System.out.println("readServerResponse start");
try{
while (true) {
//if (debug) System.out.println("readServerResponse outer while loop: "+ serverInput.available());
while ((c = serverInput.read()) != -1) {
if (c == '\r') {
if ((c = serverInput.read()) != '\n')
replyBuf.append('\r');
}
replyBuf.append((char)c);
if (c == '\n')
break;
}
if (debug) System.out.println("Now past inner while loop");
response = replyBuf.toString();
replyBuf.setLength(0);
if (debug) {
System.out.print(response);
}
try {
code = Integer.parseInt(response.substring(0, 3));
} catch (NumberFormatException e) {
code = -1;
} catch (StringIndexOutOfBoundsException e) {
/* this line doesn't contain a response code, so
we just completely ignore it */
continue;
}
serverResponse.addElement(response);
if (continuingCode != -1) {
/* we've seen a XXX- sequence */
if (code != continuingCode ||
(response.length() >= 4 && response.charAt(3) == '-')) {
continue;
} else {
/* seen the end of code sequence */
continuingCode = -1;
break;
}
} else if (response.length() >= 4 && response.charAt(3) == '-') {
continuingCode = code;
continue;
} else {
break;
}
}
}catch(Exception e){e.printStackTrace();}
if (debug) System.out.println("readServerResponse done");
return lastReplyCode = code;
}
/** Sends command <i>cmd</i> to the server. */
public void sendServer(String cmd) {
if (debug) System.out.println("sendServer start");
serverOutput.println(cmd);
if (debug) System.out.println("sendServer done");
}
/** Returns all server response strings. */
public String getResponseString() {
String s = new String();
for(int i = 0;i < serverResponse.size();i++) {
s+=serverResponse.elementAt(i);
}
serverResponse = new Vector(1);
return s;
}
public String getResponseStringNoReset() {
String s = new String();
for(int i = 0;i < serverResponse.size();i++) {
s+=serverResponse.elementAt(i);
}
return s;
}
/**
* issue the QUIT command to the FTP server and close the connection.
*/
public void closeServer() throws IOException {
if (serverIsOpen()) {
issueCommand("QUIT");
if (! serverIsOpen()) {
return;
}
serverSocket.close();
serverSocket = null;
serverInput = null;
serverOutput = null;
}
}
protected int issueCommand(String cmd) throws IOException {
command = cmd;
int reply;
if (debug) System.out.println(cmd);
if (replyPending) {
if (debug) System.out.println("replyPending");
if (readReply() == FTP_ERROR)
System.out.print("Error reading pending reply\n");
}
replyPending = false;
do {
sendServer(cmd);
reply = readReply();
//if (debug) System.out.println("in while loop of issueCommand method");
} while (reply == FTP_TRY_AGAIN);
return reply;
}
protected void issueCommandCheck(String cmd) throws IOException {
if (debug) System.out.println("issueCommandCheck");
if (issueCommand(cmd) != FTP_SUCCESS) {
throw new FtpProtocolException(cmd);
}
}
protected int readReply() throws IOException {
lastReplyCode = readServerResponse();
switch (lastReplyCode / 100) {
case 1:
replyPending = true;
/* falls into ... */
case 2://This case is for future purposes. If not properly used, it might cause an infinite loop.
//Don't add any code here , unless you know what you are doing.
case 3:
return FTP_SUCCESS;
case 5:
if (lastReplyCode == 530) {
if (user == null) {
throw new FtpLoginException("Not logged in");
}
return FTP_ERROR;
}
if (lastReplyCode == 550) {
if (!command.startsWith("PASS"))
throw new FileNotFoundException(command);
else
throw new FtpLoginException("Error: Wrong Password!");
}
}
/* this statement is not reached */
return FTP_ERROR;
}
protected Socket openDataConnection(String cmd) throws IOException {
ServerSocket portSocket = null;
String portCmd;
InetAddress myAddress = InetAddress.getLocalHost();
byte addr[] = myAddress.getAddress();
int shift;
String ipaddress;
int port = 0;
IOException e;
if (this.passiveMode) {
/* First let's attempt to initiate Passive transfers */
try { // PASV code
getResponseString();
if (issueCommand("PASV") == FTP_ERROR) {
e = new FtpProtocolException("PASV");
throw e;
}
String reply = getResponseStringNoReset();
reply = reply.substring(reply.lastIndexOf("(")+1,reply.lastIndexOf(")"));
StringTokenizer st = new StringTokenizer(reply, ",");
S
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
【推荐】140个Java编程实例源代码 (461个子文件)
MemMapCppClient.aps 34KB
CsClient.java.bak 2KB
Server.java.bak 1KB
theServerBean.java.bak 608B
run.bat 367B
build.bat 63B
winter.bmp 147KB
winter.bmp 147KB
WinReg.c 10KB
CallWinAPI.c 407B
ClipImage.class 7KB
CubicPanel.class 6KB
EditorFrame.class 5KB
FontSelection.class 4KB
FileUpload.class 3KB
JSplashWindowEx.class 3KB
SimpleDB.class 3KB
RoundButton.class 2KB
CardLayoutFrame.class 2KB
JSplashWindow.class 2KB
LoginDialogDemo.class 2KB
JNIFrame.class 2KB
ATM.class 2KB
MultiFrame.class 2KB
SplitDemo.class 2KB
JtreeDemo.class 2KB
FlowLayoutDemo.class 2KB
FontDemo.class 2KB
JtableDemo.class 2KB
TransformsDemo.class 1KB
LookAndFeelDemo.class 1KB
ExampServlet.class 1KB
Cubic.class 1KB
chatreg.class 1KB
ConDB.class 1KB
DummyCart.class 1KB
DrawPanel.class 1KB
FontPanel.class 1KB
HTML_FormatTag.class 1KB
BackgroundPanel.class 1KB
myFrame.class 1KB
NewPanel.class 1KB
ToolTipDemo.class 1KB
JTextPaneDemo.class 945B
GridLayoutDemo.class 944B
BorderLayoutDemo.class 819B
DrawDemo.class 766B
HTML_Format.class 763B
BoxLayoutFrame.class 726B
ClipImage$1.class 689B
ATM$1.class 687B
JNIFrame$1.class 675B
DrawPanel$1.class 663B
fileDialogDemo.class 558B
EditorFrame$1.class 472B
FontSelection$1.class 471B
JtreeDemo$1.class 461B
Cubic$1.class 447B
MyWindowAdapter.class 431B
counter.class 371B
fileDialog.class 357B
BackgroundDemo.class 303B
MemMapCppClient.clw 889B
MemMapLib.cpp 4KB
MemMapCppClientDlg.cpp 4KB
MemMapProxyLib.cpp 3KB
Client.cpp 3KB
MyWindow.CPP 3KB
MemMapCppClient.cpp 2KB
HideConsole.cpp 2KB
JNIFrame.cpp 571B
StdAfx.cpp 217B
a.css 406B
Thumbs.db 20KB
Thumbs.db 9KB
Thumbs.db 3KB
WinLib.dll 200KB
实例79 BS结构与数据库.doc 215KB
实例70CS聊天室.doc 173KB
实例69服务器端程序.doc 128KB
实例68 客户端程序.doc 127KB
实例115 简单的例子.doc 126KB
实例118 JTA与JTS.doc 121KB
实例116UML建模与J2EE开发.doc 108KB
MemMapProxyLib.dsp 4KB
MemMapLib.dsp 4KB
MemMapCppClient.dsp 4KB
MemMapProxyLib.dsw 553B
MemMapCppClient.dsw 553B
MemMapLib.dsw 541B
EnrollApp.ear 16KB
logo.GIF 227KB
output_真实的世界模型.gif 23KB
wsp_2.gif 9KB
red.GIF 5KB
bt.gif 5KB
go1.gif 2KB
go2.gif 2KB
go.gif 1KB
JNIFrame.h 5KB
共 461 条
- 1
- 2
- 3
- 4
- 5
资源评论
CLeopard
- 粉丝: 178
- 资源: 449
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功