如何在Android中使用最新的NanoHTTPD 2.3.0提供mp3文件?

时间:2016-04-12 02:31:00

标签: android nanohttpd

我已阅读如何投放在使用NanoHTTPD(在Android内部)的SD卡上文件





代码返回新的NanoHTTPD.Response(Status.OK,“audio / mpeg”,fis在最新的NanoHTTPD 2.3.0中不再支持





我尝试用替换它来返回newFixedLengthResponse(fis),但这是不正确的。我怎样才能做到这一点?谢谢!




 公共类StackOverflowMp3Server扩展了NanoHTTPD {

 public StackOverflowMp3Server(){
超级(8089);
 }

 @越权#XA; public Response serve(String uri,Method method,
 Map< String,String> header,Map< String,String>参数,
 Map< String,String>文件){
 String answer =“”;

 FileInputStream fis = null;
试试{
 fis = new FileInputStream(Environment.getExternalStorageDirectory()
 +“/ music / musicfile.mp3”);
 } catch(FileNotFoundException e){
 // TODO自动生成的catch块
 e.printStackTrace();
 }
返回新的NanoHTTPD.Response(Status.OK,“audio / mpeg”,fis);
 }
}
  



3 个答案:

答案 0 :(得分:4)

您可以newChunkedResponse()使用InputStream方法,如下所示:

return newChunkedResponse(Status.OK, "audio/mpeg", fis);

答案 1 :(得分:0)

此代码工作正常(将您的声音放在资源文件夹中)

class SoundServer extends NanoHTTPD {

    SoundServer() {
        super(8089);
    }

    @Override
    public Response serve(IHTTPSession session) {
        InputStream myInput = null;
        try {
            myInput = mContext.getAssets().open("sound.mp3");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return createResponse(Response.Status.OK, "audio/mpeg", myInput);
    }

    //Announce that the file server accepts partial content requests
    private Response createResponse(Response.Status status, String mimeType,
                                    InputStream message) {
        return newChunkedResponse(status, mimeType, message);
    }
}

答案 2 :(得分:0)

此代码可用于服务来自nanohttpd Web服务器的任何类型的文件。

 @Override
public Response serve(IHTTPSession session) {
    String msg = "<html><body><h1>Hello server</h1></body></html>\n";
    String msgUnauthorized = "<html><body><h1>Unauthorized</h1></body></html>\n";
    Method method = session.getMethod();
    String uri = session.getUri();
    Map<String, String> files = new HashMap<>();
    Storage storage = new Storage(OpenRAP.getContext());
    String currentpath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/www/";
    String temp = Environment.getExternalStorageDirectory().getAbsolutePath() + "/www/temp/";
    String ecarpath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/www/ecars_files/";
    String xcontent = Environment.getExternalStorageDirectory().getAbsolutePath() + "/www/xcontent/";
    MainActivity.checkfiles();
    updateurl();
    String Endpoint = session.getUri();
    if (Endpoint.equals("/")) {

        String answer = "";
        try {
            // Open file from SD Card
            File root = Environment.getExternalStorageDirectory().getAbsoluteFile();
            FileReader index = new FileReader(root +
                    "/www/openrap/index.html");
            BufferedReader reader = new BufferedReader(index);
            String line = "";
            while ((line = reader.readLine()) != null) {
                answer += line;
            }
            reader.close();
        } catch (IOException ioe) {
            Log.w("Httpd", ioe.toString());
        }

        return newFixedLengthResponse(answer);
    }
    else if (uri.equals("/app-debug.apk")) {

        File root = Environment.getExternalStorageDirectory();
        FileInputStream fis = null;
        File file = new File(root.getAbsolutePath() + "/www/resources/app-debug.apk");
        String mime = NanoHTTPD.getMimeTypeForFile("app-debug.apk");
        Log.d("Path", root.getAbsolutePath());
        try {
            if (file.exists()) {
                fis = new FileInputStream(file);

            } else
                Log.d("FOF :", "File Not exists:");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


        return newFixedLengthResponse(Response.Status.OK, mime, fis, file.length());
    }

我在这里发送apk文件,您可以使用具有各自格式和文件名的任何类型的文件