📄 ftpserverthread.java
字号:
package com.socket;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Calendar;
import java.util.ResourceBundle;
/**
* 文件传输服务器端的线程
*
* @author java
*
*/
public class FtpServerThread extends Thread {
private Socket _client;
private DataInputStream _dis;//输入信息流
private DataOutputStream _dos;//输出信息流
private String _threadId = "unKnown";
private boolean _isLog = false;
private String _logFile = "/tmp/ftplog.txt";
private static final String _bye = "bye";
private static final String _help = "help";
protected FileOutputStream _logfile;
public FtpServerThread(ResourceBundle config, Socket s, String id)
throws IOException {
//读配置文件
_isLog = Integer.parseInt(config.getString("isLog")) > 0 ? true : false;
_logFile = config.getString("logFile");
_client = s;
_threadId = id;
Calendar now = Calendar.getInstance();
String log = "[SYS] " + _client.getInetAddress().getHostAddress()
+ " login at " + now.getTime().toString() + " as thread "
+ _threadId + ".\n";
System.out.print(log);
if (_isLog) {
_logfile = new FileOutputStream(_logFile, true);
byte[] tmp = log.getBytes();
_logfile.write(tmp);
_logfile.close();
}
start();
}
//获得客户端命令
private String getMessage() throws IOException {
int ch;
byte[] btArr = new byte[1];
byte[] input = new byte[255];
int index = 0;
do {
ch = _dis.read(btArr);
input[index++] = btArr[0];
} while ((char) btArr[0] != '\n' && index < 255 && ch != -1); //防止溢出
String s = new String(input, 0, index - 1);
return s;
}
//向客户端送信息
private void sendMessage(String msg) {
try {
_dos.write(msg.getBytes());
//System.out.println("[SYS] SENT=@" + msg + "@");
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void run() {
try {
boolean flag = true;
_dis = new DataInputStream(new BufferedInputStream(_client
.getInputStream()));//初始化输入流
_dos = new DataOutputStream(new BufferedOutputStream(_client
.getOutputStream()));//初始化输出流
int loop = 0;
while (flag) {
//获得文件名,格式:GET /tmp/test.doc
String message = getMessage();
message = message == null ? "" : message.trim();
System.out.println("[SYS] Thread " + _threadId
+ " receive command \"" + message + "\"");
loop = message.length() > 0 ? 0 : loop + 1;
if (loop > 10) {
message = "BYE";
}
//发送文件
if (message.equalsIgnoreCase(_bye)) {
flag = false;
System.out.println("[SYS] Thread " + _threadId
+ " says BYE.");
} else if (message.startsWith("GET ")) {
File file = new File(message.substring(4));
if (file.exists() && file.isFile()) {
System.out.println("[SYS] Thread " + _threadId
+ " sending " + file.toString());
DataInputStream fs = new DataInputStream(
new BufferedInputStream(new FileInputStream(
file)));
byte[] bt = new byte[2048];
int b = 0;
while ((b = fs.read(bt)) != -1) {
_dos.write(bt, 0, b);
}
_dos.flush();
fs.close();
System.out.println("[SYS] Thread " + _threadId
+ " sendover " + file.toString());
flag = false;
} else {
sendMessage("[S]File not found.\n");
}
} else {
sendMessage("[S]Invalid Command.\n");
}
}
} catch (IOException ep) {
ep.printStackTrace();
} finally {
try {
_dis.close();
_dos.close();
_client.close();
System.out.println("[SYS] Thread " + _threadId + " closed");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -