2015-06-08 39 views
1

我需要創建一個本地服務器,它能夠爲設備的本地文件提供服務。我發現這library,它似乎能夠滿足我的需求。使用NanoHttpd在本地服務器上提供圖像

我已經嘗試過這個項目中的示例,它工作正常!但它張貼.html頁面,我需要圖片。

我跟着這個post其中.mp3被送達。但是這對我不起作用。可能是因爲圖書館從那時起更新了。

@Override 
public Response serve(IHTTPSession session) { 

    FileInputStream fis = null; 
    try { 
     File file = new File(Environment.getExternalStoragePublicDirectory(ROOT) 
       + PATH + "picture.jpg"); //path exists and its correct 
     fis = new FileInputStream(file); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return new NanoHTTPD.Response(Response.Status.OK, "image/jpeg", fis, 1000000); //the last parameter is totalBytes. Not sure what to put there 
} 

服務器中被啓動的onCreate:

//start web server 
    try { 
     server = new WebServer(); 
     server.start(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

目的是訪問這樣的文件:192.168.1.2:8080/picture.jpg

任何人都可以提出解決方案?提前致謝。

+0

開始。一個與uri和參數。就像您提供的那篇文章的鏈接一樣。然後你可以提取'picture.jpg。第一個或其他文件名。而你沒有告訴你什麼不適合你。 '提前致謝.'非常錯誤。如果你得到了很好的幫助,你應該承諾感謝。 – greenapps

+0

@greenapps,帶參數的'serve()'已棄用。無論如何,我已經嘗試過了。他們中的任何一個都不像提供文件。我無法通過ip訪問它們。屏幕上沒有顯示任何內容 – AnZ

+0

您的代碼應該沒問題,但要刪除一個參數:'1000000); //最後一個參數是totalBytes。' – greenapps

回答

0

所以我已經下載previous version of NanoHttpd,那對我有效。很可能開發人員在新版本中改變了行爲。我沒有時間在問題內部挖掘。

這對我有效。

@Override 
public Response serve(String uri, Method method, Map<String, String> header, Map<String, String> parms, Map<String, String> files) { 
    FileInputStream fis = null; 
    try { 
     File file = new File(Environment.getExternalStoragePublicDirectory(ROOT) 
       + PATH + "picture.jpg"); 

     if (file.exists()){ 
      fis = new FileInputStream(file); 
      Log.d(TAG, "File exists: " + file.getAbsolutePath()); 
     } else { 
      Log.d(TAG, "File doesn't exist!"); 
     } 

    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    return new NanoHTTPD.Response(Response.Status.OK, "image/jpeg", fis); 
} 

這實際上並不是一個正確的答案,因爲它不能解決新版本的問題。

1

嘗試這一個,如果你不知道文件的總大小,那麼你必須給-T

@Override 
public Response serve(IHTTPSession session) { 
    FileInputStream fis = null; 
    try { 
     File file = new File(Environment.getExternalStoragePublicDirectory(ROOT) 
      + PATH + "picture.jpg"); //path exists and its correct 
     fis = new FileInputStream(file); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return new NanoHTTPD.Response(Response.Status.OK, "image/jpeg", fis, -1); //the last parameter is totalBytes. Not sure what to put there 
} 

編輯: 爲響應正確的參數是:

return new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, "image/jpeg", fis, file.length()); 

最後一個參數是大小FileInputStream.

1

我對這個答案感到非常興奮。但是,據我所知,最後一個參數是文件大小(以(長)字節爲單位)。因此,您可以創建新的整數並對其進行初始化try塊內,初始化後FIS,如下所示:

size=fis.available(); // returns available size of file 

,然後設置大小到最後一個參數。

+0

這看起來比我上面的編輯好。 Sherzodbek佔用了fis的大小;這比文件的大小更正確。 (雖然它應該是相同的) –

0

在新版本響應受保護。

我用這個下載APK文件,我想這會爲圖像的工作了:使用不同的服務()方法

File f = new File("apk/app-release.apk"); 
FileInputStream fis = new FileInputStream(f); 
Response res = newChunkedResponse(Response.Status.OK, "application/vnd.android.package-archive", fis); 
res.addHeader("Content-Disposition", "attachment; filename=\"" + f.getName() + "\""); 
return res; 
相關問題