我尝试检索音频文件时出现HTTP 500错误

时间:2016-01-25 00:55:37

标签: java http

我试图让这个Java程序使用URL接收音频文件。当我这样做时,它只是给出一个错误:

  

javax.sound.sampled.UnsupportedAudioFileException:无法从输入流中获取音频输入流       在javax.sound.sampled.AudioSystem.getAudioInputStream(未知来源)       在Starter.main(Starter.java:21)

当您转到浏览器中的URL时,它会下载一个没有.mp3扩展名的文件,其中包含用于将其存储在数据库中的哈希值。

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;

public class Starter {

public static void main(String[] args) {
    AudioInputStream din = null;
    try {
        URL url = new URL("http://www.roblox.com/asset/?id=138738005");
        HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
        InputStream bufferedIn = new BufferedInputStream(httpcon.getInputStream());
        //AudioInputStream in = AudioSystem.getAudioInputStream(Starter.class.getResourceAsStream("338876528.mp3"));
        AudioInputStream in = AudioSystem.getAudioInputStream(bufferedIn);
        AudioFormat baseFormat = in.getFormat();
        AudioFormat decodedFormat = new AudioFormat(
                AudioFormat.Encoding.PCM_SIGNED,
                baseFormat.getSampleRate(), 16, baseFormat.getChannels(),
                baseFormat.getChannels() * 2, baseFormat.getSampleRate(),
                false);
        din = AudioSystem.getAudioInputStream(decodedFormat, in);
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, decodedFormat);
        SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
        if(line != null) {
            line.open(decodedFormat);
            byte[] data = new byte[4096];
            // Start
            line.start();

            int nBytesRead;
            while ((nBytesRead = din.read(data, 0, data.length)) != -1) {
                line.write(data, 0, nBytesRead);
            }
            // Stop
            line.drain();
            line.stop();
            line.close();
            din.close();
        }

    }
    catch(Exception e) {
        e.printStackTrace();
    }
    finally {
        if(din != null) {
            try { din.close(); } catch(IOException e) { }
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

http://www.roblox.com/asset/?id=138738005的内容是

<roblox xmlns:xmime="http://www.w3.org/2005/05/xmlmime" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.roblox.com/roblox.xsd" version="4">
  <External>null</External>
  <External>nil</External>
  <Item class="ShirtGraphic" referent="RBX0">
    <Properties>
      <Content name="Graphic">
        <url>http://www.roblox.com/asset/?id=138738004</url>
      </Content>
      <string name="Name">Shirt Graphic</string>
      <bool name="archivable">true</bool>
    </Properties>
  </Item>
</roblox>

所以我会在尝试将其视为音频文件时期待异常。 url标记http://www.roblox.com/asset/?id=138738004中引用的文件是PNG

referenced file

所以,即使提取这些也无济于事。