Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.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程序对wave资源文件的多个http访问?_Java_Http_Inputstream_Wav_Javasound - Fatal编程技术网

如何避免java程序对wave资源文件的多个http访问?

如何避免java程序对wave资源文件的多个http访问?,java,http,inputstream,wav,javasound,Java,Http,Inputstream,Wav,Javasound,我使用以下代码从web服务器上的wav文件获取数据。getFormat(),getFormatLength(),totallength并读取字节,每个字节都执行了http访问,服务器日志显示有3次访问。有没有办法一次旅行 try { audioInputStream = AudioSystem.getAudioInputStream(url);//soundFile); format = audioInputStream.getFormat(); totallength

我使用以下代码从web服务器上的wav文件获取数据。
getFormat()
getFormatLength()
totallength
并读取字节,每个字节都执行了http访问,服务器日志显示有3次访问。有没有办法一次旅行

try {
    audioInputStream = AudioSystem.getAudioInputStream(url);//soundFile);
    format = audioInputStream.getFormat();
    totallength = audioInputStream.getFrameLength()*format.getFrameSize();
    waveData = new byte[(int)totallength];
    int total=0;
    int nBytesRead = 0;
    try {
        while (nBytesRead != -1 && total<totallength) {
            nBytesRead = audioInputStream.read(waveData, total, (int) totallength);
            if (nBytesRead>0)
                total+=nBytesRead;
            }
    ...
试试看{
audioInputStream=AudioSystem.getAudioInputStream(url);//声音文件);
format=audioInputStream.getFormat();
totallength=audioInputStream.getFrameLength()*format.getFrameSize();
waveData=新字节[(int)总长度];
int-total=0;
int nBytesRead=0;
试一试{
而(nBytesRead!=-1&&total0)
总+=N字节读取;
}
...

看起来您首先要将整个文件读入内存,为什么不先读入所有文件呢

    // Read the audio file into waveData:
    BufferedInputStream in = new BufferedInputStream(url.openStream());
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    for (;;) {
        int b = in.read();
        if (b == -1) {
            break;
        }
        bos.write(b);
    }
    in.close();
    bos.close();
    byte[] waveData = bos.toByteArray();

    // Create the AudioInputSteam:
    ByteArrayInputStream bis = new ByteArrayInputStream(waveData);
    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(bis);

看起来您是在首先将整个文件读入内存,为什么不先读入所有文件呢

    // Read the audio file into waveData:
    BufferedInputStream in = new BufferedInputStream(url.openStream());
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    for (;;) {
        int b = in.read();
        if (b == -1) {
            break;
        }
        bos.write(b);
    }
    in.close();
    bos.close();
    byte[] waveData = bos.toByteArray();

    // Create the AudioInputSteam:
    ByteArrayInputStream bis = new ByteArrayInputStream(waveData);
    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(bis);

从内存中逐字节读取会非常慢。唯一的方法是使用缓冲区而不是逐字节读取。但是这种方法有效!从内存中逐字节读取会非常慢。唯一的方法是使用缓冲区而不是逐字节读取。但是这种方法有效!所有这些请求都是完全值得的GET请求吗(在响应体中返回整个文件),或者是头几个请求(只返回HTTP头,因此是空的响应体)。我希望它们是“只是”头请求,应该非常便宜。或者,服务器是由您自己维护的吗(例如servlet?)是不是它的工作做得不完全正确,并返回整个文件的头部呢?@BalusC我使用了servlt,我想我需要更新代码。但我也在考虑使用Joop Eggen的建议,即只读取整个流而不知道其大小。我意识到没有使用格式。这样可以减少r往返时间也一样。是所有这些fullworthy GET请求(在响应正文中返回整个文件),还是
getFormat()
getFormatLength()
头请求(只返回HTTP头,因此响应正文为空)上的前几个请求。我希望它们是“just”HEAD请求应该非常便宜。或者,服务器是由您自己维护的(例如servlet?)是不是它的工作做得不完全正确,并返回整个文件的头部呢?@BalusC我使用了servlt,我想我需要更新代码。但我也在考虑使用Joop Eggen的建议,即只读取整个流而不知道其大小。我意识到没有使用格式。这样可以减少r旅行时间也一样。