用java写的服务器
来源:互联网 发布:p2psearcher软件下载 编辑:程序博客网 时间:2024/06/01 09:45
用java写的服务器
用java写了一个服务器,其中有Request请求类、ProcessFile处理类、Response响应类、HttpServer服务端、 还写了个main方法程序入口,代码分享如下:
HttpServer服务端
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class HttpServer {
ServerSocket serverSocket=null;
private static int port=8000;
public HttpServer(){
System.out.println("coming--HttpServer");
try {
serverSocket = new ServerSocket(port);
System.out.println("serverSocket--->"+serverSocket);
while(true){
System.out.println("loop");
Socket socket=serverSocket.accept();
System.out.println("socket--->"+socket);
Request request = new Request(socket,socket.getInputStream());
socket.close();
}
} catch (IOException e) {
System.out.println("error!"+e.getMessage());
}
}
}
Request请求类
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
public class Request {
private static InputStream inputStream=null;
private static Socket socket=null;
public Request(Socket socket, InputStream getInputStream) throws IOException {
this.inputStream=getInputStream;
this.socket=socket;
System.out.println("coming---Request()");
getURL();
}
public void getURL() throws IOException {
System.out.println("coming------getURL");
String url=null;
try {
System.out.println("coming try 003");
BufferedReader in=new BufferedReader(new InputStreamReader(inputStream));
System.out.println("in-->"+in);
String line=in.readLine();
System.out.println("line------>"+line);
url=line.substring(line.indexOf("/")+1,line.lastIndexOf("/")-5);
System.out.println("url---->"+url);
} catch (IOException e) {
System.out.println("获取文件资源失败!"+e.getMessage());
}
if(url != null){
ProcessFile pf=new ProcessFile(url,socket);
}
}
}
ProcessFile处理类
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
public class ProcessFile {
private static String url=null;
private static Socket socket=null;
public ProcessFile(String url, Socket socket) throws IOException {
this.url=url;
this.socket=socket;
getOutContent();
}
public void getOutContent() throws IOException {
File file=new File(url);
System.out.println("file----->"+url);
FileInputStream fileStream=new FileInputStream(file.getAbsolutePath());
System.out.println("fileStream-->"+fileStream);
byte[] data=new byte[fileStream.available()];
System.out.println("data----------------->"+data.length);
fileStream.read(data);
String datas=new String(data);
if(datas != null){
Response response=new Response(datas,socket);
}
fileStream.close();
}
}
Response响应类
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
public class Response {
private static String datas=null;
private static Socket socket=null;
public Response(String datas, Socket socket) throws IOException {
System.out.println("coming ---- Response() 0005");
this.datas=datas;
this.socket=socket;
outHTML();
}
public void outHTML() throws IOException {
System.out.println("comging outHTML() 44455");
new PrintWriter(socket.getOutputStream(),true).println(datas);
}
}
main方法程序入口
public class TestHttpServer {
/**
* @param args
*/
public static void main(String[] args) {
new HttpServer();
}
}
- 用java写的服务器
- JAVA写服务器程序代码
- 一个用JAVA写的测算服务器响应速度的程序
- 用java 写的一个简单的 web 服务器(适合新手练练)
- C++写的MFC调用java服务器的Action
- 用libevent 写高性能的服务器
- 用java写简单的web服务器,在宿舍提供视频播放
- 网友写的JAVA版HTTP服务器程序
- 如何用Java写一个简单的代理Web服务器
- java造轮子之自己写简单的HTTP服务器
- Java写简单HTTP服务器
- C++写的服务器与Java写的客户端通过Socket通信(源代码)
- 用java写的计算器
- 用Java写的计算器
- 用java写的计算器
- 用java写的TrieTree
- [epoll服务器]第一次写服务器的总结
- C写的http服务器
- 使用SQL*Loader将CSV文件数据导入ORACLE数据表
- FileLib.PRG -- VFP 文件基础函数库
- 将Android的示例(samples)导入模拟器并运行
- java中的对象等价性讨论
- EBS Form个性化的工作原理
- 用java写的服务器
- 段错误(segmentation fault)
- 杭电46道DP牛人总结
- FtpLib.PRG -- VFP FTP 通信函数库
- 图像压缩、生成缩略图类
- cocos2d-x 2D砖块地图实现技术之四
- oracle 分页
- poi合并单元格同时导出excel
- EXTJS 给Tree设置字体