package FTPServer;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.sql.ResultSet;
import java.util.ArrayList;
import util.CommandParser;
import util.Message;
public class FTPWorker implements Runnable {
private Socket client = null;
private Thread theThread = null;
private static int count;
private static ThreadGroup threadGroup = null;
private static String prePath = null;
private String path = null;
private ResultSet rs = null;
private ConnSystem a = null;
private String sessionId = null;
ObjectInputStream objSockIn = null;
ObjectOutputStream objSockOut = null;
ArrayList lineparas = null;
Message gmsg = null;
String lineCommand = null;
SessionManager newSession = new SessionManager();
CommandParser myComParse = null;
/** ****************构造方法********** */
public FTPWorker(Socket s) {
client = s;
theThread = new Thread(threadGroup, this, "Socket" + count);
count++;
System.out.println("Socket" + count);
theThread.start();
}
/** ************数据库方法*************** */
// 用户查询
public int select(String sql) {
int k = 0;
a = new ConnSystem();
try {
k = 0;
a.stmt = a.conn.createStatement();
rs = a.stmt.executeQuery(sql);
while (rs.next()) {
path = rs.getString("folder");
sessionId = String.valueOf(rs.getInt("id"));
// System.out.println("sessionId:" + sessionId);
// System.out.println("path:" + path);
// int id=rs.getInt("id");
// String str=String.valueOf(id);
// sessionId=rs.getString("id");
newSession.setSession(sessionId, path);
k++;
}
} catch (Exception e) {
e.printStackTrace();
}
return k;
}
// 关闭数据库连接
public void close() {
try {
if (rs != null)
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (a.stmt != null)
a.stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if (a.conn != null) {
a.conn.close();
a.conn = null;
// System.err.println();
// System.err.println("数据连接+");
}
} catch (Exception e) {
System.out.println("close");
e.printStackTrace();
}
}
/** *******************方法******************* */
// 得到
public void gain() {
System.out.println("等待接收客户端的对象");
try {
objSockIn = new ObjectInputStream(client.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
try {
gmsg = (Message) objSockIn.readObject();
System.out.println("对象接收完毕");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 回复
public void revert(Message tempMsg) {
try {
System.out.println("开始给客户端返回对象......");
objSockOut = new ObjectOutputStream(client.getOutputStream());
objSockOut.writeObject(tempMsg);
objSockOut.flush();
System.out.println("对象返回完毕");
gmsg.getParas().clear();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(0);
}
}
// 登陆系统
public void loginSystem() {
myComParse = new CommandParser();
String user = (String) lineparas.get(0);
String password = (String) lineparas.get(1);
String sql = "select * from userinfo where username='" + user + "'"
+ "and password='" + password + "'";
int testNum = select(sql);
if (testNum > 0) {
System.out.print("登陆用户id"+sessionId+"/n目录"
+ newSession.getSession(sessionId));
Message tempMsglogin = null;
gmsg.getParas().clear();
tempMsglogin = myComParse.parse("MSG ok " + sessionId + " end");
prePath = newSession.getSession(sessionId);
revert(tempMsglogin);
} else {
Message tempMsgloginfail = myComParse.parse("MSG fail end");
revert(tempMsgloginfail);
}
}
private void logoutSystem() {
myComParse = new CommandParser();
Message tempMsglogout = null;
String sId = (String) lineparas.get(lineparas.size() - 1);
System.out.println("退出系统的用户ID:" + sId);
count--;
tempMsglogout = myComParse.parse("MSG ok endloginout");
// System.out.println(tempMsglogout.getCommand()+tempMsglogout.getType()+tempMsglogout.getParas().toString());
revert(tempMsglogout);
}
// 查询目录
@SuppressWarnings("unchecked")
private void ls() {
String currentpath = null;
Message tempMsgls = new Message();
String sId = (String) lineparas.get(lineparas.size() - 1);
currentpath = newSession.getSession(sId);
File f1 = new File(currentpath);
System.out.println("文件和目录总数:" + f1.list().length);
System.out.println("当前目录:" + currentpath);
String templs[]=null;
templs = f1.list();
myComParse = new CommandParser();
for (int i = 0; i < f1.list().length; i++) {
tempMsgls = myComParse.parse1("MSG " + templs[i]);
// System.out.println("内容:" + templs[i]);
}
// for(int i=0;i<templs.length;i++)
// {
// tempMsgls.getParas().add(templs[i]);
// }
revert(tempMsgls);
}
// 更改目录
private void cd() {
Message tempMsgcd = null;
String currentpath = null;
String sId = (String) lineparas.get(lineparas.size() - 1);
// System.out.println("sId=" + sId);
currentpath = newSession.getSession(sId);
if (lineparas.get(0).equals("/") || lineparas.get(0).equals("\\")) {
String sql = "select * from userinfo where id= '"
+ Integer.parseInt(sId) + "'";
int testNum = select(sql);
// System.out.println(testNum);
if (testNum > 0) {
myComParse = new CommandParser();
String currentpathg = newSession.getSession(sId);
System.out.println("用户目录:"
+ newSession.getSession(sessionId));
newSession.setSession(sessionId, currentpathg);
System.out.println("用户路径已经记录~!");
tempMsgcd = myComParse.parse("MSG ok endcdg");
revert(tempMsgcd);
} else {
myComParse = new CommandParser();
tempMsgcd = myComParse.parse("MSG fail endcdg");
revert(tempMsgcd);
}
tempMsgcd = myComParse.parse("MSG ok endcd");
revert(tempMsgcd);
} else if (lineparas.get(0).equals("..")) {
// String path = (String) newSession.getSession(user);
if (!currentpath.equals(prePath)) {
int spath = currentpath.lastIndexOf("/");
String stpath1 = currentpath.substring(0, spath);// 取出“/”前的内容
int spath1 = stpath1.lastIndexOf("/") + 1;
String stpath = currentpath.substring(0, spath1);//
String prepath = stpath;
if (prepath.length() < prePath.length()) {
myComParse = new CommandParser();
tempMsgcd = myComParse.parse("MSG fail endcdend");
revert(tempMsgcd);
} else {
newSession.setSession(sId, prepath);
// System.out.println("..path:" + newSession.getSession(sId));
myComParse = new CommandParser();
tempMsgcd = myComParse.parse("MSG ok endcd");
revert(tempMsgcd);
}
} else {
myComParse = new CommandParser();
tempMsgcd = myComParse.parse("MSG fail endcd");
revert(tempMsgcd);
}
} else {
String currentpath1 = currentpath + lineparas.get(0) + "/";
File f2 = new File(currentpath1);
currentpath = currentpath1;
if (f2.isDirectory()) {
newSession.setSession(sessionId, currentpath);
tempMsgcd = myComParse.parse("MSG ok endcdx");
revert(tempMsgcd);
} else {
tempMsgcd = myComParse.parse("MSG fail endcdx");
revert(tempMsgcd);
}
}
}
// 下载文件
private void get() {
myComParse = new CommandParser();
Message tempMsgget = null;
String filestr = null;
String filenameS = (String) lineparas.get(0);
// System.out.println("filenameS:" + filenameS);
String filenameC = (String) lineparas.get(1);
// System.out.println("filenameC:" + filenameC);
String sId = (String) lineparas.get(lineparas.size() - 1);
File fileget = new File(newSession.getSession(sId) + filenameS);
FileInputStream files;
try {
files = new FileInputStream(fileget);
byte[] fileb = new byte[(int) fileget.length()];
try {
files.read(fileb);
files.close();
System.out.println(fileget.length());
if (lineparas.size() < 3) {
filestr = "MSG " + filenameS + " " + filenameS;
} else {
filestr = "MSG " + filenameS + " " + filenameC;
}
tempMsgget = myComParse.parse(filestr);
// System.out.println("filestr: " + filestr);
tempMsgget.setBytedata(fileb);
// System.out.println(tempMsgget.getBytedata().length);
revert(tempMsgget);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 上传文件
private void put() {
myComParse = new CommandParser();
Message tempMsgput = null;
String sId = (String) lineparas.get(lineparas.size() - 1);
String wholefile = newSession.getSession(sId)
+ (String) lineparas.get(1);
try {
FileOutputStream filei = new FileOutputStream(wholefile);
byte[] fileb = gmsg.getBytedata();
filei.write(fileb);
filei.close();
if ((int) fileb.length != 0) {
System.out.println("数据上传成功,上传了" + fileb.length + "字节");
tempMsgput = myComParse.parse("MSG ok " + fileb.length
+ " endput");
revert(tempMsgput);
} else {
tempMsgput = myComParse.parse("MSG fail endput");
revert(tempMsgput);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void mkdir() {
String sId = (String) lineparas.get(lineparas.size() - 1);
String strm = newSession.getSession(sId) + lineparas.get(0);
File filem = new File(strm);
Message tempMsgdir = null;
if (filem.exists()) {
tempMsgdir = myComParse.parse("MSG fail enddir");
revert(tempMsgdir);
} else {
if (filem.mkdir()) {
tempMsgdir = myComParse.parse("MSG ok enddir");
revert(tempMsgdir);
} else {
tempMsgdir = myComParse.parse("MSG ok enddir");
revert(tempMsgdir);
}
}
}
public void run() {
while (true) {
System.out.println("*****************分享就是快乐********************");
gain();
lineCommand = gmsg.getCommand();
lineparas = new ArrayList();
lineparas = gmsg.getParas();
// System.out.println(lineparas);
// gmsg.getParas().clear();
// System.out.println(lineparas);
// String lineType = gmsg.getType();
// System.out.println("得到命令:" + lineCommand);
if (lineCommand.toUpperCase().equals("LOGIN")) {
loginSystem();
} else if (lineCommand.toUpperCase().equals("LS")) {
ls();
} else if (lineCommand.toUpperCase().equals("CD")) {
cd();
} else if (lineCommand.toUpperCase().equals("GET")) {
get();
} else if (lineCommand.toUpperCase().equals("PUT")) {
put();
} else if (lineCommand.toUpperCase().equals("LOGOUT")) {
logoutSystem();
} else if (lineCommand.toUpperCase().equals("MKDIR")) {
mkdir();
} else if (lineCommand.toUpperCase().equals("QUIT")||lineCommand.toUpperCase().equals("BYE")) {
try {
objSockIn.close();
objSockOut.close();
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}
}
发表于 @ 2007年03月29日 20:29:00|编辑