HttpServer 模拟监听数据接收
这两天研究的课题就是HttpServer,这是java自带一个处理监听端口的服务端,也就说,有了这个,只要项目运行,那你的电脑就是一台服务器。通常情况下,都是在Linux系统上运行,当你的电脑装配了虚拟机,虚拟机装配了HttpServer,就可以模拟部署项目。
一 实现过程
import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; import org.apache.commons.io.IOUtils; import java.io.*; import java.net.InetSocketAddress; import java.net.URLDecoder; import java.text.SimpleDateFormat; import java.util.*; /** * Hello world! */ public class App { public final static String DATE_FORMAT_SECOND = "yyyyMMddHHmmss"; public final static String path ="D:\\data\\"; static class TestHandler implements HttpHandler { @Override public void handle(HttpExchange exchange) { System.out.println("start data deal"); try { byte[] bytes = IOUtils.toByteArray(exchange.getRequestBody()); ResultData resultData = getResult(bytes); System.out.println("len:" + resultData.getBytes().length); FileOutputStream fos = new FileOutputStream(new File(path+getImageName()+".jpg")); fos.write(resultData.getBytes()); BufferedWriter writer = new BufferedWriter(new FileWriter(path+getImageName()+".txt")); writer.write(resultData.getJson().trim()); writer.flush(); writer.close(); fos.close(); } catch (Exception e) { e.printStackTrace(); System.out.println("read data is error "); } System.out.println("main over"); } } public static String getImageName() { Date date = new Date(); SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_SECOND); return format.format(date); } public static ResultData getResult(byte[] bytes) throws Exception { ResultData resultData = new ResultData(); //获取字符串 String s = new String(bytes, 0, bytes.length, "UTF-8"); StringBuffer sb = new StringBuffer(); sb.append(s); int jsonStart = sb.indexOf("Content-Disposition: form-data; name=\"attachValue\""); int jsonEnd = sb.indexOf("Content-Disposition: form-data; name=\"attachFile\"; filename=\"snap.jpg\""); String jsonString = sb.substring(jsonStart, jsonEnd); //数据进行处理 确保数据格式 int kChangeIndex = jsonString.indexOf("------KchangFA37jNCchRYdSBZA"); int len = ("Content-Disposition: form-data; name=\"attachValue\"").length() + 1; jsonString = jsonString.substring(len, kChangeIndex); resultData.setJson(jsonString); //获取image byte[] DataInputStream bis = new DataInputStream(new ByteArrayInputStream(bytes)); while (true) { String str = bis.readLine();; if (str.contains("Content-Type")) { bis.readLine(); break; } } int num; byte[] b = new byte[1024]; ByteArrayOutputStream bos = new ByteArrayOutputStream(); while ((num = bis.read(b)) != -1) { String sss = new String(b, 0, num); bos.write(b, 0, num); if (sss.contains("KchangFA37jNCchRYdSBZA")) { break; } } resultData.setBytes(bos.toByteArray()); return resultData; } public static long bytesToLong(byte[] array) { return ((((long) array[0] & 0xff) << 56) | (((long) array[1] & 0xff) << 48) | (((long) array[2] & 0xff) << 40) | (((long) array[3] & 0xff) << 32) | (((long) array[4] & 0xff) << 24) | (((long) array[5] & 0xff) << 16) | (((long) array[6] & 0xff) << 8) | (((long) array[7] & 0xff) << 0)); } public static Map<String, String> formData2Dic(String formData) { Map<String, String> result = new HashMap<>(); if (formData == null || formData.trim().length() == 0) { return result; } final String[] items = formData.split("&"); Arrays.stream(items).forEach(item -> { final String[] keyAndVal = item.split("="); if (keyAndVal.length == 2) { try { final String key = URLDecoder.decode(keyAndVal[0], "utf8"); final String val = URLDecoder.decode(keyAndVal[1], "utf8"); result.put(key, val); } catch (UnsupportedEncodingException e) { } } }); return result; } public static void main(String[] args) throws Exception { System.out.println("main start"); HttpServer server = HttpServer.create(new InetSocketAddress(9300), 0); server.createContext("/config", new TestHandler()); server.start(); }
这是我的实现过程
localhost:9300/config
注意,具体什么的数据请求,可以自己测试自己模拟