免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
论坛 程序设计 Java 多少
最近访问板块 发新帖
查看: 1042 | 回复: 0
打印 上一主题 下一主题

多少 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-05-22 17:14 |只看该作者 |倒序浏览
import java.net.ServerSocket;
import java.net.Socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Date;

public class WebServer{
public static void main(String args[]){
try{
String ls_url=new String("");
String request_method=new String("");
int ll_pos;
ServerSocket server=new ServerSocket(8080);
while (true){
Socket soc=server.accept();
OutputStream dos=soc.getOutputStream();
InputStream is=soc.getInputStream();
DataInputStream dis=new DataInputStream(is);
ls_url=dis.readLine();
System.out.println("Request Finished..........");
ll_pos=ls_url.indexOf(" ");
request_method=ls_url.substring(0,ll_pos);
ls_url=ls_url.substring(ll_pos+1,ls_url.length());
ll_pos=ls_url.indexOf(" ");
ls_url=ls_url.substring(0,ll_pos);
String request_file=ls_url.substring(1,ls_url.length());
System.out.println("request uri.."+request_file);
if (!request_file.startsWith("_vti"))
{
if (!request_method.equals("OPTIONS"))
{
File fil=new File(request_file);
FileInputStream fis=new FileInputStream(fil);
DataInputStream dfis=new DataInputStream(fis);
StringBuffer file_stream=new StringBuffer("");
byte []data=new byte[1024];
int count;
if (request_file.toLowerCase().endsWith(".doc"))
{
StringBuffer hearder=new StringBuffer("HTTP/1.1 200 OK\r\n");
hearder.append("Date:"+new Date().toString()+"\r\n");
hearder.append("Content-Type:application/msword\r\n");
//hearder.append("Content-Encoding: x-compress\r\n");
//hearder.append("\r\n");
dos.write(new String(hearder).getBytes());
System.out.println("Hearder Information:\r\n");
System.out.println(hearder);
}
else{
StringBuffer hearder=new StringBuffer("HTTP/1.1 200 OK\r\n");
hearder.append("Date: "+new Date().toString()+"\r\n");
hearder.append("Content-Type: text/html\r\n");
hearder.append("\r\n");
dos.write(new String(hearder).getBytes());
System.out.println("Hearder Information:\r\n");
System.out.println(hearder);
}
while ((count=dfis.read(data)) != -1){
//System.out.print(new String(data));
dos.write(data,0,count);
}

}
else{
StringBuffer ls_options=new StringBuffer("HTTP/1.1 200 OK\r\n");
ls_options.append("Allow:HEAD,GET,POST,TRACE,OPTIONS\r\n");
dos.write(new String(ls_options).getBytes());
}
}
else{
StringBuffer hearder=new StringBuffer("HTTP/1.1 404 Not Found\r\n");
hearder.append("Date:"+new Date().toString()+"\r\n");
dos.write(new String(hearder).getBytes());
}
dos.close();

}
}
catch(Exception e){System.out.println(e);}

}
}

[ 本帖最后由 vidarz 于 2006-7-21 10:13 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP