的Java IO插槽Web服务器 - 发送到输出流之前无法读取POST消息 - HTML文件
问题描述:
我有以下问题:的Java IO插槽Web服务器 - 发送到输出流之前无法读取POST消息 - HTML文件
我试图用Java编写一个简单的Web服务器,它可以托管HTML文件并在.html文件中处理POST公式的公式。
问题是,在我刷新输出流之前,BufferedReader没有从HTML文档中的公式中读取消息。
所以我总是要刷新输出流,然后才能处理POST消息。
对于我的服务器,我需要在刷新输出流之前处理POST消息,所以我可以在发生POST消息的情况下自定义HTML文件。
这里是我的代码(simplized有点;)的服务器):
import java.io.BufferedReader;
import java.io.DataOutputStream;
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.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class NewWebServer {
public static void main(String[] args) throws Exception {
new NewWebServer();
}
public NewWebServer() {
waitForConnection();
}
/**
* Open the server
*/
public void waitForConnection() {
// Set standard socketPort and serverSocket variable
ServerSocket serverSocket = null;
int socketPort = 8080;
boolean socketFound = false;
// Search for available socket ports (bigger than 8079)
while (!socketFound) {
System.out.println("Try to open Server at Port " + socketPort);
try {
// Try to open server socket
serverSocket = new ServerSocket(socketPort);
// Server Socket openend
socketFound = true;
} catch (Exception e) {
e.printStackTrace();
// Set port + 1
socketPort++;
}
System.out.println("++++++++++++++++Server Openened++++++++++");
System.out.println("Server started at Port " + socketPort);
}
Socket socket;
for (;;) {
try {
System.out.println("Server waiting for Connection");
socket = serverSocket.accept();
System.out.println("Server got Connection");
new Thread(new SocketHandler(socket)).start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class SocketHandler implements Runnable {
Socket socket;
public SocketHandler(Socket socket) {
this.socket = socket;
}
public void run() {
// Handle the Input Stream
new Thread(new InputStreamHandler(socket)).start();
}
}
class InputStreamHandler implements Runnable {
Socket socketToHandle;
DataOutputStream dataOutputStream;
public InputStreamHandler(Socket socketToHandle) {
this.socketToHandle = socketToHandle;
}
public void run() {
BufferedReader bufferedReader;
try {
// Initialize BufferedReader to read Input Stream
bufferedReader = new BufferedReader(new InputStreamReader(socketToHandle.getInputStream()));
// Initialize DataOutputStream to Handle OutputStream
dataOutputStream = new DataOutputStream(socketToHandle.getOutputStream());
String lineOfInputStream = null;
/*
* Write the Website Header and the .html file
*
* Have to be before the Input Stream reading, if not send , the
* post of the HTML Formular can't be read ! ?
*/
new Thread(new WriteToOutputStream(dataOutputStream)).start();
// Read the InputStream
while ((lineOfInputStream = bufferedReader.readLine()) != null) {
System.out.println("InputStream got Line " + lineOfInputStream);
}
System.out.println("BufferedReader finished");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class WriteToOutputStream implements Runnable {
DataOutputStream out;
public WriteToOutputStream(DataOutputStream dataOutputStream) {
this.out = dataOutputStream;
}
public void run() {
System.out.println("Run WriteToOutputStream");
// File of HTML File with <form action="" method="POST"> Formular
File file = new File("C:/htdocs/test.html");
FileInputStream fileInputStream;
try {
// Open File Input Stream
fileInputStream = new FileInputStream(file);
// Write the Header for the Browser , to handle the .html Output
out.writeBytes("HTTP/1.0 200 OK\r\n");
out.writeBytes("Content-Type: text/html\r\n");
out.writeBytes("Content-Length: " + fileInputStream.available() + "\r\n");
out.writeBytes("Server: Jastonex/0.1");
out.writeBytes("\r\n\r\n");
// Initialize Bytes
byte[] fileBytes = null;
// Read the file, convert it into a byte array
fileBytes = new byte[fileInputStream.available()];
fileInputStream.read(fileBytes);
// Write the byte array of the file into the output stream
out.write(fileBytes);
// Flush
out.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("OutputStreamRun finished");
}
}
}
这里有一个HTML文件,我主持的一个例子:
<html>
\t <head>
\t \t <title>ASDF</title>
\t </head>
\t <body>
\t \t <p style="color:red;">Blablabla</p>
\t \t <form action="" method="POST">
\t \t <input type="password" name="passwort" value="Bla"/>
\t \t <input type="password2" name="passwort2" value="Bla"/>
\t \t <input type="password3" name="passwort34" value="Bla"/>
\t \t <input type="password4" name="passwort55" value="Bla"/>
\t \t \t <input type="submit" name="submit" value="Testbutton">
\t \t </form>
\t </body>
</html>
这里是控制台中的输出:
Try to open Server at Port 8080
++++++++++++++++Server Openened++++++++++
Server started at Port 8080
Server waiting for Connection
Server got Connection
Server waiting for Connection
Run WriteToOutputStream
InputStream got Line POST /asshjshshs HTTP/1.1
InputStream got Line Host: localhost:8080
InputStream got Line Connection: keep-alive
InputStream got Line Content-Length: 74
InputStream got Line Cache-Control: max-age=0
InputStream got Line Origin: http://localhost:8080
InputStream got Line Upgrade-Insecure-Requests: 1
InputStream got Line User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36
InputStream got Line Content-Type: application/x-www-form-urlencoded
InputStream got Line Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
InputStream got Line Referer: http://localhost:8080/asshjshshs
InputStream got Line Accept-Encoding: gzip, deflate, br
InputStream got Line Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
InputStream got Line
OutputStreamRun finished
InputStream got Line passwort=Bla&passwort2=Bla&passwort34=Bla&passwort55=Bla&submit=Testbutton
BufferedReader finished
我冲洗OutputDataStream之前所需要的线路如下:
passwort =布拉& passwort2 =布拉& passwort34 =布拉& passwort55 =布拉&提交= Testbutton
正在执行的浏览器是铬浏览器,但Internet Explorer以相同的方式生成输入流-.-
希望你能帮助我;)
答
好了 - 我才发现,这是按在HTML的公式推按钮后,工作流程:
获取连接,发送消息到与POST操作服务器,得到返回信息,关闭套接字
获取新连接,发送干净的消息到服务器没有发布消息,得到返回消息,显示为HTML代码。
是否有identificate客户到第二消息分配给第一一个独特的方式?
如果我可以分配它们,我可以通过转换第一条消息的Form-Data来在第二条消息中生成HTML-Output。
编辑:只有港是在8080和仅适用于Chrome工作
有人可以帮助我吗?:D
但那只是在浏览器浏览器,在Internet Explorer和边缘浏览器我总是得到1个电话的网站。 – Arol