Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.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 IOException:使用HttpHandler服务mp3的管道破裂。仅适用于Android浏览器_Java_Android_Mp3_Httphandler - Fatal编程技术网

java IOException:使用HttpHandler服务mp3的管道破裂。仅适用于Android浏览器

java IOException:使用HttpHandler服务mp3的管道破裂。仅适用于Android浏览器,java,android,mp3,httphandler,Java,Android,Mp3,Httphandler,我正试图用HttpHandler播放mp3,却收到了一根断了的管子。它在我的mac和iPad上与Google Chrome一起工作,但Android导致HttpHander在获得IOException后挂起,我必须重新启动。使用非常简单的代码,可以很好地处理图像和html try { String requestURI = t.getRequestURI().toString().substring(1); if(requestUR

我正试图用HttpHandler播放mp3,却收到了一根断了的管子。它在我的mac和iPad上与Google Chrome一起工作,但Android导致HttpHander在获得IOException后挂起,我必须重新启动。使用非常简单的代码,可以很好地处理图像和html

       try {
            String requestURI = t.getRequestURI().toString().substring(1);

            if(requestURI.equals("") || requestURI.equals("/"))
                requestURI = "index.htm";

            requestURI = requestURI.replaceAll("%20", " ");

            if(requestURI.contains("mp3")) {
                urlToResource = new File(System.getProperty("user.home") + "/test/" +
                        requestURI).toURI().toURL();
            }

            System.out.println("Modified requestURI:" + requestURI);

            if(requestURI.contains("mp3")) {
                sContentType = "audio/mpeg";
            } else if(requestURI.contains("png")) {
                sContentType = "image/png";
            } else if(requestURI.contains("jpg")) {
                sContentType = "image/jpg";
            } else if(requestURI.contains("favicon.ico")) {
                sContentType = "content/unknown";
            } else if(requestURI.contains("css")) {
                sContentType = "text/css";
            } else {
                sContentType = "text/html";
            }

            if(!requestURI.contains("mp3")) {
                urlToResource = new File("src/com/daford/web/" + requestURI).toURI().toURL();
            }

            if(urlToResource != null) {
                conn = urlToResource.openConnection();
                int size = conn.getContentLength();

                System.out.println("file " + requestURI + " size is:" + size);

                inConnectionReader = conn.getInputStream();
                headers = t.getResponseHeaders();
                headers.add("Content-Type", sContentType);
                t.sendResponseHeaders(200, size);
                os = t.getResponseBody();
                int iReadByte = inConnectionReader.read();
                while (iReadByte != -1) {
                    os.write(iReadByte);
                    iReadByte = inConnectionReader.read();
                }
            } else {
                headers = t.getResponseHeaders();
                headers.add("Content-Type", "text/html");
                String sErrorMessage = "Error getting webpage.";
                t.sendResponseHeaders(404, sErrorMessage.length());
                os = t.getResponseBody();
                os.write(sErrorMessage.getBytes());
            }

            if(os != null) {
                os.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }