Java HTTPServer从服务器向客户端发送get请求
我想从http服务器向客户端发送参数(GET请求)Java HTTPServer从服务器向客户端发送get请求,java,http,Java,Http,我想从http服务器向客户端发送参数(GET请求) private void displayPage(HttpExchange he) throws IOException { String root = "html"; URI uri = he.getRequestURI(); File file = new File(root + uri.getPath()).getCanonicalFile(); OutputStream os = null;
private void displayPage(HttpExchange he) throws IOException {
String root = "html";
URI uri = he.getRequestURI();
File file = new File(root + uri.getPath()).getCanonicalFile();
OutputStream os = null;
String response = "";
if (!file.isFile()) {
// Object does not exist or is not a file: reject with 404 error.
response = "404 (Not Found)\n";
he.sendResponseHeaders(404, response.length());
os = he.getResponseBody();
os.write(response.getBytes());
} else {
// Object exists and is a file: accept with response code 200.
he.sendResponseHeaders(200, 0);
os = he.getResponseBody();
FileInputStream fs = new FileInputStream(file);
final byte[] buffer = new byte[0x10000];
int count = 0;
while ((count = fs.read(buffer)) >= 0)
os.write(buffer,0,count);
fs.close();
}
os.close();
}
此方法将request.html页面发送到客户端
private void displayPage(HttpExchange he) throws IOException {
String root = "html";
URI uri = he.getRequestURI();
File file = new File(root + uri.getPath()).getCanonicalFile();
OutputStream os = null;
String response = "";
if (!file.isFile()) {
// Object does not exist or is not a file: reject with 404 error.
response = "404 (Not Found)\n";
he.sendResponseHeaders(404, response.length());
os = he.getResponseBody();
os.write(response.getBytes());
} else {
// Object exists and is a file: accept with response code 200.
he.sendResponseHeaders(200, 0);
os = he.getResponseBody();
FileInputStream fs = new FileInputStream(file);
final byte[] buffer = new byte[0x10000];
int count = 0;
while ((count = fs.read(buffer)) >= 0)
os.write(buffer,0,count);
fs.close();
}
os.close();
}
我想在http头中插入以下参数:
String urlParameters = "sensornumber="+N_SENSORI+"&lightnumber="+N_LUCI+"&defaultlightname="+LUCE+"&defaultsensornumber="+SENSORE;
因此,我可以在.html文件中的javascript中使用它们为什么不根据需要生成html,而不是发送带有标题的静态html等?我不会赞同这一切背后的逻辑思想,但要回答您的问题,您应该理解的是,您正在读取一个文件,然后将其导出。您应该开始了解如何在java中编辑文件。这种逻辑将在while循环中使用明显的if语句。这应该可以解决当前的问题。