How to download and save a file from Internet using Java?

前端 未结 21 2887
情深已故
情深已故 2020-11-21 05:06

There is an online file (such as http://www.example.com/information.asp) I need to grab and save to a directory. I know there are several methods for grabbing a

21条回答
  •  后悔当初
    2020-11-21 05:42

    It's possible to download the file with with Apache's HttpComponents instead of Commons-IO. This code allows you to download a file in Java according to its URL and save it at the specific destination.

    public static boolean saveFile(URL fileURL, String fileSavePath) {
    
        boolean isSucceed = true;
    
        CloseableHttpClient httpClient = HttpClients.createDefault();
    
        HttpGet httpGet = new HttpGet(fileURL.toString());
        httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
        httpGet.addHeader("Referer", "https://www.google.com");
    
        try {
            CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity fileEntity = httpResponse.getEntity();
    
            if (fileEntity != null) {
                FileUtils.copyInputStreamToFile(fileEntity.getContent(), new File(fileSavePath));
            }
    
        } catch (IOException e) {
            isSucceed = false;
        }
    
        httpGet.releaseConnection();
    
        return isSucceed;
    }
    

    In contrast to the single line of code:

    FileUtils.copyURLToFile(fileURL, new File(fileSavePath),
                            URLS_FETCH_TIMEOUT, URLS_FETCH_TIMEOUT);
    

    this code will give you more control over a process and let you specify not only time outs but User-Agent and Referer values, which are critical for many web-sites.

提交回复
热议问题