当前位置:   article > 正文

Java:从零开始创建的一个开源的Web服务器_java文件服务器开源

java文件服务器开源

写在前面:最近在学web前后端内容,其中web服务器搭建大多采用现成的tomcat以及serverlet,今天突发奇想想着能不呢自己写一个web服务器,省去大量的对我来讲没什么用的解析过程,这个简单项目由此展开(所以这个项目没有采用任何框架,都是从零开始写的web服务器)

这是第一次迭代的想法:

1.web文件都采用文件管理模式(后续维护或许会添加上数据库管理模式吧)

2.通过终端控制台控制。

3.对每一个web的html创建一个控制文件负责时间戳的202与304处理

问题探讨:

问题:keep-alive 生效?

我第一次思考到keep-alive时想法把一个html文件关联了几个资源文件全部写到控制文件中进行处理,就用一个tcp连接来完成所有资源的传输,后来发现是我年轻了,因为html一旦发送到客户端后不管是否keep-alive与否它全都给我发送新的tcp资源请求到服务器了,也就是说,原来的那个tcp连接线程一直就在空等,死也等不到,后来查了查资料,告诉我可能原因是浏览器在接受html时是便接受边解析,一旦解析到资源请求就发送tcp连接,后果就是原来的tcp的html资源还没全部过来就有了要求,浏览器只能重新发送tcp连接,(虽然我在服务器上添加了守护线程超时就把tcp关掉了),但多个tcp线程挂在服务器属实离谱,后来发现当css文件到了客户端后,css的资源请求会从之前html请求的tcp连接发出,查询的话主流浏览器服务器都是把tcp连接挂载在服务器上,用守护线程等超时再关掉,除非有客户端发来的close,我思考(难道我要用我那少得可怜的服务器资源来等客户端的反应么)于是我果断弃用了keep-alive,就是一个请求我就发送一个资源然后就把socket给close了。(可能是我对http的keep-alive的理解还不够到位,欢迎指正点评)

以下就是我的第一版运行效果了:(挂载在Linux上了)

客户端浏览器就可以通过端口号+“/”+文件夹名称就可以访问文件夹下的网页文件...

 

 

 当然我这个服务器仅仅只是实现了get请求,以及实现了时间戳控制,后续会更新post请求等等吧

我有个大胆想法把post和get挂在不同服务器,然后用数据库活动密钥的方法来get和post,当然啦,仅仅还在初步构想。

如果不会配置源代码并需要源文件,欢迎私信我。。。

项目名:JavaWeb

包:user

JavaWeb.java:

  1. package user;
  2. import model.ServerBasis;
  3. public class JavaWeb
  4. {
  5. /**
  6. * 当前类是控制文件的标准类,用于控制文件的读取和写出。
  7. * 第一次设计迭代时间:2022.11.8 //Name:Star_Tian 功能:实现了基本的web传输功能,完成了基本文件控制,并将一个文件夹下文件进行便于管理
  8. * 第二次迭代时间:2022.11.15 //Name:Star_Tian 功能:修复了多个资源控制的bug,实现了时间戳控制,删除了KeepAlice,简化了控制文件,简要优化了控制台。
  9. * 第三次迭代时间:2022.11.17 //Name:Star_Tian 功能:添加了服务器配置文件,用于动态配置服务器的各种配置数据,初始只有端口等
  10. */
  11. static Integer OK=0;
  12. public static void main(String args[])
  13. {
  14. System.out.println("启动中......");
  15. ServerBasis.initialize();
  16. new ListenProc(ServerBasis.ServerInfo.GetPort()).start();
  17. }
  18. }

ListenProc.java:

  1. package user;
  2. import java.io.IOException;
  3. import java.net.ServerSocket;
  4. import java.net.Socket;
  5. import lockbag.HeaderAnalysis;
  6. public class ListenProc extends Thread
  7. {
  8. private int Port;
  9. private ServerSocket server;
  10. public ListenProc(int port)
  11. {
  12. this.Port=port;
  13. }
  14. public void run()
  15. {
  16. Socket client;
  17. System.out.println("监听端口初始化中......");
  18. try
  19. {
  20. server = new ServerSocket(this.Port);
  21. new Console(this.server).start();
  22. System.out.println("监听端口初始化建立成功");
  23. while(true)
  24. {
  25. client=this.server.accept();
  26. new HeaderAnalysis(client).start();
  27. }
  28. } catch (IOException e)
  29. {
  30. System.out.println("服务端接口已经失效,检查服务器端口占用情况");
  31. }
  32. }
  33. }

Console.java:

  1. package user;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.net.ServerSocket;
  6. import java.util.Scanner;
  7. import model.LWebY;
  8. import model.ServerBasis;
  9. public class Console extends Thread {
  10. ServerSocket Server;
  11. static String HelpString="//Quit 操作说明:退出系统命令\n"
  12. + "//Create 操作说明:建立新的web控制文件\n"
  13. + "//Update + web文件夹名 操作说明:更新web文件夹控制文件的时间戳\n"
  14. + "//SetPort + 端口号 操作说明:更新web服务器的端口号(重启后生效)\n"
  15. + " ......其他";
  16. static String CodeLeft="JavaWeb>>";
  17. static String CreateTip="创建web控制文件中,请按以下格式输入相关信息,信息之间用空格隔开(注意,新文件会覆盖旧文件,请注意备份):\n"
  18. +"主html文件名称+文件存储文件夹名称(当前目录下)";
  19. static String ErrTip="指令识别失败,可以使用//WebHelp(//help)查看帮助";
  20. static String TypeErrTip="输入信息不符合格式,创建失败";
  21. static String CodeErrTip="指令格式错误,请遵照//WebHelp(//help)提供格式输入指令";
  22. public Console(ServerSocket server)
  23. {
  24. this.Server=server;
  25. }
  26. public void run()
  27. {
  28. System.out.println("控制台程序初始化完成");
  29. System.out.println("控制台使用注意:使用//WebHelp(//help)指令可以获取指令帮助,当前指令系统不区分大小写");
  30. while(true)
  31. {
  32. System.out.print(CodeLeft);
  33. Scanner GetCode = new Scanner(System.in);
  34. String Code =GetCode.nextLine();
  35. String[] deal=Code.split(" ");
  36. deal[0]=deal[0].toLowerCase();
  37. switch(deal[0])
  38. {
  39. case "//quit":
  40. try
  41. {
  42. this.Server.close();
  43. } catch (IOException e)
  44. {
  45. e.printStackTrace();
  46. }
  47. System.out.println("服务器配置保存中......");
  48. ServerBasis.ServerInfo.FileWrite();
  49. System.out.println("退出系统成功.");
  50. GetCode.close();
  51. System.exit(0);
  52. break;
  53. case "//webhelp":
  54. System.out.println(HelpString);
  55. break;
  56. case "//help":
  57. System.out.println(HelpString);
  58. break;
  59. case "//create":
  60. System.out.println(CreateTip);
  61. Code =GetCode.nextLine();
  62. String Analysis[]=Code.split(" ");
  63. if(Analysis.length!=2)
  64. {
  65. System.err.println(CodeErrTip);
  66. break;
  67. }
  68. LWebY Create=new LWebY(Analysis[0],Analysis[1]);
  69. Create.new WriteThread(Create).start();
  70. break;
  71. case "//update":
  72. if(deal.length<2)
  73. System.err.println(CodeErrTip);
  74. else
  75. {
  76. new update(deal[1]).start();
  77. }
  78. break;
  79. case "//setport":
  80. {
  81. if(deal.length<2)
  82. System.err.println(CodeErrTip);
  83. else
  84. {
  85. ServerBasis.ServerInfo.setPort(Integer.valueOf(deal[1]));
  86. System.out.println("服务器端口更改成功,请//quit退出后重启服务器即可生效.");
  87. }
  88. break;
  89. }
  90. default:
  91. System.out.println(ErrTip);
  92. }
  93. }
  94. }
  95. public class update extends Thread{
  96. private String DirName;
  97. public update(String DirName)
  98. {
  99. this.DirName=DirName;
  100. }
  101. public void run()
  102. {
  103. File updateDir=new File(DirName);
  104. if(updateDir.exists()) {
  105. try
  106. {
  107. LWebY old=LWebY.FileInput(DirName);
  108. LWebY ControlNew=new LWebY(old.GetHtml(),old.GetDirName());
  109. ControlNew.new WriteThread(ControlNew).start();
  110. System.out.println("控制文件时间更新中...");
  111. } catch (FileNotFoundException e)
  112. {
  113. System.err.println("文件检入错误,当前的网页文件不存在control文件,请使用//create指令创建");
  114. }
  115. }
  116. else {
  117. System.err.println("文件夹不存在,当前操作无效");
  118. }
  119. }
  120. }
  121. }

包:model

LWebY.java:

  1. package model;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.ObjectInputStream;
  8. import java.io.ObjectOutputStream;
  9. import java.io.Serializable;
  10. import java.util.Date;
  11. /**/
  12. public class LWebY implements Serializable
  13. {
  14. /**
  15. * 当前类是控制文件的标准类,用于控制文件的读取和写出。
  16. * 第一次设计迭代时间:2022.11.8 //Name:Star_Tian
  17. * 第二次迭代时间:2022.11.15 //Name:Star_Tian 功能:修复了多个资源控制的bug
  18. */
  19. private static final long serialVersionUID = -5799700946989607148L;
  20. private String titleHtml; //html文件名称
  21. private String FileName; //控制文件名称
  22. private String DirName; //文件夹名称
  23. private Date LastNewDate; //最后更新日期
  24. private String DateStr; //日期的格式
  25. public LWebY(String titleHtml,String DirName)
  26. {
  27. this.FileName="Control.LWebY";
  28. this.titleHtml=titleHtml;
  29. this.DirName=DirName;
  30. this.LastNewDate=new Date();
  31. this.DateStr=this.LastNewDate.toString();
  32. }
  33. public class WriteThread extends Thread
  34. {
  35. private LWebY Focus;
  36. public WriteThread(LWebY Focus)
  37. {
  38. this.Focus=Focus;
  39. }
  40. public void run()
  41. {
  42. try
  43. {
  44. Focus.FileWrite();
  45. System.out.println("控制文件创建成功!");
  46. System.out.print("JavaWeb>>");
  47. } catch (FileNotFoundException e)
  48. {
  49. System.err.println("文件名称创建错误,检查LWebY.FileWrite的FileOutputStream");
  50. } catch (IOException e)
  51. {
  52. System.err.println("文件流出现错误,检查LWebY.FileWrite的ObjectOutputStream");
  53. } catch (FileNotFinded e)
  54. {
  55. System.err.println("输入参数错误,检查输入的路径以及html文件是否正确或尚未将文件放入指定文件夹");
  56. }
  57. }
  58. }
  59. public class FileNotFinded extends Exception
  60. {
  61. /**
  62. *
  63. */
  64. private static final long serialVersionUID = -5404731048838183395L;
  65. }
  66. public boolean FileWrite() throws FileNotFoundException,IOException, FileNotFinded
  67. {
  68. FileOutputStream Output;
  69. try
  70. {
  71. File newControl=new File(this.DirName+"/"+this.titleHtml);
  72. if(!newControl.exists())
  73. throw new FileNotFinded();
  74. Output = new FileOutputStream(DirName+"/"+FileName);
  75. ObjectOutputStream ObjectOut=new ObjectOutputStream(Output);
  76. Object ObjectContent=this;
  77. ObjectOut.writeObject(ObjectContent);
  78. ObjectOut.close();
  79. } catch (FileNotFoundException e)
  80. {
  81. System.err.println("文件流创建失败,请检查LWebY.FileWrite的FileOutputStream");
  82. throw e;
  83. } catch (IOException e)
  84. {
  85. System.err.println("文件流输出失败,请检查LWebY.FileWrite的输出流样式");
  86. throw e;
  87. }
  88. return true;
  89. }
  90. static public LWebY FileInput(String DirName) throws FileNotFoundException
  91. {
  92. try
  93. {
  94. DirName=DirName+"/Control.LWebY";
  95. FileInputStream FileIn=new FileInputStream(DirName);
  96. ObjectInputStream ObjectIn=new ObjectInputStream(FileIn);
  97. Object Reading=ObjectIn.readObject();
  98. ObjectIn.close();
  99. return (LWebY)Reading;
  100. } catch (FileNotFoundException e)
  101. {
  102. throw e;
  103. } catch (ClassNotFoundException e)
  104. {
  105. System.err.println("对象类型转化失败,请检查对应LWebY类本地是否存在,如不存在,请联系设计者获取");
  106. return null;
  107. } catch (IOException e)
  108. {
  109. System.err.println("输入流过程中出现异常,请检查LWebY.FileInput输入流");
  110. return null;
  111. }
  112. }
  113. public String GetDirName()
  114. {
  115. return this.DirName;
  116. }
  117. public String GetHtml()
  118. {
  119. return this.titleHtml;
  120. }
  121. public String GetDate()
  122. {
  123. return this.DateStr;
  124. }
  125. public Date DateGet()
  126. {
  127. return this.LastNewDate;
  128. }
  129. }

ServerBasis.java:

  1. package model;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.ObjectInputStream;
  8. import java.io.ObjectOutputStream;
  9. import java.io.Serializable;
  10. import java.util.Date;
  11. /**/
  12. public class LWebY implements Serializable
  13. {
  14. /**
  15. * 当前类是控制文件的标准类,用于控制文件的读取和写出。
  16. * 第一次设计迭代时间:2022.11.8 //Name:Star_Tian
  17. * 第二次迭代时间:2022.11.15 //Name:Star_Tian 功能:修复了多个资源控制的bug
  18. */
  19. private static final long serialVersionUID = -5799700946989607148L;
  20. private String titleHtml; //html文件名称
  21. private String FileName; //控制文件名称
  22. private String DirName; //文件夹名称
  23. private Date LastNewDate; //最后更新日期
  24. private String DateStr; //日期的格式
  25. public LWebY(String titleHtml,String DirName)
  26. {
  27. this.FileName="Control.LWebY";
  28. this.titleHtml=titleHtml;
  29. this.DirName=DirName;
  30. this.LastNewDate=new Date();
  31. this.DateStr=this.LastNewDate.toString();
  32. }
  33. public class WriteThread extends Thread
  34. {
  35. private LWebY Focus;
  36. public WriteThread(LWebY Focus)
  37. {
  38. this.Focus=Focus;
  39. }
  40. public void run()
  41. {
  42. try
  43. {
  44. Focus.FileWrite();
  45. System.out.println("控制文件创建成功!");
  46. System.out.print("JavaWeb>>");
  47. } catch (FileNotFoundException e)
  48. {
  49. System.err.println("文件名称创建错误,检查LWebY.FileWrite的FileOutputStream");
  50. } catch (IOException e)
  51. {
  52. System.err.println("文件流出现错误,检查LWebY.FileWrite的ObjectOutputStream");
  53. } catch (FileNotFinded e)
  54. {
  55. System.err.println("输入参数错误,检查输入的路径以及html文件是否正确或尚未将文件放入指定文件夹");
  56. }
  57. }
  58. }
  59. public class FileNotFinded extends Exception
  60. {
  61. /**
  62. *
  63. */
  64. private static final long serialVersionUID = -5404731048838183395L;
  65. }
  66. public boolean FileWrite() throws FileNotFoundException,IOException, FileNotFinded
  67. {
  68. FileOutputStream Output;
  69. try
  70. {
  71. File newControl=new File(this.DirName+"/"+this.titleHtml);
  72. if(!newControl.exists())
  73. throw new FileNotFinded();
  74. Output = new FileOutputStream(DirName+"/"+FileName);
  75. ObjectOutputStream ObjectOut=new ObjectOutputStream(Output);
  76. Object ObjectContent=this;
  77. ObjectOut.writeObject(ObjectContent);
  78. ObjectOut.close();
  79. } catch (FileNotFoundException e)
  80. {
  81. System.err.println("文件流创建失败,请检查LWebY.FileWrite的FileOutputStream");
  82. throw e;
  83. } catch (IOException e)
  84. {
  85. System.err.println("文件流输出失败,请检查LWebY.FileWrite的输出流样式");
  86. throw e;
  87. }
  88. return true;
  89. }
  90. static public LWebY FileInput(String DirName) throws FileNotFoundException
  91. {
  92. try
  93. {
  94. DirName=DirName+"/Control.LWebY";
  95. FileInputStream FileIn=new FileInputStream(DirName);
  96. ObjectInputStream ObjectIn=new ObjectInputStream(FileIn);
  97. Object Reading=ObjectIn.readObject();
  98. ObjectIn.close();
  99. return (LWebY)Reading;
  100. } catch (FileNotFoundException e)
  101. {
  102. throw e;
  103. } catch (ClassNotFoundException e)
  104. {
  105. System.err.println("对象类型转化失败,请检查对应LWebY类本地是否存在,如不存在,请联系设计者获取");
  106. return null;
  107. } catch (IOException e)
  108. {
  109. System.err.println("输入流过程中出现异常,请检查LWebY.FileInput输入流");
  110. return null;
  111. }
  112. }
  113. public String GetDirName()
  114. {
  115. return this.DirName;
  116. }
  117. public String GetHtml()
  118. {
  119. return this.titleHtml;
  120. }
  121. public String GetDate()
  122. {
  123. return this.DateStr;
  124. }
  125. public Date DateGet()
  126. {
  127. return this.LastNewDate;
  128. }
  129. }

包:lockbag

HeaderAnalysis.java

  1. package lockbag;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import java.net.Socket;
  6. import java.text.DateFormat;
  7. import java.text.ParseException;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Date;
  10. import java.util.Locale;
  11. public class HeaderAnalysis extends Thread {
  12. Socket Client;
  13. OutputStream Output;
  14. BufferedReader reader;
  15. boolean FirstOrNot;
  16. String ConnectionState="Keep-Alive";
  17. String DirName="";
  18. Date ModifiedDate;
  19. static DateFormat DateFor=new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy",Locale.ENGLISH);
  20. public HeaderAnalysis(Socket client)
  21. {
  22. /**
  23. * 当前类是控制文件的标准类,用于控制文件的读取和写出。
  24. * 第一次设计迭代时间:2022.11.8 //Name:Star_Tian
  25. * 第二次迭代时间:2022.11.15 //Name:Star_Tian 功能:添加了304时间戳测试,对时间戳进行判定,省略大量IO时间
  26. */
  27. this.Client=client;
  28. this.FirstOrNot=true;
  29. }
  30. public void run()
  31. {
  32. try {
  33. byte[] header=new byte[1024*1024];
  34. TimeCul TimeClose=new TimeCul(5000,this.Client);
  35. TimeClose.start(); //启动守护线程
  36. int len=Client.getInputStream().read(header);
  37. TimeClose.interrupt();//收到信息关闭守护线程
  38. if(len>0)
  39. {
  40. String Out=new String(header,"UTF-8");
  41. String TargetFile="",Action="",Versions="",IPHost="",AccConnectionStates="";
  42. String[] Analysis=Out.split("\r\n");
  43. for(int i=0;i<Analysis.length;i++) {
  44. String[] SecondAna=Analysis[i].split(" ",2);
  45. switch(SecondAna[0])
  46. {
  47. case "GET":
  48. Action=SecondAna[0];
  49. String GetFocus[]=SecondAna[1].split(" ", 2);
  50. TargetFile=GetFocus[0];
  51. Versions=GetFocus[1];
  52. break;
  53. case "POST":
  54. break;
  55. case "Host:":
  56. IPHost=SecondAna[1];
  57. break;
  58. case "Accept:":
  59. AccConnectionStates=SecondAna[1];
  60. break;
  61. case "If-Modified-Since:":
  62. try
  63. {
  64. ModifiedDate=HeaderAnalysis.DateFor.parse(SecondAna[1]);
  65. } catch (ParseException e)
  66. {
  67. // TODO 自动生成的 catch 块
  68. System.err.println("日期转化失败");
  69. }
  70. break;
  71. default:
  72. break;
  73. }
  74. }
  75. if(!TargetFile.isEmpty())
  76. {
  77. new SendProc(TargetFile,Action,Versions,IPHost,AccConnectionStates,this.Client,ModifiedDate).start();
  78. }
  79. }
  80. } catch (IOException e) {
  81. }
  82. }
  83. }

SendProc.java:

  1. package lockbag;
  2. import java.io.FileNotFoundException;
  3. import java.net.Socket;
  4. import java.util.Date;
  5. import data.MinSend;
  6. import model.LWebY;
  7. public class SendProc extends Thread
  8. {
  9. /**
  10. * 当前类是头文件解析后发送基本文件的解释类,完成对头部解析的信息进行分析处理后调用数据层MinSend。
  11. * 第一次设计迭代时间:2022.11.8 //Name:Star_Tian
  12. * 第二次迭代时间:2022.11.15 //Name:Star_Tian 功能:添加了304时间戳测试,添加了来源请求预警与记录播报
  13. */
  14. String TargetFile,Action,Versions,IPHost,AccConnecitonStates;
  15. Socket Client;
  16. Date ModifiedDate;
  17. public SendProc(String TargetFile,String Action,String Versions,String IPHost,String AccConnectionStates,Socket client,Date ModifiedDate)
  18. {
  19. this.Client=client;
  20. this.TargetFile=TargetFile;
  21. this.Action=Action;
  22. this.Versions=Versions;
  23. this.IPHost=IPHost;
  24. this.AccConnecitonStates=AccConnectionStates;
  25. this.ModifiedDate=ModifiedDate;
  26. }
  27. public void run()
  28. {
  29. String FileName[]=this.TargetFile.substring(1).split("/");
  30. try
  31. {
  32. LWebY ControlFile = LWebY.FileInput(FileName[0]);
  33. if(FileName.length<2) {
  34. if(this.ModifiedDate!=null&&ControlFile.DateGet().getTime()-this.ModifiedDate.getTime()<1000)
  35. {
  36. new MinSend("304 Not Modified",this.Action,this.Versions,this.IPHost,"close",this.Client,ControlFile).start();
  37. }
  38. else{
  39. new MinSend(FileName[0],ControlFile.GetHtml(),this.Action,this.Versions,this.IPHost,"close",this.Client,ControlFile).start();
  40. }
  41. }
  42. else {
  43. if(this.ModifiedDate!=null&&ControlFile.DateGet().getTime()-this.ModifiedDate.getTime()<1000) {
  44. new MinSend("304 Not Modified",this.Action,this.Versions,this.IPHost,"close",this.Client,ControlFile).start();
  45. }
  46. else {
  47. new MinSend(FileName[0],FileName[1],this.Action,this.Versions,this.IPHost,"close",this.Client,ControlFile).start();
  48. }
  49. }
  50. } catch (FileNotFoundException e)
  51. {
  52. System.err.println("警告,出现无效文件访问,访问来源:"+this.Client.toString()+" 请求目标:"+this.TargetFile+" 请求时间:"+new Date().toString());
  53. System.out.print("JavaWeb>>");
  54. }
  55. System.out.println("来自"+this.Client.toString()+"的请求资源:"+this.TargetFile+" 请求时间:"+new Date().toString());
  56. System.out.print("JavaWeb>>");
  57. }
  58. }

TimeCul.java

  1. package lockbag;
  2. import java.io.IOException;
  3. import java.net.Socket;
  4. public class TimeCul extends Thread
  5. {
  6. /**
  7. * 当前类是等待TCP连接的守护线程,用于超时关闭线程避免占用资源。
  8. * 第一次设计迭代时间:2022.11.9 //Name:Star_Tian
  9. * 第二次迭代:2022.11.16 暂时取消本类使用,由于取消了keep-alive机制
  10. */
  11. long timelong;
  12. Socket Client;
  13. public TimeCul(long timelong,Socket Client)
  14. {
  15. this.timelong=timelong;
  16. this.Client=Client;
  17. }
  18. public void run()
  19. {
  20. try
  21. {
  22. Thread.sleep(timelong);
  23. Client.close();
  24. } catch (InterruptedException e)
  25. {
  26. } catch (IOException e)
  27. {
  28. }
  29. }
  30. }

包:data

MinSend.java:

  1. package data;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.UnsupportedEncodingException;
  6. import java.net.Socket;
  7. import java.util.Date;
  8. import model.LWebY;
  9. public class MinSend extends Thread
  10. {
  11. /**
  12. * 当前类数据层底层发送类,负责IO主要功能以及Socket的主要通信.
  13. * 第一次设计迭代时间:2022.11.8 //Name:Star_Tian
  14. * 第二次迭代时间:2022.11.15 //Name:Star_Tian 功能:添加了304时间戳测试,添加了来源请求预警与记录播报
  15. */
  16. public final static String header="HTTP/1.1 %s\r\n"
  17. + "Server: LwebY\r\n"
  18. + "Accept-Ranges: bytes\r\n"
  19. + "Keep-Alive: timeout=5, max=100\r\n"
  20. + "Connection: %s\r\n"
  21. + "Date: %s\r\n"
  22. + "Last-Modified: %s\r\n\r\n";
  23. @SuppressWarnings("unused")
  24. private String Action,Versions,IPHost,ConnecitonStates;
  25. private Socket Client;
  26. private String DirName,FileName;
  27. private LWebY Control;
  28. private String State=null;
  29. public MinSend(String DirName,String FileName,String Action,String Versions,String IPHost,String ConnectionStates,Socket client,LWebY Control)
  30. {
  31. this.DirName=DirName;
  32. this.FileName=FileName;
  33. this.Client=client;
  34. this.Action=Action;
  35. this.Versions=Versions;
  36. this.IPHost=IPHost;
  37. this.ConnecitonStates=ConnectionStates;
  38. this.Control=Control;
  39. }
  40. public MinSend(String state,String Action,String Versions,String IPHost,String ConnectionStates,Socket client,LWebY Control)
  41. {
  42. this.State=state;
  43. this.Client=client;
  44. this.Action=Action;
  45. this.Versions=Versions;
  46. this.IPHost=IPHost;
  47. this.ConnecitonStates=ConnectionStates;
  48. this.Control=Control;
  49. }
  50. public void run()
  51. {
  52. Date DateNow=new Date();
  53. if(State!=null)
  54. {
  55. String HeaderFormat=String.format(MinSend.header,this.State,"close",DateNow.toString(),this.Control.GetDate());
  56. byte[] Header;
  57. try
  58. {
  59. Header = HeaderFormat.getBytes("UTF-8");
  60. Client.getOutputStream().write(Header,0,Header.length);
  61. Client.getOutputStream().flush();
  62. Client.close();
  63. return;
  64. } catch (UnsupportedEncodingException e)
  65. {
  66. // TODO 自动生成的 catch 块
  67. e.printStackTrace();
  68. } catch (IOException e)
  69. {
  70. // TODO 自动生成的 catch 块
  71. e.printStackTrace();
  72. }
  73. }
  74. try
  75. {
  76. File GetFile=new File(DirName+"/"+FileName);
  77. byte[] Buffer=new byte[(int) GetFile.length()+1024];
  78. FileInputStream Open=new FileInputStream(GetFile);
  79. String HeaderFormat=String.format(MinSend.header,"200 OK",this.ConnecitonStates,DateNow.toString(),this.Control.GetDate());
  80. byte[] Header = HeaderFormat.getBytes("UTF-8");
  81. System.arraycopy(Header, 0, Buffer, 0, Header.length);
  82. int FileLen=Open.read(Buffer, Header.length,Buffer.length-Header.length);
  83. String End="\r\n";
  84. byte[] end=End.getBytes("UTF-8");
  85. System.arraycopy(end, 0, Buffer, Header.length+FileLen, end.length);
  86. Client.getOutputStream().write(Buffer,0,Header.length+FileLen+end.length);
  87. Client.getOutputStream().flush();
  88. Open.close();
  89. Client.close();
  90. } catch (IOException e)
  91. {
  92. // TODO 自动生成的 catch 块
  93. try {
  94. String HeaderFormat=String.format(MinSend.header,"404 NOT FOUND","close",new Date().toString(),"");
  95. byte[] Header=HeaderFormat.getBytes("UTF-8");
  96. Client.getOutputStream().write(Header,0,Header.length);
  97. Client.getOutputStream().flush();
  98. Client.close();
  99. } catch (UnsupportedEncodingException e1) {
  100. // TODO Auto-generated catch block
  101. e1.printStackTrace();
  102. } catch (IOException e1) {
  103. // TODO Auto-generated catch block
  104. e1.printStackTrace();
  105. }
  106. }
  107. }
  108. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/216956
推荐阅读
相关标签
  

闽ICP备14008679号