gpt4 book ai didi

rest - Jersey REST 支持简历/媒体流

转载 作者:行者123 更新时间:2023-12-03 14:43:29 24 4
gpt4 key购买 nike

我需要在 Jersey REST 上支持简历,我正在尝试这样做:

@Path("/helloworld")
public class RestServer {

@GET

@Path("say")
@Produces("audio/mp3")
public Response getMessage(@HeaderParam("Range") String r ) throws IOException{
String str="/Users/dima/Music/crazy_town_-_butterfly.mp3";

System.out.println(r);
RandomAccessFile f=new RandomAccessFile(str, "r");

int off=0;
int to=(int)f.length();
byte[] data ;
if(r!=null){
String from=r.split("=")[1].split("-")[0];
String t=r.split("=")[1].split("-")[1];
off=Integer.parseInt(from);
to=Integer.parseInt(t);

}
data= new byte[to-off];
f.readFully(data, off, to-off);

ResponseBuilder res=Response.ok(data)
.header("Accept-Ranges","bytes")
.header("Content-Range:", "bytes "+off+"-"+to+"/"+data.length)
.header("Pragma", "no-cache");;

if(r==null){
res=res.header("Content-Length", data.length);
}
f.close();

Response ans=res.build();

return ans;


}
}

我希望能够流式传输 mp3,以便浏览器可以寻找音乐,但在 safari 中它仍然无法正常工作。有任何想法吗?

最佳答案

这是我基于提供的解决方案的看法 here .它可以在不同的浏览器上正常工作。我也可以在 Safari 和其他浏览器中很好地寻找音乐。你可以在我的 Github 上找到示例项目 repository其中有更多细节。 Chrome 和 Safari 很好地利用范围 header 来流式传输媒体,您可以在请求/响应跟踪中看到它。

    @GET
@Produces("audio/mp3")
public Response streamAudio(@HeaderParam("Range") String range) throws Exception {
return buildStream(audio, range);
}

private Response buildStream(final File asset, final String range) throws Exception {
// range not requested : Firefox, Opera, IE do not send range headers
if (range == null) {
StreamingOutput streamer = new StreamingOutput() {
@Override
public void write(final OutputStream output) throws IOException, WebApplicationException {

final FileChannel inputChannel = new FileInputStream(asset).getChannel();
final WritableByteChannel outputChannel = Channels.newChannel(output);
try {
inputChannel.transferTo(0, inputChannel.size(), outputChannel);
} finally {
// closing the channels
inputChannel.close();
outputChannel.close();
}
}
};
return Response.ok(streamer).header(HttpHeaders.CONTENT_LENGTH, asset.length()).build();
}

String[] ranges = range.split("=")[1].split("-");
final int from = Integer.parseInt(ranges[0]);
/**
* Chunk media if the range upper bound is unspecified. Chrome sends "bytes=0-"
*/
int to = chunk_size + from;
if (to >= asset.length()) {
to = (int) (asset.length() - 1);
}
if (ranges.length == 2) {
to = Integer.parseInt(ranges[1]);
}

final String responseRange = String.format("bytes %d-%d/%d", from, to, asset.length());
final RandomAccessFile raf = new RandomAccessFile(asset, "r");
raf.seek(from);

final int len = to - from + 1;
final MediaStreamer streamer = new MediaStreamer(len, raf);
Response.ResponseBuilder res = Response.status(Status.PARTIAL_CONTENT).entity(streamer)
.header("Accept-Ranges", "bytes")
.header("Content-Range", responseRange)
.header(HttpHeaders.CONTENT_LENGTH, streamer.getLenth())
.header(HttpHeaders.LAST_MODIFIED, new Date(asset.lastModified()));
return res.build();
}

这是 MediaStreamer 实现,用于在您的资源方法中流式传输输出。
public class MediaStreamer implements StreamingOutput {

private int length;
private RandomAccessFile raf;
final byte[] buf = new byte[4096];

public MediaStreamer(int length, RandomAccessFile raf) {
this.length = length;
this.raf = raf;
}

@Override
public void write(OutputStream outputStream) throws IOException, WebApplicationException {
try {
while( length != 0) {
int read = raf.read(buf, 0, buf.length > length ? length : buf.length);
outputStream.write(buf, 0, read);
length -= read;
}
} finally {
raf.close();
}
}

public int getLenth() {
return length;
}
}

关于rest - Jersey REST 支持简历/媒体流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14410344/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com