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

前端 未结 21 2882
情深已故
情深已故 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:46

    There is an issue with simple usage of:

    org.apache.commons.io.FileUtils.copyURLToFile(URL, File) 
    

    if you need to download and save very large files, or in general if you need automatic retries in case connection is dropped.

    What I suggest in such cases is Apache HttpClient along with org.apache.commons.io.FileUtils. For example:

    GetMethod method = new GetMethod(resource_url);
    try {
        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            logger.error("Get method failed: " + method.getStatusLine());
        }       
        org.apache.commons.io.FileUtils.copyInputStreamToFile(
            method.getResponseBodyAsStream(), new File(resource_file));
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        method.releaseConnection();
    }
    

提交回复
热议问题