File Downloading in Java
<p>I have an <code>mp3</code> link i want to download from java just for testing. Here is what i code to do that</p>

<pre><code>private void saveFile() throws Exception{
        System.out.println("Opening InputStream.");
        InputStream is =  fileUrlConnection.getInputStream();
        System.out.println("Total: "+is.available()+" bytes");
        FileOutputStream fos = new FileOutputStream(new File("hello.mp3"));
        byte[] buffer = new byte[1024];
        while (is.read(buffer)!= -1) {
            fos.write(buffer);
        }
        is.close();
        fos.close();
    }
</code></pre>

<p>Above method throws an exception after a number of calls.</p>

<pre class="lang-none prettyprint-override"><code>java.net.SocketException: Unexpected end of file from server
    at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
    at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
    at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
    at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at com.jwc.FileSaver.saveFile(FileSaver.java:24)
    at com.jwc.FileSaver.run(FileSaver.java:39)
    at java.lang.Thread.run(Unknown Source)
</code></pre>