Corrupt file when using Java to download file
<p>This problem seems to happen inconsistently.  We are using a java applet to download a file from our site, which we store temporarily on the client's machine. </p>

<p>Here is the code that we are using to save the file:</p>

<pre><code>URL targetUrl = new URL(urlForFile);
InputStream content = (InputStream)targetUrl.getContent();
BufferedInputStream buffered = new BufferedInputStream(content);
File savedFile = File.createTempFile("temp",".dat");

FileOutputStream fos = new FileOutputStream(savedFile);
int letter;
while((letter = buffered.read()) != -1)
    fos.write(letter);
fos.close();
</code></pre>

<p>Later, I try to access that file by using:</p>

<pre><code>ObjectInputStream keyInStream = new ObjectInputStream(new FileInputStream(savedFile));
</code></pre>

<p>Most of the time it works without a problem, but every once in a while we get the error:</p>

<pre><code>java.io.StreamCorruptedException: invalid stream header: 0D0A0D0A
</code></pre>

<p>which makes me believe that it isn't saving the file correctly.</p>
