Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java NanoHttpd:尝试提供文件时未找到文件_Java_Android_Network Programming_Android Networking_Nanohttpd - Fatal编程技术网

Java NanoHttpd:尝试提供文件时未找到文件

Java NanoHttpd:尝试提供文件时未找到文件,java,android,network-programming,android-networking,nanohttpd,Java,Android,Network Programming,Android Networking,Nanohttpd,我一直在尝试在Android上创建一个服务器,它可以像MP3一样存储文件。 我面临的挑战是,在播放MP3时,我会遇到文件未找到异常 下面是服务器类。 public class Server extends NanoHTTPD { private static final String MIME_AUDIO = "audio/mpeg"; private String URI; public Server(String uri,String type) throws

我一直在尝试在Android上创建一个服务器,它可以像MP3一样存储文件。 我面临的挑战是,在播放MP3时,我会遇到
文件未找到异常

下面是服务器类。

public class Server extends NanoHTTPD {

    private static final String MIME_AUDIO   = "audio/mpeg";
    private String URI;
    public Server(String uri,String type) throws IOException {
        super(8081);
        this.URI            = uri;
        start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
    }

    @Override
    public Response serve(IHTTPSession session) {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(Environment.getExternalStorageDirectory() + "/"+URI);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            try {
                int bytes   = 0;
                if(fis != null){
                    bytes   = fis.available();
                }
                return newFixedLengthResponse(Response.Status.OK, MIME_AUDIO, fis, bytes);
            } catch (IOException e) {
                e.printStackTrace();
            }
        return newFixedLengthResponse(Response.Status.OK, MIME_HTML, "Unknown Request");
    }
}
public void onActivityResult(int i, int resultCode, Intent intent) {
        super.onActivityResult(i, resultCode, intent);
        switch (i) {
            case 1013:
                if (resultCode == RESULT_OK) {
                    Uri uri = intent.getData();
                    try {
                        new Server(uri.toString(),"audio").start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
        }
    }
以下是选择文件后如何启动服务器。

public class Server extends NanoHTTPD {

    private static final String MIME_AUDIO   = "audio/mpeg";
    private String URI;
    public Server(String uri,String type) throws IOException {
        super(8081);
        this.URI            = uri;
        start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
    }

    @Override
    public Response serve(IHTTPSession session) {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(Environment.getExternalStorageDirectory() + "/"+URI);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            try {
                int bytes   = 0;
                if(fis != null){
                    bytes   = fis.available();
                }
                return newFixedLengthResponse(Response.Status.OK, MIME_AUDIO, fis, bytes);
            } catch (IOException e) {
                e.printStackTrace();
            }
        return newFixedLengthResponse(Response.Status.OK, MIME_HTML, "Unknown Request");
    }
}
public void onActivityResult(int i, int resultCode, Intent intent) {
        super.onActivityResult(i, resultCode, intent);
        switch (i) {
            case 1013:
                if (resultCode == RESULT_OK) {
                    Uri uri = intent.getData();
                    try {
                        new Server(uri.toString(),"audio").start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
        }
    }
我错过了什么