2014-05-12 22 views
0

使用下面的代碼,我能夠使用Nanohttpd輕量級服務器在android手機上創建移動服務器。該代碼基本上循環通過主機android設備的根目錄並列出文件和文件夾作爲鏈接。我想要實現的是,當用戶點擊任何鏈接(文件夾鏈接)時,瀏覽器應顯示包含在點擊文件夾鏈接中的文件和文件夾。我如何做到這一點,因爲我找不到任何Nanohttpd文檔的初學者。使用Android Nanohttpd輕量級服務器的文件目錄導航

import java.io.File; 
import java.util.Map; 

import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.os.Bundle; 
import android.os.Environment; 
import android.os.Handler; 
import android.widget.TextView; 

public class MainActivity extends Activity { 
    private static final int PORT = 8080; 
    private TextView hello; 
    private WebServer server; 
    private Handler handler = new Handler(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     hello = (TextView) findViewById(R.id.hello); 
    } 

    /* 
    * There are some earlier versions of android that can not implement this 
    * method of getting IP address and isEmpty() method. The 
    * 
    * @SupreesLint("NewAPI") helps to suppress the error that will arise in 
    * such devices when implementing these methods . For the application 
    * however, a minimum version of API that can be able to execute the 
    * application flawlessly is set. The enables error checking as lower 
    * version that can not implement this methods wouldn't be able to install 
    * the application. 
    */ 
    @SuppressLint("NewApi") 
    @Override 
    protected void onResume() { 
     super.onResume(); 

     TextView textIpaddr = (TextView) findViewById(R.id.ipaddr); 
     if (Utils.getIPAddress(true).trim().isEmpty()) { 
      textIpaddr.setText(Utils.getIPAddress(false) + ":" + PORT); 
     } else { 
      textIpaddr.setText(Utils.getIPAddress(true) + ":" + PORT); 
     } 

     try { 
      server = new WebServer(); 
      server.start(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public String intToIp(int i) { 

     return ((i >> 24) & 0xFF) + "." + ((i >> 16) & 0xFF) + "." 
       + ((i >> 8) & 0xFF) + "." + (i & 0xFF); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     if (server != null) 
      server.stop(); 
    } 

    private class WebServer extends NanoHTTPD { 

     public WebServer() { 
      super(8080); 
     } 

     @Override 
     public Response serve(String uri, Method method, 
       Map<String, String> header, Map<String, String> parameters, 
       Map<String, String> files) { 
      File rootDir = Environment.getExternalStorageDirectory(); 
      File[] files2 = rootDir.listFiles(); 
      String answer = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"><title>sdcard0 - TECNO P5 - WiFi File Transfer Pro</title>"; 
      for (File detailsOfFiles : files2) { 
       answer += "<a href=\"" + detailsOfFiles.getAbsolutePath() 
         + "\" alt = \"\">" + detailsOfFiles.getAbsolutePath() 
         + "</a><br>"; 
      } 
      answer += "</head></html>"; 
      return new NanoHTTPD.Response(answer); 
     } 



    } 

} 

Here is how the output looks like in my browser

+0

您是否只想將文件夾名稱作爲鏈接? –

+0

如圖所示,如果任何鏈接應該被點擊,如果它是一個文件夾,則應打開一個新頁面,顯示包含在被點擊文件夾中的文件和子文件夾。 –

回答

2

我終於弄清楚如何學習下面的NanoHTTPD Framework.The代碼足夠的時間後,這樣做可以幫助我在主機Android設備的目錄中導航:

@Override 
     public Response serve(String uri, Method method, 
       Map<String, String> header, Map<String, String> parameters, 
       Map<String, String> files) { 
      File rootDir = Environment.getExternalStorageDirectory(); 
      File[] filesList = null; 
      String filepath = ""; 
      if (uri.trim().isEmpty()) { 
       filesList = rootDir.listFiles(); 
      } else { 
       filepath = uri.trim(); 
      } 
      filesList = new File(filepath).listFiles(); 
      String answer = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"><title>sdcard0 - TECNO P5 - WiFi File Transfer Pro</title>"; 
      if (new File(filepath).isDirectory()) { 
       for (File detailsOfFiles : filesList) { 
        answer += "<a href=\"" + detailsOfFiles.getAbsolutePath() 
          + "\" alt = \"\">" 
          + detailsOfFiles.getAbsolutePath() + "</a><br>"; 
       } 
      } else { 
      } 
      answer += "</head></html>" + "uri: " + uri + " \nfiles " + files 
        + " \nparameters " + parameters + " \nheader "; 
      return new NanoHTTPD.Response(answer); 
     } 

在響應中的URI參數方法包含該時間點的瀏覽器網址: 如果地址欄上顯示的網址爲:/192.168.43.1:8080/storage/sdcard1/Smadav_2012_Rev._9.0,則uri包含/storage/sdcard1/Smadav_2012_Rev ._9.0。我所做的只是將uri作爲文件路徑傳遞,當然,當uri爲空時,首次連接並不是這樣。

2

你在第一個參數得到URI。因此將其附加到打開指定目錄的路徑。 如果您要求192.168.1.6:8080/ABC程序將在外部目錄中查找ABC文件夾。

然後當檢查採取的項目是文件還是目錄&根據我們改變我們的輸出。使用

.isFile() 

下面是代碼應工作:

.... 
public Response serve(String uri, Method method, 
       Map<String, String> header, Map<String, String> parameters, 
       Map<String, String> files) { 

      File rootDir = new File(Environment.getExternalStorageDirectory() + File.separator + uri); 
      File[] files2 = rootDir.listFiles(); 
      String answer = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"><title>sdcard0 - TECNO P5 - WiFi File Transfer Pro</title>"; 
      for (File detailsOfFiles : files2) { 
       if(detailsOfFiles.isFile()){ 
         answer += detailsOfFiles.getAbsolutePath() + "<br>"; 
       }else{ 
       answer += "<a href=\"" + detailsOfFiles.getAbsolutePath() 
         + "\" alt = \"\">" + detailsOfFiles.getAbsolutePath() 
         + "</a><br>"; 
         } 
      } 
      answer += "</head></html>"; 
      return new NanoHTTPD.Response(answer); 
} 
... 

對不起,我不好解釋。