【问题标题】:Cannot download file from URL in java无法从java中的URL下载文件
【发布时间】:2015-07-13 18:29:35
【问题描述】:

我正在制作一个从 URL 下载文件的程序。下载总是开始,但没有完成。例如,如果文件大小为 3 MB,程序只下载一半,所以我无法打开下载的文件。但是程序说文件下载成功。

public class FileDownloader {

    public static void main (String [] args) throws IOException {

        InputStream fileIn;
        FileOutputStream fileOut;
        Scanner s = new Scanner(System.in);

        System.out.println("Enter URL: ");
        String urlStr = s.nextLine();

        URL url = new URL(urlStr);
        URLConnection urlConnect = url.openConnection();
        fileIn = urlConnect.getInputStream();

        System.out.println("Enter file name: ");
        String fileStr = s.nextLine();
        fileOut = new FileOutputStream(fileStr);

        while (fileIn.read() != -1) {   
            fileOut.write(fileIn.read());
        }
        System.out.println("File is downloaded");
    }
}

那我该如何解决呢?应该用其他方式下载吗?

【问题讨论】:

标签: java file url download


【解决方案1】:

由于

,您正在失去所有候补byte
    while (fileIn.read() != -1) {     //1st read
        fileOut.write(fileIn.read());     //2nd read - 1st write
    }

你读了两次,只写了一次。

你需要做的是

    int x;
    while ((x = fileIn.read()) != -1) {   //1st read
        fileOut.write(x);     //1st write
    }

这是你的完整代码

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;

public class FileDownloader {

    public static void main(String[] args) throws IOException {

        InputStream fileIn;
        FileOutputStream fileOut;
        Scanner s = new Scanner(System.in);

        System.out.println("Enter URL: ");
        String urlStr = s.nextLine();

        URL url = new URL(urlStr);
        URLConnection urlConnect = url.openConnection();
        fileIn = urlConnect.getInputStream();

        System.out.println("Enter file name: ");
        String fileStr = s.nextLine();
        fileOut = new FileOutputStream(fileStr);

        int x;
        while ((x = fileIn.read()) != -1) {
            fileOut.write(x);
        }
        System.out.println("File is downloaded");

}

【讨论】:

    【解决方案2】:

    您可以使用以下代码高效下载大文件。

     public static void main(String[] args) throws IOException {
        InputStream in = null;
        FileOutputStream out = null;
        try {
            System.out.println("Starting download");
            long t1 = System.currentTimeMillis();
            URL url = new URL(args[0]);// or you can hard code the URL
            // Open the input and out files for the streams
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            in = conn.getInputStream();
            out = new FileOutputStream(args[1]);//// or you can hard code the filename
            // Read data into buffer and then write to the output file
            byte[] buffer = new byte[8192];
            int bytesRead;
            while ((bytesRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytesRead);
            }
            long t2 = System.currentTimeMillis();
            System.out.println("Time for download & save file in millis:"+(t2-t1));
        } catch (Exception e) {
            // Display or throw the error
            System.out.println("Erorr while execting the program: "
                    + e.getMessage());
        } finally {
            // Close the resources 
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      简单地用这个:

      import org.apache.commons.io.FileUtils;
      import java.net.URL;
      String path =  "F:/"
      String fileName =  "song"
      FileUtils.copyURLToFile(myUrl, new File(path + fileName + ".mp3"));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-24
        • 1970-01-01
        • 2022-01-18
        • 2018-08-10
        • 1970-01-01
        • 1970-01-01
        • 2013-09-16
        • 2012-08-24
        相关资源
        最近更新 更多