Java中的文件下载

问题描述:

我有一个mp3链接,我想从Java下载只是为了测试。以下是我的代码要做的Java中的文件下载

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(); 
    } 

上述方法在多次调用后引发异常。

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) 
+0

你怎么知道它没有越过这条线? 'getInputStream'是否会引发异常?顺便说一句。 'is.read()'返回读取的字节数。你不能忽略这个数字。 – SpiderPig

+0

对不起,它确实会抛出异常。请再次看看我的问题再次感谢 –

+0

你必须赶上例外,然后你可以再次下载文件,或者希望它完成了,尽管这个错误。您也可以按照此问题中所述,尝试在停止的位置恢复下载。 http://stackoverflow.com/questions/3411480/how-to-resume-an-interrupted-download – SpiderPig

尝试下一编码,它应该工作:

BufferedInputStream in = null; 
FileOutputStream fout = null; 
try { 
    in = fileUrlConnection.getInputStream(); 
    fout = new FileOutputStream(new File("hello.mp3")); 

    final byte data[] = new byte[1024]; 
    int count; 
    while ((count = in.read(data)) != -1) { 
     fout.write(data, 0, count); 
    } 
} finally { 
    if (in != null) { 
     in.close(); 
    } 
    if (fout != null) { 
     fout.close(); 
    } 
} 

如果使用在最后的Java 7,您可以使用Files.copy。鉴于存在的例子是exacly你想要什么:“......捕捉网页,并将其保存到文件”

try (InputStream is = fileUrlConnection.getInputStream()) { 
     Files.copy(is, Paths.get("hello.mp3")); 
    } 

我建议使用新的IO来解决这个问题:

import java.io.IOException; 
import java.net.URL; 
import java.net.URLConnection; 
import java.nio.ByteBuffer; 
import java.nio.channels.Channels; 
import java.nio.channels.ReadableByteChannel; 
import java.nio.channels.SeekableByteChannel; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
import java.nio.file.StandardOpenOption; 
public class Main { 
    public static void main(String[] args) throws IOException { 
     URL url = null; 
     ReadableByteChannel in = null; 
     SeekableByteChannel out = null; 
     try { 
      url = new URL("https://img1.goodfon.ru/original/3216x2072/f/56/samoed-sobaka-belaya-yazyk-trava.jpg"); 
      URLConnection cc = url.openConnection(); 
      in = Channels.newChannel(cc.getInputStream()); 
      out = Files.newByteChannel(Paths.get("dog.jpg"), StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE); 
      ByteBuffer buf = ByteBuffer.allocate(1024); 
      while (in.read(buf) != -1) { 
       buf.flip(); 
       out.write(buf); 
       buf.rewind(); 
       buf.limit(buf.capacity()); 
      } 
     } finally { 
      if (in != null) 
       in.close(); 
      if (out != null) 
       out.close(); 
     } 
    } 
}