2016-02-14 89 views
0

我有一個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

回答

0

嘗試下一編碼,它應該工作:

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

如果使用在最後的Java 7,您可以使用Files.copy。鑑於存在的例子是exacly你想要什麼:「......捕捉網頁,並將其保存到文件」

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

我建議使用新的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(); 
     } 
    } 
}