📄 threadedserver.java
字号:
/**
* @date 2009/4/6
* @author ***
* @version 1.0.0
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.net.*;
import java.util.StringTokenizer;
import java.io.*;
public class ThreadedServer {
private static String serverRoot = null;
/**
* Write a server that creates a socket with port number 8000 and waits for
* incoming requests.
*
* @param args
*/
public static void main(String[] args) throws IOException {
ServerSocket server = null;
Socket client = null;
BufferedReader string1 = new BufferedReader(new InputStreamReader(
System.in));
try {
//create a server
server = new ServerSocket(8000);
System.out.println("The Server is start!");
System.out.println("Please input the server's root (such as C://)");
serverRoot = string1.readLine();
while (true) {
//listening the client all the time
client = server.accept();
//take the mission to the thread
new Handler(client, serverRoot);
}
} catch (Exception e) {
System.out.println("Error:" + e);
} finally {
//close the server,the client,the string1
server.close();
client.close();
string1.close();
}
}
}
/**
* This class handles request.
*/
class Handler implements Runnable {
private Socket client;
private String serverRoot;
public Handler(Socket s, String path) {
Thread thread;
this.client = s;
this.serverRoot = path;
//create a new thread;
thread = new Thread(this, "Handler Thread");
thread.start();
}
/**
* Handles the client's requests
*/
public void run() {
try {
// inFromServer is used to get information from the client
BufferedReader inFromServer = new BufferedReader(
new InputStreamReader(client.getInputStream()));
// out is used to send out information to the client
OutputStream out = client.getOutputStream();
String requset = inFromServer.readLine();
System.out.println("The command of the client:" + requset);
// feedback the command of the client.
while (requset != null) {
if (requset.length() < 3)
break;
String str1 = requset.substring(0, 3);
if (str1.equalsIgnoreCase("GET")) {
String filename = requset.substring(5, requset
.lastIndexOf(" "));
String filePath = serverRoot + filename; // the full path // of the file
System.out.println(filePath);
File file = new File(filePath);
if (!file.exists()) {
System.out.println("The file is not exist");
break;
}
// The server sent the data of file to the client, for example:IE
InputStream in = new FileInputStream(file);
byte[] buf = new byte[1024];
int n = 0;
while ((n = in.read(buf)) != -1) {
out.write(buf, 0, n);
out.flush();
}
in.close();
} else if (str1.equalsIgnoreCase("PUT")) {
// analyse the data,looking for the "PUT" and filepath
String filename1 = requset.substring(5, requset
.lastIndexOf(" "));
// the full path of the soure file
String filePath1 = serverRoot + filename1;
// get the name of file
StringTokenizer st = new StringTokenizer(filename1, "/",
true);
String name = null;
while (true) {
if (st.hasMoreTokens() == false) {
name = st.nextToken();
break;
}
}
// the full path of the destination
String filePath2 = serverRoot + name;
System.out.println(filePath1);
File file = new File(filePath1);
File file1 = new File(filePath2);
if (!file.exists()) {
System.out.println("The file is not exist");
break;
}
InputStream in1 = new FileInputStream(file);
OutputStream out1 = new FileOutputStream(file1);
System.out.println("Put" + filePath1 + "to" + filePath2);
int data = in1.read();
while (data != -1) {
out1.write(data);
data = in1.read();
}
in1.close();
out1.close();
}
requset = inFromServer.readLine();
}
//close the out and client.
out.close();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -