import java.net.*;
import java.io.*;
import java.util.*;
class FtpClient {
static final boolean debug = false;
public static final int FTP_PORT = 21; // ftp的端口
static int FTP_SUCCESS = 1;
static int FTP_TRY_AGAIN = 2;
static int FTP_ERROR = 3;
// 数据传输套接字
private Socket dataSocket = null;
private boolean replyPending = false;
private boolean binaryMode = false;
private boolean passiveMode = false;
String user = null; // 登录用的用户名
String password = null; // 登录用的密码
String command; // 最近一次命令
int lastReplyCode; // 最近的响应信息
public String welcomeMsg; // 服务器的欢迎信息
// 从服务器返回的响应字符串对象
protected Vector serverResponse = new Vector(1);
protected Socket serverSocket = null; // 与服务器通信的套接字
public PrintWriter serverOutput;
public InputStream serverInput; // 从服务器读取响应的缓冲流
/** 返回服务器连接的状态 */
public boolean serverIsOpen() {
return serverSocket != null;
}
/** 设置为被动模式 */
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) {
// 此行不存在响应码,循环跳到下一次
continue;
}
serverResponse.addElement(response);
if (continuingCode != -1) {
/* we've seen a XXX- sequence */
if (code != continuingCode ||
(response.length() >= 4 && response.charAt(3) == '-')) {
continue;
} else {
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;
}
/** 发送命令 <i>cmd</i> 给服务器 */
public void sendServer(String cmd) {
if (debug) System.out.println("sendServer start");
serverOutput.println(cmd);
if (debug) System.out.println("sendServer done");
}
/** 返回服务器的所有响应字符串 */
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;
}
/** 发送 QUIT 命令给服务器并关闭连接 */
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;
case 2:// 这个case用来以后扩展功能
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!");
}
}
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) {
// 首先尝试被动模式传输
try {
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, ",");
String[] nums = new String[6];
int i = 0;
while (st.hasMoreElements()) {
try {
nums[i] = st.nextToken();
i++;
} catch (Exception a) {
a.printStackTrace();
}
}
ipaddress = nums[0] + "." + nums[1] + "." + nums[2] + "."
+ nums[3];
try {
int firstbits = Integer.parseInt(nums[4]) << 8;
int lastbits = Integer.parseInt(nums[5]);
port = firstbits + lastbits;
} catch (Exception b) {
b.printStackTrace();
}
if ((ipaddress != null) && (port != 0)) {
dataSocket = new Socket(ipaddress, port);
} else {
e = new FtpProtocolException("PASV");
throw e;
}
if (issueCommand(cmd) == FTP_ERROR) {
e = new FtpProtocolException(cmd);
throw e;
}
} catch (FtpProtocolException fpe) {
portCmd = "PORT ";
// 附加host地址
for (int i = 0; i < addr.length; i++) {
portCmd = portCmd + (addr[i] & 0xFF) + ",";
}
try {
portSocket = new ServerSocket(20000);
// 附加端口
portCmd = portCmd
+ ((portSocket.getLocalPort() >>> 8) & 0xff) + ","
+ (portSocket.getLocalPort()
没有合适的资源?快使用搜索试试~ 我知道了~
java 150例子
共272个文件
java:166个
class:25个
xml:20个
需积分: 33 8 下载量 180 浏览量
2009-09-07
11:01:18
上传
评论
收藏 1.18MB RAR 举报
温馨提示
150个java小程序例子,有益于初学者。
资源推荐
资源详情
资源评论
收起资源包目录
java 150例子 (272个子文件)
flute+hrn+mrmba.aif 516KB
flute+hrn+mrmba.aif 516KB
spacemusic.au 47KB
spacemusic.au 47KB
AccountBean.class 8KB
regpost_jsp.class 7KB
index_jsp.class 6KB
reg_jsp.class 6KB
DBConn.class 6KB
CalculateUtil.class 4KB
AccountClient.class 4KB
reg.class 3KB
login.class 3KB
CountClient.class 2KB
Client.class 2KB
checkLogin.class 2KB
CountBean.class 1KB
CalculateSession.class 879B
AccountPK.class 827B
CalculateBean.class 651B
AccountException.class 634B
AccountHome.class 567B
Account.class 487B
CalculateHome.class 404B
CalculateLocalHome.class 398B
CountHome.class 252B
Calculate.class 205B
Count.class 199B
CalculateLocal.class 145B
.classpath 1KB
.classpath 547B
.classpath 547B
TestDLL.cpp 352B
web-app_2.2.dtd 18KB
tu.gif 21KB
tu.gif 21KB
SCREAN.GIF 2KB
r.GIF 1KB
x.GIF 1KB
DOT.GIF 827B
TestDLL.h 546B
upload.htm 440B
webOrder.html 7KB
JavaMail.html 1KB
shop.html 1KB
Input.html 553B
Test.html 478B
SimpleApplet1092536846768.html 99B
FtpClient.java 20KB
Calculator.java 9KB
AccountBean.java 9KB
MIDPApplication.java 7KB
QueryDB.java 7KB
JDialogDemo.java 6KB
ClipImage.java 6KB
Snake.java 5KB
MultiFieldsMIDlet.java 5KB
QQ.java 5KB
CalculateUtil.java 5KB
DBConn.java 5KB
JTreeDemo.java 5KB
Clock.java 5KB
NotePad.java 5KB
ViewDB.java 5KB
MulticastDemo.java 4KB
XMLTest.java 4KB
EllipseButton.java 4KB
JTableDemo.java 4KB
MessageBean.java 4KB
SoundApplet.java 4KB
FileUpload.java 4KB
Browser.java 4KB
UploadClient.java 4KB
DigitalClock.java 4KB
DBOperation.java 4KB
WebAppConfig.java 3KB
HttpsServer.java 3KB
DOMTreeWalker.java 3KB
MessageClientf.java 3KB
MailTest.java 3KB
reg.java 3KB
Dinner.java 3KB
List.java 3KB
WebBrowser.java 3KB
IOStreamDemo.java 3KB
Fader.java 3KB
RSADemo.java 3KB
TicTacToe.java 3KB
ConnectHttpMIDlet.java 3KB
CountClient.java 3KB
DigitalSignDemo.java 2KB
ThreadLister.java 2KB
SetSavepoint.java 2KB
MenuDemo.java 2KB
AccountClient.java 2KB
JScrollPaneDemo.java 2KB
TransactionDemo.java 2KB
JSplashWindowPlus.java 2KB
MidiDemo.java 2KB
CMD.java 2KB
共 272 条
- 1
- 2
- 3
资源评论
sycrab
- 粉丝: 1
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功