简体   繁体   English

使用 Java 托管 MP3 文件

[英]Hosting MP3 Files with Java

So I am trying to emulate something like what this site has set up:所以我试图模仿这个网站已经设置的东西:

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

Where when a browser like chrome goes to this exact url, basically a player appears and you are able to 'stream' the music.当像 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();
        }
    }

}

I am trying to emulate this with some simple java code.我正在尝试用一些简单的 java 代码来模拟这一点。 Where I am struggling as figuring out how I should format the request to appear in this way.我正在努力弄清楚我应该如何格式化以这种方式出现的请求。 Is there a way to send a local file stored on my drive to the request?有没有办法将存储在我的驱动器上的本地文件发送到请求? I have been struggling to find an example of how to do this我一直在努力寻找如何做到这一点的例子

You can use a FileInputStream to read the bytes of your file to send to the browser:您可以使用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();
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM