即時通訊嘗試通過Java庫Restlet流文件。但是文件在流式傳輸時被寫入。在這裏它應該如何工作。正在寫文件時使用restlet流文件正在寫入
我創建了一個視頻和一個音頻文件,然後將這兩個文件合併爲一個,這一步需要相當長的時間。所以,當新文件創建時,我想將文件流式傳輸到瀏覽器,無需等待10分鐘即可觀看視頻。
目前我可以通過FileInputStream讀取文件塊,但我不知道如何將文件提供給瀏覽器。有任何想法嗎?
它甚至可以使用Restlet來提供動態文件嗎?
在此先感謝,併爲我的英語不好對不起^^
ZimTis
[更新]
我能夠播放MP3文件,它在創建的時候,多虧了傑羅姆Louvel :
public class RestletStreamTest extends ServerResource {
private InputRepresentation inputRepresentation;
public FileInputStream fis;
@Get
public InputRepresentation readFile() throws IOException {
final File f = new File("/path/to/tile.mp3");
fis = new FileInputStream(f);
inputRepresentation = new InputRepresentation(new InputStream() {
private boolean waited = false;
@Override
public int read() throws IOException {
waited = false;
// read the next byte of the FileInputStream, when reaching the
// end of the file, wait for 2 seconds and try again, in case
// the file was not completely created yet
while (true) {
byte[] b = new byte[1];
if (fis.read(b, 0, 1) > 0) {
return b[0] + 256;
} else {
if (waited) {
return -1;
} else {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
waited = true;
}
}
}
}
}, MediaType.AUDIO_MPEG);
return inputRepresentation;
}
}
這是一個有點生硬,但作品和稍後將細化。 當我更改代碼以嘗試播放視頻時,播放器會讀取視頻的所有字節,然後開始播放並再次讀取所有字節。視頻播放結束後,當我點擊播放按鈕時,什麼也沒有發生。 Restlet引發超時,然後視頻重新開始播放。我嘗試使用.mp4和.flv文件,但始終使用相同的結果。
我不確定它是Restlet還是palyer的問題。我在Firefox中使用VLC播放器,並嘗試使用Chrome中的標準html5播放器。但是Chrome播放器甚至沒有開始播放。
我錯過了什麼嗎?或者這只是玩家的問題?
謝謝,我能夠加載文件,但遇到了一堆新問題。文件將加載到頁面加載時寫入的位置,然後在超時後重新開始,但這次是更多的數據。如果我的聲望超過15,你的答案肯定會得到+1 ^^ – ZimTis