用Java下载文件

时间:2016-02-14 04:33:50

标签: java url inputstream

我想要从java下载的mp3链接仅用于测试。这是我编写的代码

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)

3 个答案:

答案 0 :(得分: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();
    }
}

答案 1 :(得分:0)

如果您在最后使用Java 7,则可以使用Files.copy。这里给出的例子非常符合您的要求:“......捕获网页并将其保存到文件中”

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

答案 2 :(得分:0)

我建议使用New 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();
        }
    }
}