📄 proxy.java
字号:
import java.io.*;
import java.net.*;
import java.util.*;
/**
* @author yhy
* @version 1.0.0
* The Proxy Server Class
*
*/
public class Proxy extends Thread {
static final int PORT = 8000;
public static void main(String[] args) throws IOException {
ServerSocket welcom_socket = new ServerSocket(PORT);
System.out.println("ThreadedServer has started !\r\n" + welcom_socket);
try {
while (true) {
Socket connect_socket = welcom_socket.accept();
try {
new Proxy(connect_socket);
} catch (IOException e) {
connect_socket.close();
}
}
} finally {
welcom_socket.close();
}
}
private Socket socket;
private BufferedReader in;
private DataOutputStream out;
public Proxy(Socket connect_socket) throws IOException {
socket = connect_socket;
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new DataOutputStream(socket.getOutputStream());
start();
}
public void run() {
try {
String command = "";
String str = in.readLine();
String uri = "";
int port = 80;
System.out.println("Echoing: " + str);
StringTokenizer tokenizer = new StringTokenizer(str);
if (tokenizer.hasMoreTokens()
&& tokenizer.nextToken().equals("GET")) {
command = tokenizer.nextToken();
StringTokenizer uritokenizer = new StringTokenizer(command
.substring(7), "/");
if (uritokenizer.hasMoreTokens()) {
uri = uritokenizer.nextToken();
StringTokenizer porttokenizer = new StringTokenizer(uri,
":");
if (porttokenizer.hasMoreTokens()
&& porttokenizer.countTokens() > 1) {
uri = porttokenizer.nextToken();
port = Integer.parseInt(porttokenizer.nextToken());
}
} else {
System.out.println("Bad Command!");
System.exit(0);
}
} else {
System.out.println("Bad Command!");
System.exit(0);
}
Socket Clientsocket;
DataInputStream Clientin;
PrintWriter Clientout;
BufferedReader inClient;
DataOutputStream outClient;
try {
Clientsocket = new Socket(uri, port);
if (Clientsocket.isConnected())
System.out.println(uri
+ " is listening to your request:\r\n");
Clientout = new PrintWriter(Clientsocket.getOutputStream(),
true);
Clientin = new DataInputStream(Clientsocket.getInputStream());
Clientout.println(str + "\r\n\r\n");
System.out.println("\r\nHEADER:\r\n");
StringTokenizer currenttoken;
do {
String currentLine = Clientin.readLine();
if (currentLine == null) {
System.out
.println("Maybe the file doesn't exsit!\r\n"
+ "Please restart and enter the correct command!");
System.exit(0);
//break;
}
currenttoken = new StringTokenizer(currentLine);
System.out.println(currentLine);
out.writeBytes(currentLine + "\r\n");
} while (currenttoken.hasMoreTokens());
String fileName = "index";
File file = new File(fileName);
if (file.exists())
file.delete();
file.createNewFile();
RandomAccessFile raf = new RandomAccessFile(file, "rw");
byte[] buf = new byte[1024];
int num = Clientin.read(buf, 0, 1024);
while (num != (-1)) {//是否读完所有数据
raf.write(buf, 0, num);//将数据写往文件
raf.skipBytes(num);//顺序写文件字节
num = Clientin.read(buf);//继续从网络中读取文件
}
System.out.println("complete!!!");
Clientin.close();
//in.close();
raf.close();
} catch (Exception e) {
System.out.println("failure!!!");
}
File file = new File("hao");
int numOfBytes = (int) file.length();
FileInputStream inFile = new FileInputStream("hao");
byte[] fileOutBytes = new byte[numOfBytes];
inFile.read(fileOutBytes);
out.write(fileOutBytes, 0, numOfBytes);
} catch (IOException e) {
System.out.println(e.toString());
System.out.println(e.getMessage());
} finally {
try {
socket.close();
} catch (IOException e) {
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -