日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

Java/Android 實現簡單的HTTP服務器

瀏覽:32日期:2022-08-22 10:55:24

目前在對Android的代碼進行功能測試的時候,需要服務器返回一個數據來測試整個流程是否正確。不希望引入第三方的JAR包,因此需要一個特別簡單的HTTP服務器。

網上查詢了一下,找到可用的代碼如下:

import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;import java.util.Date;import java.util.StringTokenizer;// The tutorial can be found just here on the SSaurel’s Blog : // https://www.ssaurel.com/blog/create-a-simple-http-web-server-in-java// Each Client Connection will be managed in a dedicated Threadpublic class JavaHTTPServer implements Runnable{ static final File WEB_ROOT = new File('.');static final String DEFAULT_FILE = 'index.html';static final String FILE_NOT_FOUND = '404.html';static final String METHOD_NOT_SUPPORTED = 'not_supported.html';// port to listen connectionstatic final int PORT = 8080;// verbose modestatic final boolean verbose = true;// Client Connection via Socket Classprivate Socket connect;public JavaHTTPServer(Socket c) {connect = c;}public static void main(String[] args) {try {ServerSocket serverConnect = new ServerSocket(PORT);System.out.println('Server started.nListening for connections on port : ' + PORT + ' ...n');// we listen until user halts server executionwhile (true) {JavaHTTPServer myServer = new JavaHTTPServer(serverConnect.accept());if (verbose) {System.out.println('Connecton opened. (' + new Date() + ')');}// create dedicated thread to manage the client connectionThread thread = new Thread(myServer);thread.start();}} catch (IOException e) {System.err.println('Server Connection error : ' + e.getMessage());}}@Overridepublic void run() {// we manage our particular client connectionBufferedReader in = null; PrintWriter out = null; BufferedOutputStream dataOut = null;String fileRequested = null;try {// we read characters from the client via input stream on the socketin = new BufferedReader(new InputStreamReader(connect.getInputStream()));// we get character output stream to client (for headers)out = new PrintWriter(connect.getOutputStream());// get binary output stream to client (for requested data)dataOut = new BufferedOutputStream(connect.getOutputStream());// get first line of the request from the clientString input = in.readLine();// we parse the request with a string tokenizerStringTokenizer parse = new StringTokenizer(input);String method = parse.nextToken().toUpperCase(); // we get the HTTP method of the client// we get file requestedfileRequested = parse.nextToken().toLowerCase();// we support only GET and HEAD methods, we checkif (!method.equals('GET') && !method.equals('HEAD')) {if (verbose) {System.out.println('501 Not Implemented : ' + method + ' method.');}// we return the not supported file to the clientFile file = new File(WEB_ROOT, METHOD_NOT_SUPPORTED);int fileLength = (int) file.length();String contentMimeType = 'text/html';//read content to return to clientbyte[] fileData = readFileData(file, fileLength);// we send HTTP Headers with data to clientout.println('HTTP/1.1 501 Not Implemented');out.println('Server: Java HTTP Server from SSaurel : 1.0');out.println('Date: ' + new Date());out.println('Content-type: ' + contentMimeType);out.println('Content-length: ' + fileLength);out.println(); // blank line between headers and content, very important !out.flush(); // flush character output stream buffer// filedataOut.write(fileData, 0, fileLength);dataOut.flush();} else {// GET or HEAD methodif (fileRequested.endsWith('/')) {fileRequested += DEFAULT_FILE;}File file = new File(WEB_ROOT, fileRequested);int fileLength = (int) file.length();String content = getContentType(fileRequested);if (method.equals('GET')) { // GET method so we return contentbyte[] fileData = readFileData(file, fileLength);// send HTTP Headersout.println('HTTP/1.1 200 OK');out.println('Server: Java HTTP Server from SSaurel : 1.0');out.println('Date: ' + new Date());out.println('Content-type: ' + content);out.println('Content-length: ' + fileLength);out.println(); // blank line between headers and content, very important !out.flush(); // flush character output stream bufferdataOut.write(fileData, 0, fileLength);dataOut.flush();}if (verbose) {System.out.println('File ' + fileRequested + ' of type ' + content + ' returned');}}} catch (FileNotFoundException fnfe) {try {fileNotFound(out, dataOut, fileRequested);} catch (IOException ioe) {System.err.println('Error with file not found exception : ' + ioe.getMessage());}} catch (IOException ioe) {System.err.println('Server error : ' + ioe);} finally {try {in.close();out.close();dataOut.close();connect.close(); // we close socket connection} catch (Exception e) {System.err.println('Error closing stream : ' + e.getMessage());} if (verbose) {System.out.println('Connection closed.n');}}}private byte[] readFileData(File file, int fileLength) throws IOException {FileInputStream fileIn = null;byte[] fileData = new byte[fileLength];try {fileIn = new FileInputStream(file);fileIn.read(fileData);} finally {if (fileIn != null) fileIn.close();}return fileData;}// return supported MIME Typesprivate String getContentType(String fileRequested) {if (fileRequested.endsWith('.htm') || fileRequested.endsWith('.html'))return 'text/html';elsereturn 'text/plain';}private void fileNotFound(PrintWriter out, OutputStream dataOut, String fileRequested) throws IOException {File file = new File(WEB_ROOT, FILE_NOT_FOUND);int fileLength = (int) file.length();String content = 'text/html';byte[] fileData = readFileData(file, fileLength);out.println('HTTP/1.1 404 File Not Found');out.println('Server: Java HTTP Server from SSaurel : 1.0');out.println('Date: ' + new Date());out.println('Content-type: ' + content);out.println('Content-length: ' + fileLength);out.println(); // blank line between headers and content, very important !out.flush(); // flush character output stream bufferdataOut.write(fileData, 0, fileLength);dataOut.flush();if (verbose) {System.out.println('File ' + fileRequested + ' not found');}}}

以上就是Java/Android 實現簡單的HTTP服務器的詳細內容,更多關于Java/Android HTTP服務器的資料請關注好吧啦網其它相關文章!

標簽: Java
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
欧美福利专区| 色综合视频一区二区三区日韩 | 麻豆国产一区| 亚洲欧洲专区| 日韩1区2区3区| 亚洲免费婷婷| 欧美日韩黄网站| 免费观看亚洲天堂| 正在播放日韩精品| 激情五月综合| 蜜桃一区二区三区在线观看| 亚洲精品麻豆| 国产精品大片免费观看| 国产白浆在线免费观看| 日韩午夜av| 日本视频在线一区| 日韩成人亚洲| 亚洲一区二区三区在线免费| 日韩成人一级| 国产精品极品国产中出| 免费观看亚洲天堂| 久久精品一区二区三区中文字幕| 久久精品72免费观看| 国产精品成人3p一区二区三区| 国产欧美三级| 国产精品亚洲二区| 欧美日韩免费观看视频| 欧美+亚洲+精品+三区| 香蕉成人久久| 欧美一区精品| 丁香婷婷久久| 欧美在线资源| 日韩精品欧美成人高清一区二区| 国产日韩欧美一区| 国产不卡人人| 国产综合色产| 日韩av二区在线播放| 国产videos久久| 免费成人在线影院| 国产精品1区在线| 久久久天天操| 青青国产精品| 蜜桃av.网站在线观看| 在线综合视频| 美女精品视频在线| 91久久在线| 国产精品免费99久久久| 亚洲综合三区| 国产一区福利| 亚洲欧美网站在线观看| 国产精品成人一区二区网站软件| 日韩欧美二区| 欧美精品中文| 欧美成a人国产精品高清乱码在线观看片在线观看久 | 亚洲资源av| 视频在线不卡免费观看| 中文无码久久精品| 日韩久久视频| 国产欧美丝祙| 亚洲开心激情| 美女国产一区| 一区二区精品伦理...| 91免费精品国偷自产在线在线| 99久久久久国产精品| 国产精品2023| 欧美一区91| 日韩久久99| 欧美日韩日本国产亚洲在线 | 在线精品亚洲| 久久激情中文| а√天堂8资源在线| 国产日产一区| 视频一区日韩精品| 四虎884aa成人精品最新| 国产剧情一区| 国产亚洲久久| 欧美在线黄色| 日韩三级久久| 日韩国产在线一| 色综合视频一区二区三区日韩| 悠悠资源网久久精品| 国产精品久久久久av电视剧| 日韩综合一区| 国产精品av久久久久久麻豆网| 波多视频一区| 久久国产日本精品| 91久久久精品国产| 99热精品在线| 亚洲一区二区免费看| 欧美+亚洲+精品+三区| 欧美天堂亚洲电影院在线观看| 美女少妇全过程你懂的久久| 亚洲五月婷婷| 国内不卡的一区二区三区中文字幕| 福利一区二区免费视频| 丝袜美腿诱惑一区二区三区| 激情婷婷综合| 日本va欧美va瓶| 美女av一区| 狠狠干综合网| 日韩不卡一二三区| 麻豆久久久久久| 日韩黄色大片| 亚洲欧洲一区| 欧美视频久久| 97欧美在线视频| 久久精品国产www456c0m| 六月婷婷一区| 九九99久久精品在免费线bt| 999久久久亚洲| 午夜性色一区二区三区免费视频| 日韩精品久久理论片| 国产成人黄色| 国产精品最新| 亚洲精品国产偷自在线观看| 日韩在线观看一区二区三区| 国产精品久久久久久久久久齐齐| 91精品一区二区三区综合在线爱| 欧美午夜不卡| 国产精品久久久久久久久久齐齐| 99久久亚洲精品| 911精品国产| 亚洲综合日本| 日本不卡免费高清视频在线| 中文字幕一区二区精品区| 日产精品一区二区| 91精品国产自产精品男人的天堂| 99精品在线免费在线观看| 狂野欧美性猛交xxxx| 亚洲一区二区三区在线免费| 久久久久免费av| 成人在线视频免费看| 日韩激情综合| 日韩影院精彩在线| 国产成年精品| 加勒比视频一区| 久久a爱视频| 日韩精品1区2区3区| 中文亚洲免费| 蜜桃视频欧美| 免费成人性网站| 一区在线免费| 欧美日韩国产免费观看视频| 日韩精品免费一区二区在线观看| 麻豆精品久久久| 91久久精品无嫩草影院| 亚洲精品黄色| 亚洲日产国产精品| 亚洲精品字幕| 在线观看免费一区二区| 在线看片不卡| 黄色欧美日韩| 午夜性色一区二区三区免费视频| 亚洲综合婷婷| 欧美另类中文字幕| 国产精品久久久久久久久久齐齐 | 国产精品13p| 精品久久99| 正在播放日韩精品| 制服诱惑一区二区| 亚洲一区二区三区在线免费| 亚洲精品进入| 久久99久久人婷婷精品综合| 国产精品igao视频网网址不卡日韩| 国产精品99精品一区二区三区∴ | 亚洲a一区二区三区| 香蕉视频成人在线观看| 欧美天堂一区| av免费不卡国产观看| 五月天久久777| 国产无遮挡裸体免费久久| 国产精品亚洲欧美| 神马日本精品| 日韩在线一区二区| 国产精成人品2018| 亚洲午夜黄色| 欧美精品国产| 狠狠操综合网| 麻豆视频观看网址久久| 欧美成人亚洲| 老司机精品视频在线播放| 亚洲精品91| 国产欧美一级| 欧美福利一区| 精品一区二区三区中文字幕视频| 国产日韩欧美高清免费| 国产精品观看| 亚洲天堂黄色| 精品亚洲精品| 中文字幕乱码亚洲无线精品一区| 91偷拍一区二区三区精品| 亚洲制服少妇| 亚洲欧洲高清| 国产毛片一区二区三区| 久久福利一区| 日韩一区三区| 日韩成人高清| 中文字幕在线免费观看视频| 日韩av一区二| 日本一区二区三区中文字幕|