当前位置: 首页 > 面试题库 >

Firefox不支持Android Web服务器

梁丘安晏
2023-03-14
问题内容

我已经在Android中创建了一个网络服务器,该服务器绑定到端口2090,并为Android设备的sdcard服务,如果ip:port/index.htm在Internet
Explorer中键入,则可以正常工作,但效果很好,但是当我在其他浏览器中打开它时,它会以文本形式打开HTML文件请帮我。

这是我的代码。

服务器启动器类:

package dolphin.developers.com;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import dolphin.devlopers.com.R;



public class JHTTS extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);

        setContentView(R.layout.facebook);

        try{

             File documentRootDirectory = new File (Environment.getExternalStorageDirectory().getAbsolutePath()+"/");
            JHTTP j = new JHTTP(documentRootDirectory,2090);
            j.start();


            Log.d("Server Rooot", ""+documentRootDirectory);


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace(); 
        }    
    }
}

jhttp类:

package dolphin.developers.com;

import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;

public class JHTTP extends Thread {



          private File documentRootDirectory;
          private  String indexFileName = "index.htm";
          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.htm");
   }

        public JHTTP(File documentRootDirectory) throws IOException {

                  this(documentRootDirectory, 9001, "index.htm");
  }

        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 (SocketException ex) {

         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
 }       
 }   
}

RequestProcessor:

package dolphin.developers.com;

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.htm";

           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 return "text/plain";

   }


   } // end RequestProcessor

Logcat:

没有发布,因为没有错误。


问题答案:

问题解决了,我更换了生产线

else return "text/plain";

else return "text/html";

实际上问题出在MIME类型中,谢谢大家的帮助。



 类似资料:
  • 我的Spring Boot初始化遇到了麻烦。我在一个简单的Spring Boot项目中有这个结构。

  • 我无法在远程GlassFish服务器(4.0)上连接/部署/运行任何应用程序,并且已经为此奋斗了几天。 在本地,我运行的是Windows8,我也尝试过使用NetBeans7.3和8.0。我在本地安装了Java7.25和Java8。远程GF服务器是版本4.0 build 89,在多主机Windows 8计算机上,Java 7 25,在Glassfish中启用远程管理。我还让HTTP在8888端口而不

  • 我们继续上一章节的内容,大家应该记得我们 Lua 代码中是如何完成 ngx_postgres 模块调用的。我们把他简单改造一下,让他更接近真实代码。 local json = require "cjson" function db_exec(sql_str) local res = ngx.location.capture('/postgres',

  • 本文列出了Apache HTTP服务器中所有的可执行程序。 索引 httpd Apache超文本传输协议服务器 apachectl Apache HTTP服务器控制接口 ab Apache HTTP服务器性能测试工具 apxs APache功能扩展工具 configure 配置源代码树 dbmmanage 建立和更新DBM形式的基本认证文件 htcacheclean 清理磁盘缓冲区 htdiges

  • 我是K8s的新手,所以仍然试图让我的头脑周围的事情。我一直在研究部署,并能够理解它们将有多有用。但是,我不明白为什么它们不支持服务(只支持副本集和豆荚)。

  • 与我之前的问题相关,接下来我尝试在Firefox for Android82上使用SharedArrayBuffer。 谢了。