Java Web服务器不打开php文件
我正在用java创建一个web服务器,它在端口80上侦听,工作正常,所有页面都按原样显示,但如果我使用php脚本的网页,它只会将php脚本显示为文本,请帮助我 这是我的web服务器的代码Java Web服务器不打开php文件,java,php,webserver,Java,Php,Webserver,我正在用java创建一个web服务器,它在端口80上侦听,工作正常,所有页面都按原样显示,但如果我使用php脚本的网页,它只会将php脚本显示为文本,请帮助我 这是我的web服务器的代码 import java.io.File; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class JHTTP extends Thread {
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class JHTTP extends Thread {
private File documentRootDirectory;
private String indexFileName = "index.html";
private ServerSocket server;
private int numThreads = 50;
public JHTTP(File documentRootDirectory, int port,
String indexFileName) throws IOException {
if (!documentRootDirectory.isDirectory( )) {
throw new IOException(documentRootDirectory
+ " does not exist as a directory");
}
this.documentRootDirectory = documentRootDirectory;
this.indexFileName = indexFileName;
this.server = new ServerSocket(port);
}
public JHTTP(File documentRootDirectory, int port) throws IOException {
this(documentRootDirectory, port, "index.html");
}
public JHTTP(File documentRootDirectory) throws IOException {
this(documentRootDirectory, 80, "index.html");
}
public void run( ) {
for (int i = 0; i < numThreads; i++) {
Thread t = new Thread(
new RequestProcessor(documentRootDirectory, indexFileName));
t.start( );
}
System.out.println("Accepting connections on port " + server.getLocalPort( ));
System.out.println("Document Root: " + documentRootDirectory);
while (true) {
try {
Socket request = server.accept( );
RequestProcessor.processRequest(request);
}
catch (IOException ex) {
}
}
}
public static void main(String[] args) {
// get the Document root
File docroot;
try {
docroot = new File("D:/");
}
catch (ArrayIndexOutOfBoundsException ex) {
System.out.println("Usage: java JHTTP docroot port indexfile");
return;
}
// set the port to listen on
int port;
try {
port = Integer.parseInt(args[1]);
if (port < 0 || port > 65535) port = 80;
}
catch (Exception ex) {
port = 80;
}
try {
JHTTP webserver = new JHTTP(docroot, port);
webserver.start( );
}
catch (IOException ex) {
System.out.println("Server could not start because of an "
+ ex.getClass( ));
System.out.println(ex);
}
}
}
导入java.io.File;
导入java.io.IOException;
导入java.net.ServerSocket;
导入java.net.Socket;
公共类JHTTP扩展线程{
私有文件文档根目录;
私有字符串indexFileName=“index.html”;
专用服务器套接字服务器;
私有int numThreads=50;
公共JHTTP(文件documentRootDirectory,int端口,
字符串indexFileName)引发IOException{
如果(!documentRootDirectory.isDirectory()){
抛出新IOException(documentRootDirectory
+“不作为目录存在”);
}
this.documentRootDirectory=documentRootDirectory;
this.indexFileName=indexFileName;
this.server=新服务器套接字(端口);
}
公共JHTTP(文件documentRootDirectory,int端口)引发IOException{
这(documentRootDirectory,端口,“index.html”);
}
公共JHTTP(文件documentRootDirectory)引发IOException{
这(documentRootDirectory,80,“index.html”);
}
公开作废运行(){
for(int i=0;i65535)端口=80;
}
捕获(例外情况除外){
端口=80;
}
试一试{
jhttpwebserver=新的JHTTP(docroot,端口);
start();
}
捕获(IOEX异常){
System.out.println(“由于出现错误,服务器无法启动”
+例如getClass());
系统输出打印项次(ex);
}
}
}
请求处理器:
import java.net.*;
import java.io.*;
import java.util.*;
public class RequestProcessor implements Runnable {
@SuppressWarnings("rawtypes")
private static List pool = new LinkedList( );
private File documentRootDirectory;
private String indexFileName = "index.html";
public RequestProcessor(File documentRootDirectory,
String indexFileName) {
if (documentRootDirectory.isFile( )) {
throw new IllegalArgumentException(
"documentRootDirectory must be a directory, not a file");
}
this.documentRootDirectory = documentRootDirectory;
try {
this.documentRootDirectory
= documentRootDirectory.getCanonicalFile( );
}
catch (IOException ex) {
}
if (indexFileName != null) this.indexFileName = indexFileName;
}
@SuppressWarnings("unchecked")
public static void processRequest(Socket request) {
synchronized (pool) {
pool.add(pool.size( ), request);
pool.notifyAll( );
}
}
public void run( ) {
// for security checks
String root = documentRootDirectory.getPath( );
while (true) {
Socket connection;
synchronized (pool) {
while (pool.isEmpty( )) {
try {
pool.wait( );
}
catch (InterruptedException ex) {
}
}
connection = (Socket) pool.remove(0);
}
try {
String filename;
String contentType;
OutputStream raw = new BufferedOutputStream(
connection.getOutputStream( )
);
Writer out = new OutputStreamWriter(raw);
Reader in = new InputStreamReader(
new BufferedInputStream(
connection.getInputStream( )
),"ASCII"
);
StringBuffer requestLine = new StringBuffer( );
int c;
while (true) {
c = in.read( );
if (c == '\r' || c == '\n') break;
requestLine.append((char) c);
}
String get = requestLine.toString( );
// log the request
System.out.println(get);
StringTokenizer st = new StringTokenizer(get);
String method = st.nextToken( );
String version = "";
if (method.equals("GET")) {
filename = st.nextToken( );
if (filename.endsWith("/")) filename += indexFileName;
contentType = guessContentTypeFromName(filename);
if (st.hasMoreTokens( )) {
version = st.nextToken( );
}
File theFile = new File(documentRootDirectory,
filename.substring(1,filename.length( )));
if (theFile.canRead( )
// Don't let clients outside the document root
&& theFile.getCanonicalPath( ).startsWith(root)) {
DataInputStream fis = new DataInputStream(
new BufferedInputStream(
new FileInputStream(theFile)
)
);
byte[] theData = new byte[(int) theFile.length( )];
fis.readFully(theData);
fis.close( );
if (version.startsWith("HTTP ")) { // send a MIME header
out.write("HTTP/1.0 200 OK\r\n");
Date now = new Date( );
out.write("Date: " + now + "\r\n");
out.write("Server: JHTTP/1.0\r\n");
out.write("Content-length: " + theData.length + "\r\n");
out.write("Content-type: " + contentType + "\r\n\r\n");
out.flush( );
} // end if
// send the file; it may be an image or other binary data
// so use the underlying output stream
// instead of the writer
raw.write(theData);
raw.flush( );
} // end if
else { // can't find the file
if (version.startsWith("HTTP ")) { // send a MIME header
out.write("HTTP/1.0 404 File Not Found\r\n");
Date now = new Date( );
out.write("Date: " + now + "\r\n");
out.write("Server: JHTTP/1.0\r\n");
out.write("Content-type: text/html\r\n\r\n");
}
out.write("<HTML>\r\n");
out.write("<HEAD><TITLE>File Not Found</TITLE>\r\n");
out.write("</HEAD>\r\n");
out.write("<BODY>");
out.write("<H1>HTTP Error 404: File Not Found</H1>\r\n");
out.write("</BODY></HTML>\r\n");
out.flush( );
}
}
else { // method does not equal "GET"
if (version.startsWith("HTTP ")) { // send a MIME header
out.write("HTTP/1.0 501 Not Implemented\r\n");
Date now = new Date( );
out.write("Date: " + now + "\r\n");
out.write("Server: JHTTP 1.0\r\n");
out.write("Content-type: text/html\r\n\r\n");
}
out.write("<HTML>\r\n");
out.write("<HEAD><TITLE>Not Implemented</TITLE>\r\n");
out.write("</HEAD>\r\n");
out.write("<BODY>");
out.write("<H1>HTTP Error 501: Not Implemented</H1>\r\n");
out.write("</BODY></HTML>\r\n");
out.flush( );
}
}
catch (IOException ex) {
}
finally {
try {
connection.close( );
}
catch (IOException ex) {}
}
} // end while
} // end run
public static String guessContentTypeFromName(String name) {
if (name.endsWith(".html") || name.endsWith(".htm")) {
return "text/html";
}
else if (name.endsWith(".txt") || name.endsWith(".java")) {
return "text/plain";
}
else if (name.endsWith(".gif")) {
return "image/gif";
}
else if (name.endsWith(".class")) {
return "application/octet-stream";
}
else if (name.endsWith(".jpg") || name.endsWith(".jpeg")) {
return "image/jpeg";
}
else if (name.endsWith(".png") ) {
return "image/png";
}
else if (name.endsWith(".js")) {
return "text/javascript";
}
else if (name.endsWith(".js")) {
return "text/javascript";
}
else if (name.endsWith(".css")) {
return "text/css";
}
else return "text/plain";
}
} // end RequestProcessor
import java.net.*;
导入java.io.*;
导入java.util.*;
公共类RequestProcessor实现可运行{
@抑制警告(“原始类型”)
私有静态列表池=新的LinkedList();
私有文件文档根目录;
私有字符串indexFileName=“index.html”;
公共请求处理器(文件documentRootDirectory,
字符串索引(文件名){
if(documentRootDirectory.isFile()){
抛出新的IllegalArgumentException(
“documentRootDirectory必须是目录,而不是文件”);
}
this.documentRootDirectory=documentRootDirectory;
试一试{
这个.documentRootDirectory
=documentRootDirectory.getCanonicalFile();
}
捕获(IOEX异常){
}
如果(indexFileName!=null)this.indexFileName=indexFileName;
}
@抑制警告(“未选中”)
公共静态void processRequest(套接字请求){
已同步(池){
pool.add(pool.size(),request);
notifyAll();
}
}
公开作废运行(){
//进行安全检查
String root=documentRootDirectory.getPath();
while(true){
插座连接;
已同步(池){
while(pool.isEmpty()){
试一试{
pool.wait();
}
捕获(中断异常例外){
}
}
连接=(套接字)池。删除(0);
}
试一试{
字符串文件名;
字符串内容类型;
OutputStream原始=新的BufferedOutputStream(
connection.getOutputStream()
);
Writer out=新的OutputStreamWriter(原始);
Reader in=新的InputStreamReader(
新的BufferedInputStream(
connection.getInputStream()
),“ASCII”
);
StringBuffer requestLine=新的StringBuffer();
INTC;
while(true){
c=in.read();
如果(c=='\r'| c=='\n')中断;
append((char)c);
}
字符串get=requestLine.toString();
//记录请求
System.out.println(get);
StringTokenizer st=新的StringTokenizer(get);
String方法=st.nextToken();
字符串版本=”;
if(method.equals(“GET”)){
filename=st.nextToken();
如果(filename.endsWith(“/”)filename+=indexFileName;
contentType=guessContentTypeFromName(文件名);
if(st.hasMoreTokens()){
version=st.nextToken();
}
File theFile=新文件(documentRootDirectory,
filename.substring(1,filename.length());
if(文件的可读取性)
//不要让客户端位于文档根目录之外
&&theFile.getCanonicalPath().startsWith(根)){
DataInputStream fis=新的DataInputStream(
新的BufferedInputStream(
新文件输入流(文件)
)
);
byte[]theData=新字节[(int)theFile.length()];
财务报表(数据);
fis.close();
if(version.startsWith(“HTTP”){//发送MIME头
out.write(“HTTP/1.0 200 OK\r\n”);
现在日期=新日期();
out.write(“日期:“+now+”\r\n”);
out.write(“服务器:JHTTP/1.0\r\n”);
out.write(“内容长度:“+theData.length+”\r\n”);
out.write(“内容类型:“+contentType+”\r\n\r\n”);
out.flush();
}//如果结束,则结束
//发送文件;它可能是图像或其他二进制数据