java web 写服务器代码后用浏览器打开html出现的是html的源代码
packagecjm.itest;importjava.io.FileInputStream;importjava.io.InputStream;importjava.i...
package cjm.itest;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(4567, 30,
InetAddress.getByName("localhost"));
Socket socket = server.accept();
OutputStream out = socket.getOutputStream();
InputStream is = new FileInputStream("1.html");
byte[] buf = new byte[1024];
int len = -1;
while ((len = is.read(buf)) > 0) {
out.write(buf, 0, len);
}
is.close();
out.close();
socket.close();
server.close();
}
}
运行后如图。。 展开
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(4567, 30,
InetAddress.getByName("localhost"));
Socket socket = server.accept();
OutputStream out = socket.getOutputStream();
InputStream is = new FileInputStream("1.html");
byte[] buf = new byte[1024];
int len = -1;
while ((len = is.read(buf)) > 0) {
out.write(buf, 0, len);
}
is.close();
out.close();
socket.close();
server.close();
}
}
运行后如图。。 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询