Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
0 Star 0 Fork 1

RunCoder/HttpServer

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
master
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
The license selected for the repository is subject to the license used by the main branch of the repository.
master
Branches (1)
master
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
master
HttpServer
/
src
/
com
/
httpserver
/
two
/
HttpServer.java
HttpServer
/
src
/
com
/
httpserver
/
two
/
HttpServer.java
HttpServer.java 3.30 KB
Copy Edit Raw Blame History
RunCoder authored 2018年06月06日 15:23 +08:00 . 1. 代码格式调整, 2.增加示例页面 index.html
package com.httpserver.two;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
/**
* @description: 使用socket 实现 web服务器 --- 具体执行读写操作的。
* @version:v1.0
* @author:w
* @date:2018年6月6日上午11:06:20
*
*/
public class HttpServer extends Thread {
/**
* web资源根路径
*/
public static final String ROOT = "c:/";
/**
* 输入流对象,读取浏览器请求
*/
private InputStream input;
/**
* 输出流对象,响应内容给浏览器
*/
private OutputStream out;
/**
* @description:初始化socket对象,获取对应 输入,输出流
* @param socket
*/
public HttpServer(Socket socket) {
try {
input = socket.getInputStream();
out = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 多线程方法调用
*/
@Override
public void run() {
String filePath = read();
response(filePath);
}
/**
* @description: 读取资源文件,响应给浏览器。
* @param:@param filePath
* 资源文件路径
* @return:void
* @version:v1.0
* @author:w
* @date:2018年6月6日 上午11:42:37
*
*/
private void response(String filePath) {
File file = new File(ROOT + filePath);
if (file.exists()) {
// 1、资源存在,读取资源
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuffer sb = new StringBuffer();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\r\n");
}
StringBuffer result = new StringBuffer();
result.append("HTTP /1.1 200 ok /r/n");
// result.append("Content-Type:text/html /r/n");
result.append("Content-Length:" + file.length() + "/r/n");
result.append("\r\n:" + sb.toString());
out.write(result.toString().getBytes());
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
} else {
// 2、资源不存在,提示 file not found
StringBuffer error = new StringBuffer();
error.append("HTTP /1.1 400 file not found /r/n");
error.append("Content-Type:text/html \r\n");
error.append("Content-Length:20 \r\n").append("\r\n");
error.append("<h1 >File Not Found..</h1>");
try {
out.write(error.toString().getBytes());
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
*
* @description:解析资源文件路径
* @example: GET /index.html HTTP/1.1
* @param:@return
* @return:String
* @version:v1.0
* @author:w
* @date:2018年6月6日 上午11:39:42
*
*/
private String read() {
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
try {
// 读取请求头, 如:GET /index.html HTTP/1.1
String readLine = reader.readLine();
String[] split = readLine.split(" ");
if (split.length != 3) {
return null;
}
System.out.println(readLine);
return split[1];
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/RunCoder/HttpServer.git
git@gitee.com:RunCoder/HttpServer.git
RunCoder
HttpServer
HttpServer
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /