import java.io.*;
import java.net.*;
public class ConnectionThread extends Thread {// ConnectionThread类完成一个Web浏览器的通信
Socket client; // 连接Web浏览器的socket套接字
int counter; // 计数器
public ConnectionThread(Socket cl, int c) {
client = cl;
counter = c;
}
@Override
public void run() {
try {
String destIP = client.getInetAddress().toString(); // 客户端IP地址
int destport = client.getLocalPort(); // 客户端端口号
System.out.println("Connect " + counter + ":connected to " + destIP
+ " on port " + destport + ".");
PrintStream outstream = new PrintStream(client.getOutputStream());
BufferedReader instream = new BufferedReader(new InputStreamReader(client
.getInputStream()));
String inline = instream.readLine(); // 读取Web浏览器提交的请求信息
System.out.println("Received:" + inline);
if (getrequest(inline)) {// 如果是GET请求
String filename = getfilename(inline);
File file = new File(filename);
if (file.exists()) {// 若文件存在,则将文件送个Web浏览器
System.out.println(filename + " requested.");
outstream.println("HTTP/1.0 200 OK");
outstream.println("MIME_version:1.0");
outstream.println("Content_Type:text/html");
int len = (int) file.length();
outstream.println("Content_Length:" + len);
outstream.println(" ");
sendfile(outstream, file); // 发送文件
outstream.flush();
} else {// 文件不存在时
String notfound = "<html><head><title>Not Found</title></head>"
+ "<body><h1>Error 404-file not found</h1></body></html>";
outstream.println("HTTP/1.0 404 Not Found");
outstream.println("Content_Type:text/html");
outstream
.println("Content_Length:" + notfound.length() + 2);
outstream.println(" ");
outstream.println(notfound);
outstream.flush();
}
}
long m1 = 1;
while (m1 < 11100000) {// 延时
m1++;
client.close();
}
} catch (IOException e) {
System.out.println("Exception:" + e);
}
}
private void sendfile(PrintStream outs, File file) {
DataInputStream in;
try {
in = new DataInputStream(new FileInputStream(file));
int len = (int) file.length();
byte buf[] = new byte[len];
in.readFully(buf);
outs.write(buf, 0, len);
outs.flush();
in.close();
} catch (Exception e) {
System.out.println("Error retrieving file.");
System.out.println(1);
}
}
private String getfilename(String s) {
String f = s.substring(s.indexOf(' ') + 1);
f = f.substring(0, f.indexOf(' '));
try {
if (f.charAt(0) == '/')
f = f.substring(1);
} catch (StringIndexOutOfBoundsException e) {
System.out.println("Exception:" + e);
}
if (f.equals(""))
f = "index.html";
return f;
}
// 获取请求类型那个是否为”GET“
private boolean getrequest(String s) {
if (s.length() > 0) {
if (s.substring(0, 3).equalsIgnoreCase("GET"))
return true;
}
return false;
}
}
Http.rar_HTTP_http get
版权申诉
188 浏览量
2022-09-20
17:12:53
上传
评论
收藏 2KB RAR 举报
alvarocfc
- 粉丝: 111
- 资源: 1万+