【问题标题】:Hosting MP3 Files with Java使用 Java 托管 MP3 文件
【发布时间】:2019-04-30 20:39:52
【问题描述】:

所以我试图模仿这个网站的设置:

http://www.ntonyx.com/mp3files/Morning_Flower.mp3

当像 chrome 这样的浏览器访问这个确切的 url 时,基本上会出现一个播放器,您可以“流式传输”音乐。

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class WebServer {

    public static void main(String[] args) throws Exception {
        HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
        server.createContext("/test/file.mp3", new MyHandler());
        server.setExecutor(null); // creates a default executor
        server.start();
    }

    static class MyHandler implements HttpHandler {
        @Override
        public void handle(HttpExchange t) throws IOException {
            String response = "This is the response";
            t.sendResponseHeaders(200, response.length());
            OutputStream os = t.getResponseBody();
            os.write(response.getBytes());
            os.close();
        }
    }

}

我正在尝试使用一些简单的 java 代码来模拟这一点。我正在努力弄清楚我应该如何格式化以这种方式出现的请求。有没有办法将存储在我的驱动器上的本地文件发送到请求?我一直在努力寻找如何做到这一点的例子

【问题讨论】:

  • 您可以将 mp3 文件的字节转储到流中。然后大多数浏览器会缓冲它以供播放。
  • 天哪,我不知道该怎么做

标签: java http mp3


【解决方案1】:

您可以使用FileInputStream 读取文件的字节以发送到浏览器:

import java.io.IOException;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.net.InetSocketAddress;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class WebServer {

    public static void main(String[] args) throws Exception {
        HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
        server.createContext("/test/file.mp3", new MyHandler());
        server.setExecutor(null); // creates a default executor
        server.start();
    }

    static class MyHandler implements HttpHandler {
        @Override
        public void handle(HttpExchange t) throws IOException {

            File file = new File("path/to/file.mp3");                 // Create a new File object pointing to your mp3 file   

            /* https://stackoverflow.com/questions/38679686/ :) */   
            t.getResponseHeaders().put("Content-Type", "audio/mpeg"); // Make sure the browser knows this is an audio file
            t.sendResponseHeaders(200, file.length());                // Send the length of the file to the browser

            FileInputStream stream = new FileInputStream(file);       // Open an InputStream to read your file
            OutputStream os = t.getResponseBody();
            byte[] buff = new byte[1024];                             // Create a small buffer to hold bytes as you read them
            int read = 0;                                             // Keep track of how many bytes you read

            // While there are still bytes to read, send them to the client
            while((read = stream.read(buff)) > 0) {
                os.write(buff, 0, read);
            }
            // Close the streams
            os.close();
            stream.close();
        }
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    • 2012-08-30
    • 1970-01-01
    • 2020-04-01
    • 2020-08-16
    相关资源
    最近更新 更多