2015-06-08 38 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; 
相关问题