【问题标题】:Why images and style files couldn't found on NanoHTTPD为什么在 NanoHTTPD 上找不到图像和样式文件
【发布时间】:2013-06-14 07:08:30
【问题描述】:

问题:我正在使用NanoHTTPD。它运行良好,但不提供 .js 文件、图像和其他文件。

详细说明:我在 assets 文件夹中有一个 pages 文件夹。此文件夹包含index.html、css 文件、图像和其他文件。我正在使用这样的 NanoHTTPD,但是当我使用浏览器浏览时,没有任何样式或图像。服务器找不到图像和其他文件。只有 index.html 文件的内容。 活动:

MyHTTPD server = null;
        try {
            server = new MyHTTPD(getApplicationContext());
            try
               {
                   server.start();
               }
               catch( IOException ioe )
               {
                   System.err.println( "Couldn't start server:\n" + ioe );
                   System.exit( -1 );
               }
               System.out.println( "Listening on port 8080. Hit Enter to stop.\n" );
               try { System.in.read(); } catch( Throwable t ) {
                   System.out.println("read error");
               };
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

MyHTTPD 类

public Context ctx = null;
    /**
    * Constructs an HTTP server on given port.
    */
   public MyHTTPD(Context ctx) throws IOException {
       super(8080);
       this.ctx = ctx;
   }


@Override
   public Response serve( String uri, Method method,
           Map<String, String> header, Map<String, String> parms,
           Map<String, String> files )
           {
            String html = null;
            InputStream is = null;
            try {
                is = ctx.getAssets().open("pages/index.html");
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            byte[] b;
            try {
                b = new byte[is.available()];
                is.read(b);
                html = new String(b);
            } catch (IOException e) { // TODO Auto-generated catch block
                e.printStackTrace();
            }
               return new NanoHTTPD.Response(html);
           }

注意:我已阅读此问题(和答案): Using NanoHTTPD in Android file uploading error nanohttpd How to create nanohttpd server in android?

【问题讨论】:

    标签: android nanohttpd


    【解决方案1】:

    在我的 serve() 方法中,它看起来像这样:

                     @Override
                public Response serve(String uri, String method, Properties header, Properties parms, Properties files) {
                           Log.d(TAG,"SERVE ::  URI "+uri);
                  final StringBuilder buf = new StringBuilder();
                  for (Entry<Object, Object> kv : header.entrySet())
                    buf.append(kv.getKey() + " : " + kv.getValue() + "\n");
                  InputStream mbuffer = null;
    
    
    
                                try { 
                                        if(uri!=null){
    
                                           if(uri.contains(".js")){
                                                   mbuffer = mContext.getAssets().open(uri.substring(1));
                                                   return new NanoHTTPD.Response(HTTP_OK, MIME_JS, mbuffer);
                                           }else if(uri.contains(".css")){
                                                   mbuffer = mContext.getAssets().open(uri.substring(1));
                                                   return new NanoHTTPD.Response(HTTP_OK, MIME_CSS, mbuffer);
    
                                           }else if(uri.contains(".png")){
                                                   mbuffer = mContext.getAssets().open(uri.substring(1));      
                                                   // HTTP_OK = "200 OK" or HTTP_OK = Status.OK;(check comments)
                                                   return new NanoHTTPD.Response(HTTP_OK, MIME_PNG, mbuffer);
                                           }else if (uri.contains("/mnt/sdcard")){
                                                   Log.d(TAG,"request for media on sdCard "+uri);
                                                   File request = new File(uri);
                                                   mbuffer = new FileInputStream(request);
                                                   FileNameMap fileNameMap = URLConnection.getFileNameMap();
                                                   String mimeType = fileNameMap.getContentTypeFor(uri);
    
                                                   Response streamResponse = new Response(HTTP_OK, mimeType, mbuffer);
                                                   Random rnd = new Random();
                                    String etag = Integer.toHexString( rnd.nextInt() );
                                    streamResponse.addHeader( "ETag", etag);
                                                   streamResponse.addHeader( "Connection", "Keep-alive");
    
    
    
    
    
    
                                                   return streamResponse;
                                           }else{
                                                   mbuffer = mContext.getAssets().open("index.html");
                                                   return new NanoHTTPD.Response(HTTP_OK, MIME_HTML, mbuffer);
                                           }
                                        }
    
                                } catch (IOException e) {
                                        Log.d(TAG,"Error opening file"+uri.substring(1));
                                        e.printStackTrace();
                                }
    
                          return null;
    
                }
    

    对于 mime 类型,有一些不太干净的解决方案。验证应该使用类似 Getting A File's Mime Type In Java 的东西来完成,我的简单项目只是检查几种 mime。
    引用 mime 类型是 NanoHTTPD 类中的静态字段:

           /**
         * Common mime types for dynamic content
         */
        public static final String
                MIME_PLAINTEXT = "text/plain",
                MIME_HTML = "text/html",
                MIME_JS = "application/javascript",
                MIME_CSS = "text/css",
                MIME_PNG = "image/png",
                MIME_DEFAULT_BINARY = "application/octet-stream",
                MIME_XML = "text/xml";
    

    通过这个实现,我能够从资产和外部存储器中读取文件。

    【讨论】:

    • HTTP_OK 的值是多少? (int) 200 ?我收到isStreaming cannot be resolved or is not a field 错误。
    • 它也在 NanoHTTPD 类 'HTTP_OK = "200 OK"' 中,
    • 但它不是字符串。它的类型,状态。我只是试试这个,现在似乎工作了Status HTTP_OK = Status.OK;我还有一个问题,你可以查看我的最后一次编辑以获取我的第一条评论。
    • 也许您使用的是不同的方法或 NanoHTTPD 版本?我正在使用这个字符串字段。根据“isStreaming”字段,我认为您可以评论这一行(或删除)这是我记得的一些特殊要求 - 我将编辑我的答案
    • 你解决了我的问题!谢谢你。只需稍加修改,即可添加 Status HTTP_OK = Status.OK; 。再次感谢您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-04
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-17
    • 2018-01-28
    相关资源
    最近更新 更多