?? ftp-server.txt
字號(hào):
package cn.edu.bit.software.ftptrans;
import java.io.*;
import java.net.*;
import java.util.Vector;
import java.util.logging.*;
public class FtpServer
{
//客戶端socket對(duì)象
private ServerSocket m_servSocket;
//ftp服務(wù)器的端口號(hào)
private int SERVER_PORT;
//ftp服務(wù)器所允許的最大連接數(shù)
private int MAX_CONN;
//連入的客戶端處理對(duì)象管理器
private Vector vecClient;
//設(shè)定一個(gè)log日志對(duì)象
private Logger mylog;
private ConsoleHandler handler;
String strServHome;
public FtpServer(int servPort, int maxConn)
{
SERVER_PORT = servPort;
MAX_CONN = maxConn;
strServHome = "c:\\";
vecClient = new Vector();
/*------------初始化log------------*/
try
{
handler = new ConsoleHandler();
handler.setLevel(Level.ALL);
mylog = Logger.getLogger("FtpServer");
mylog.addHandler(handler);
mylog.setLevel(Level.ALL);
}
catch (SecurityException e)
{
mylog.warning("在設(shè)置程序日志級(jí)別時(shí)出現(xiàn)異常");
mylog.warning(e.getMessage());
}
/*--------------初始化服務(wù)器,端口2121----------------------*/
try
{
m_servSocket = new ServerSocket(SERVER_PORT, MAX_CONN);
while (true)
{
mylog.finest("FtpServer開始在端口2121監(jiān)聽");
Socket clientSocket = m_servSocket.accept();
vecClient.add(clientSocket);
mylog.info("#" + vecClient.size() + "號(hào)客戶端連入");
new TransHandler(this, clientSocket, vecClient.size()).start();
}
}
catch (IOException e)
{
mylog.warning("在初始化FtpServ時(shí)出現(xiàn)錯(cuò)誤");
mylog.warning(e.getMessage());
}
}
public void deleteClient(TransHandler handler)
{
try
{
vecClient.remove(handler);
vecClient.setSize(vecClient.size() - 1);
mylog.info("第#" + handler.iClientNum + "號(hào)客戶端斷開了與服務(wù)器的連接!");
}
catch (Exception e)
{
mylog.warning("在刪除第#" + handler.iClientNum + "號(hào)客戶端時(shí)出現(xiàn)異常");
mylog.warning(e.getMessage());
}
}
public static void main(String[] args)
{
new FtpServer(2121, 50);
}
}
/**
* 當(dāng)有客戶端連入時(shí),處理客戶端請(qǐng)求的類
* @author findfrog
* @version 1.0
*/
class TransHandler extends Thread
{
//服務(wù)器句柄,用于最后銷毀TransHandler對(duì)象時(shí)用
FtpServer main = null;
//客戶端的socket
private Socket m_clientSocket = null;
//日志對(duì)象
private Logger mylog;
//要上傳的文件路徑
private String strUpFilePath = null;
//要下載的文件路徑
private String strDnFilePath = null;
//本客戶端在的序號(hào)
int iClientNum = -1;
//緩沖字節(jié)數(shù)據(jù)的大小
private int ibufferlength;
//緩沖字節(jié)數(shù)組
byte[] inputBytes;
//從客戶端傳來的指令
String strClientOrder;
//用于得到從socket端的輸入信息
InputStream m_inputStream;
//用于向socket輸出的輸出流
OutputStream m_outputStream;
//用于上傳文件的輸出流
FileOutputStream m_fileOutputStream;
//用于下載文件的輸入流
FileInputStream m_fileInputStream;
//構(gòu)造函數(shù)
public TransHandler(FtpServer fserver, Socket s, int iNum)
{
try
{
main = fserver;
//將客戶端socket句柄付給本地對(duì)象
m_clientSocket = s;
//初始化log對(duì)象
mylog = Logger.getLogger("TransHandler");
//初始化本客戶端序號(hào)
iClientNum = iNum;
//用于得到從socket端的輸入信息
m_inputStream = m_clientSocket.getInputStream();
m_outputStream = m_clientSocket.getOutputStream();
ibufferlength = 1024;
inputBytes = new byte[ibufferlength + 12];
}
catch (Exception e)
{
mylog.warning("在初始化TransHandler時(shí)發(fā)生異常!");
mylog.warning(e.getMessage());
}
}
public void run()
{
try
{
int ilength;
while ( (ilength = m_inputStream.read(inputBytes, 0, 12 + ibufferlength)) !=
-1)
{
strClientOrder = new String(inputBytes, 0, 7);
if (strClientOrder.equals("DISCONN"))
{ //斷開連接
mylog.info("得到了DISCONN");
exit();
}
else if (strClientOrder.equals("LSFILES"))
{ //發(fā)送當(dāng)前目錄文件列表
mylog.info("服務(wù)器端接收到了LSFILES命令");
File flHome = new File(main.strServHome);
String[] strFileNames = flHome.list();
strFileNames = AdjustStrings(strFileNames);
for (int i = 0; i < strFileNames.length; i++)
{
String strFileNameLength = PublicFunc.formatLength(strFileNames[i].
getBytes().length);
byte[] fileNameBytes = strFileNames[i].getBytes();
byte[] outBytes = PublicFunc.makepackage("LSFILES",
strFileNameLength, fileNameBytes);
m_outputStream.write(outBytes, 0, outBytes.length);
m_outputStream.flush();
}
}
else if (strClientOrder.equals("ENDFILE"))
{ //上傳一個(gè)文件的結(jié)束標(biāo)記
mylog.info("收到文件結(jié)束標(biāo)志符號(hào)");
m_fileOutputStream.close();
}
else if (strClientOrder.equals("UPFILEN"))
{ //表示要上傳一個(gè)新的文件,并且此包中包含了文件名
int iFileNameLength = Integer.parseInt(new String(inputBytes, 7, 5));
mylog.info("要上傳的文件名的長(zhǎng)度為" + iFileNameLength);
String strFileName = new String(inputBytes, 12, iFileNameLength);
mylog.info("要上傳的文件名是:" + strFileName);
//初始化上傳文件路徑
strUpFilePath = main.strServHome + strFileName;
File upFile = new File(strUpFilePath);
m_fileOutputStream = new FileOutputStream(upFile);
}
else if (strClientOrder.equals("UPDATAS"))
{ //表示本包是要上傳的數(shù)據(jù)
//本次數(shù)據(jù)包的長(zhǎng)度
mylog.info("正在接收文件...");
int iDataLength = Integer.parseInt(new String(inputBytes, 7, 5));
m_fileOutputStream.write(inputBytes, 12, iDataLength);
m_fileOutputStream.flush();
}
else if (strClientOrder.equals("DNFILEN"))
{ //表示要下載的文件名,服務(wù)器要執(zhí)行向客戶端傳輸文件的操作
int iFileNameLength = Integer.parseInt(new String(inputBytes, 7, 5));
mylog.info("要下載的文件名的長(zhǎng)度為" + iFileNameLength);
String strFileName = new String(inputBytes, 12, iFileNameLength);
mylog.info("要下載的文件名是:" + strFileName);
//初始化上傳文件路徑
strDnFilePath = main.strServHome + strFileName;
File dnFile = new File(strDnFilePath);
//初始化了文件輸出流
m_fileInputStream = new FileInputStream(dnFile);
//開始向客戶端傳輸文件
mylog.info("開始向客戶端傳輸文件" + strFileName + "...");
int iInputLength = 0;
String strInputLength;
byte[] readBytes = new byte[ibufferlength];
while ( (iInputLength = m_fileInputStream.read(readBytes, 0,
ibufferlength)) !=
-1)
{
strInputLength = PublicFunc.formatLength(iInputLength);
byte[] outBytes = PublicFunc.makepackage("DNDATAS", strInputLength,
readBytes);
m_outputStream.write(outBytes, 0, outBytes.length);
m_outputStream.flush();
}
//最后發(fā)送一個(gè)文件結(jié)束標(biāo)記
m_outputStream.write(PublicFunc.makepackage("ENDFILE", "00001",
new byte[1]));
m_outputStream.flush();
}
}
}
catch (Exception e)
{
mylog.warning(e.getMessage());
}
}
public void exit()
{
try
{
m_outputStream.write(PublicFunc.makepackage("DISCONN", "00001",
new byte[1]));
m_inputStream.close();
m_outputStream.close();
main.deleteClient(this);
main = null;
}
catch (Exception e)
{
mylog.warning("在斷開客戶端#" + this.iClientNum + "連接時(shí)出現(xiàn)異常!");
mylog.warning(e.getMessage());
}
}
public String[] AdjustStrings(String[] strFileNames)
{
String[] strItemNames = new String[strFileNames.length + 1];
strItemNames[0] = "返回上一級(jí)";
int j = 1;
for (int i = 0; i < strFileNames.length; i++)
{
File upFile = new File(main.strServHome + strFileNames[i]);
if (!upFile.isFile())
{
strItemNames[j++] = "[文件夾]" + strFileNames[i];
}
}
for (int i = 0; i < strFileNames.length; i++)
{
File upFile = new File(main.strServHome + strFileNames[i]);
if (upFile.isFile())
{
strItemNames[j++] = strFileNames[i];
}
}
return strItemNames;
}
}
package cn.edu.bit.software.ftptrans;
import java.io.*;
import java.net.*;
import java.util.logging.*;
import java.util.Vector;
public class FtpClient extends Thread
{
Logger mylog = Logger.getLogger("FtpClient");
Socket m_clientSocket;
//緩沖字節(jié)數(shù)據(jù)的大小
private int ibufferlength;
//緩沖字節(jié)數(shù)組
byte[] inputBytes;
Vector vecServFiles;
//用于得到從socket端的輸入信息
InputStream m_inputStream;
//用于向socket輸出的輸出流
OutputStream m_outputStream;
//向本地寫文件的文件輸出流
FileOutputStream m_fileOutputStream;
//從本地讀文件的文件輸入流
FileInputStream m_fileInputStream;
//從服務(wù)器端傳來的指令
String strServerOrder;
//主機(jī)的ip地址
String strServerIP;
//服務(wù)器的端口號(hào)
int iServerPort;
public FtpClient(String strServIP, int iServPort)
{
strServerIP = strServIP;
iServerPort = iServPort;
}
public void run()
{
try
{
//建立連接
m_clientSocket = new Socket(strServerIP, iServerPort);
mylog.info("已經(jīng)連到了主機(jī)" + strServerIP + "在端口" + iServerPort);
m_inputStream = m_clientSocket.getInputStream();
m_outputStream = m_clientSocket.getOutputStream();
mylog.fine("客戶端得到了socket的輸入輸出流!");
ibufferlength = 1024;
inputBytes = new byte[ibufferlength + 12];
vecServFiles = new Vector();
doBusiness();
}
catch (UnknownHostException e)
{
mylog.warning("服務(wù)器地址未知");
mylog.warning(e.getMessage());
}
catch (IOException e)
{
mylog.warning(e.getMessage());
}
catch (Exception e)
{
mylog.warning(e.getMessage());
}
}
public void doBusiness()
{
try
{
int iLength = 0;
while ( (iLength = m_inputStream.read(inputBytes, 0, ibufferlength + 12)) !=
-1)
{
strServerOrder = new String(inputBytes, 0, 7);
if (strServerOrder.equals("DISCONN"))
{ //斷開連接
mylog.info("在client端得到了DISCONN");
int length = Integer.parseInt(new String(inputBytes, 7, 5));
mylog.info("長(zhǎng)度是" + length);
}
else if (strServerOrder.equals("LSFILES"))
{ //接收服務(wù)器當(dāng)前目錄文件列表
int iDataLength = Integer.parseInt(new String(inputBytes, 7, 5));
mylog.info("在客戶端這個(gè)文件名的長(zhǎng)度是:" + iDataLength);
String strFileName = new String(inputBytes, 12, iDataLength);
mylog.info("客戶端正在獲取服務(wù)器目錄信息....." + strFileName);
vecServFiles.add(strFileName);
}
else if (strServerOrder.equals("ENDFILE"))
{ //下載一個(gè)文件的結(jié)束標(biāo)記
mylog.info("收到下載文件結(jié)束標(biāo)志符號(hào)");
m_fileOutputStream.close();
}
else if (strServerOrder.equals("DNDATAS"))
{ //表示本包是要下載的數(shù)據(jù)
int iDataLength = Integer.parseInt(new String(inputBytes, 7, 5));
m_fileOutputStream.write(inputBytes, 12, iDataLength);
m_fileOutputStream.flush();
}
}
}
catch (Exception e)
{
}
}
/**
* 客戶端上傳文件名
* @param strFileName 要上傳文件的文件名
*/
public void upFileName(String strFileName)
{
try
{
String strLength = PublicFunc.formatLength(strFileName.getBytes().length);
byte[] outBytes = PublicFunc.makepackage("UPFILEN", strLength,
strFileName.getBytes());
m_outputStream.write(outBytes, 0, outBytes.length);
m_outputStream.flush();
}
catch (Exception e)
{
mylog.warning("在客戶端在向服務(wù)器寫要上傳的文件名時(shí)發(fā)生異常");
mylog.warning(e.getMessage());
}
}
/**
* 講本地文件strFilePath上傳到服務(wù)器
* @param strFilePath 本地文件路徑
*/
public void upFileData(String strFilePath)
{
try
{
File file = new File(strFilePath);
m_fileInputStream = new FileInputStream(file);
int iInputLength = 0;
String strInputLength;
byte[] readBytes = new byte[ibufferlength];
while ( (iInputLength = m_fileInputStream.read(readBytes, 0,
ibufferlength)) !=
-1)
{
strInputLength = PublicFunc.formatLength(iInputLength);
byte[] outBytes = PublicFunc.makepackage("UPDATAS", strInputLength,
readBytes);
m_outputStream.write(outBytes, 0, outBytes.length);
m_outputStream.flush();
}
//最后發(fā)送一個(gè)文件結(jié)束標(biāo)記
m_outputStream.write(PublicFunc.makepackage("ENDFILE", "00001",
new byte[1]));
m_outputStream.flush();
}
catch (Exception e)
{
mylog.warning("從客戶端向服務(wù)器傳輸文件內(nèi)容是發(fā)生異常");
mylog.warning(e.getMessage());
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -