📄 multiserver.java
字号:
import java.io.*;
import java.net.*;
import java.util.*;
/* This Class is the listener class of the application. This class handles the request from clients
* the class extends Thread class because any new request runs in a new thread .
*/
class OneServer extends Thread {
// Make object for client socket
private Socket socket;
// Make object for tokenizer
private StringTokenizer st_xml;
// Make object for parser
static XMLParserServer sp;
private BufferedOutputStream data_write;
// Return files and directroy names.Return the file size and rights of the files
String[][] returnvalueoffiles;
// Vectro Declared
static Vector v_file_name;
//This class is used to write xmldata
private XmlWriter xmlwriter;
private check_directory check;
//Declearing BufferedInputStream
private BufferedInputStream in;
// Declearing BufferedOutputStream
private BufferedOutputStream out;
// This String will contain the FileName
String left_half;
// Store right part of the file it means after "."
String right_half;
// Store detail path name of file & directies in file object
File path_file;
// Store files and directries names in string array
String[] files;
/* when search any word if any charecter present of left of file extension then in that
* condition this flag become true */
boolean left_half_flag = false;
/* when search any word if any charecter present of right of file extension then in that
* condition this flag become true */
boolean right_half_flag = false;
// file and directroy store in this variable
String filename;
// file size store in this variable
String filesize;
// file rigts store in this variable
String filemask;
String stemp="";
/* from this fuction listener take request from client and according to there
* request listener give the response */
public OneServer(Socket soc) throws IOException
{
// store the socket value which is connected by user
socket = soc;
// take the request from the user in the form of BufferInputStream
in = new BufferedInputStream (socket.getInputStream());
// give the response to the user from listener in form of BufferOutputStream
out = new BufferedOutputStream(socket.getOutputStream());
// start the new thread for new user
start();
}
// when any new user connect then this fuction call by start()
public void run()
{
try
{
int i = 0;
String value;
byte [] a = new byte[1024];
// open main.xml file as BufferedOutputStream for writting data into it
data_write = new BufferedOutputStream(
(new FileOutputStream("main.xml")));
// read data from user DataInputstream
in.read(a,0,1024);
// Store reading value in temp String
String temp = new String(a);
System.out.println(temp);
// Break the temp till last ">" + 1 value
temp = temp.substring(0,(temp.lastIndexOf(">")+1));
// convert temp string value in Byte
byte d[] = temp.getBytes();
// write converted value in "main.xml" file
data_write.write(d, 0, d.length);
// Close the date_write stream
data_write.close();
// Make the object of XmlWriter class
xmlwriter = new XmlWriter();
// Make the object of XMLParserServer class
sp = new XMLParserServer();
// call the function of class XMLParserServer and pass the argument as string
sp.perform("main.xml");
// This function return the file names as vector
v_file_name = sp.yakreturn();
// This for loop run the size of the vector
for(int t = 0; t < v_file_name.size(); t++)
{
// vector value store is showfiles type then enter in this condition
if((v_file_name.get(t).toString()).equalsIgnoreCase("SHOWFILES"))
{
String s = "";
// open stream of "Share.ini" file for reading
BufferedReader data_read = new
BufferedReader(new FileReader("Share.ini"));
// This while loop run till stream "data_read" not become null
while ((s = data_read.readLine()) != null)
{
String filename = "";
String filesize = "";
String mask = "";
// divide string "s" on the basis of "=" and store in st_xml
st_xml = new StringTokenizer(s, "=");
// This while loop run till that tokenizer present in st_xml
while(st_xml.hasMoreTokens())
{
// Here store first token in variable filename
filename = st_xml.nextToken();
/* This filename string end with "\" then enter in this part otherwise enter
* in else part. if filename string end with "\" it means that is directroy there
* oterwise that is file */
if( !filename.endsWith("\\") )
{
// Here store second token in variable mask. it is rights of file
mask = st_xml.nextToken();
// Here store thired token in variable filesize. it is writes of file
filesize = st_xml.nextToken();
/* Call the returnHeader function from XmlWriter class this fuction return
* the header of xml file as string and store this value in stemp variable. */
stemp = xmlwriter.returnHeader(v_file_name.get(t).toString());
/* Call the responseFString function from XmlWriter class this fuction
* writes the xml file for files. */
xmlwriter.responseFString(v_file_name.get(t).toString(), filename, filesize, mask);
}
else
{
// Here store second token in variable mask. it is rights of file
mask = st_xml.nextToken();
/* Call the returnHeader function from XmlWriter class this fuction return
* the header of xml file as string and store this value in stemp variable. */
stemp = xmlwriter.returnHeader(v_file_name.get(t).toString());
/* Call the responseFString function from XmlWriter class this fuction
* writes the xml file for Directroy. */
xmlwriter.responseFString(v_file_name.get(t).toString(), filename, "", mask);
}
}
}
/* Call the returnResponse function this function return whole xml except header of xml as
* string.Store this value in wholexmlwithoutheader veriable */
String wholexmlwithoutheader = xmlwriter.returnResponse();
/* Add two string veriable and store in any third string variable. This variable store whole
* xml file */
wholexmlwithoutheader = stemp+wholexmlwithoutheader;
System.out.println(wholexmlwithoutheader);
// Find the length of xml file and send 0 to length of file xml file bytes to user
out.write(wholexmlwithoutheader.getBytes(),0,wholexmlwithoutheader.length());
// Close the data_read stream which read from file.
data_read.close();
// Close the out stream which connected to user.
out.close();
}
// In this condition we do all download work related to user request
else if((v_file_name.get(t).toString()).equalsIgnoreCase("DOWNLOAD"))
{
// Store the file name in veriable f_name. This file down loaded by user
String f_name = v_file_name.get(1).toString();
// initilize the veriable of len
int len = 0;
// Open the file stream of stored file name which is present in f_name.
FileInputStream fstream = new FileInputStream(f_name);
// Make variable c_write as Byte array which send to user
byte[] c_write = new byte[32];
// While loop run upto 32 Byte of all stored arrey value
while ((len = fstream.read(c_write,0,32))>0)
{
// Send the out stream to user every 32 Byte
out.write(c_write, 0, len);
}
// Close the out Stream
out.close();
}
// In this condition we do all search work related to user request
else if((v_file_name.get(t).toString()).equalsIgnoreCase("SEARCH"))
{
/* Make the object of check_directory which search file and diretory
* which is requested by the user */
check = new check_directory();
// Store file & directory name with path in whole_String variable
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -