package tomcat;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.OutputStream;import java.io.PrintStream;public class Response {private OutputStream out;private FileInputStream fis;public Response(OutputStream out){this.out = out;}public void sendData(String path) throws IOException{File filePath = new File(path);//响应头中的数据String status ="HTTP/1.1 ";String Server ="Server:McaServer ";String type ="Content-Type:";String length ="Content-Length:";PrintStream ps = new PrintStream(out);if(filePath.exists()){fis = new FileInputStream(path);System.out.println(path+"------------------");ps.println(status+"200 OK");ps.println(Server);ps.println(type+getType(path));ps.println(length+fis.available());ps.println();ps.flush();//响应体byte[] buffer = new byte[fis.available()];int len = fis.read(buffer, 0, fis.available());ps.write(buffer, 0, len);ps.flush();}else{fis = new FileInputStream("webContent/notFound.html");ps.println(status+"404 Not Found");ps.println(Server);ps.println(type+"text/html");ps.println(length+fis.available());ps.println();ps.flush();//响应体byte[] buffer = new byte[fis.available()];int len = fis.read(buffer, 0, fis.available());ps.write(buffer, 0, len);ps.flush();}fis.close();}public String getType(String path){if(path.endsWith(".html")){return "text/html";}if(path.endsWith(".mp3")){return "audio/mpeg";}if(path.endsWith(".mp4")){return "video/mp4";}if(path.endsWith("jpeg")){return "image/jpeg";}elsereturn "text/plain";}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。