How to Download a File from Internet in Java


In this example, we will learn the method to download a file from the Internet in Java.

Source Code

package com.beginner.examples;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

public class DownloadFile {

	public static void main(String[] args) {
		String FileURL = "https://image.baidu.com/search/detail?ct=503316480&z=undefined&tn=baiduimagedetail&ipn=d&word=%E8%B0%B7%E6%AD%8C%E5%9B%BE%E7%89%87&step_word=&ie=utf-8&in=&cl=2&lm=-1&st=undefined&hd=undefined&latest=undefined&copyright=undefined&cs=333704077,3750189840&os=1813526069,111968815&simid=3466773258,540824448&pn=0&rn=1&di=75350&ln=1167&fr=&fmq=1554889528547_R&fm=&ic=undefined&s=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&is=0,0&istype=0&ist=&jit=&bdtype=0&spn=0&pi=0&gsm=0&objurl=http%3A%2F%2Fphotocdn.sohu.com%2F20150902%2FImg420317597.jpg&rpstart=0&rpnum=0&adpicid=0&force=undefined&ctd=1554889531714^3_1899X933%1";
        String file = "F:tmp";

        try {
            FileUtils.copyURLToFile(new URL(FileURL), new File(file), 10000, 10000);

        } catch (IOException e) {
            e.printStackTrace();
        }

	}

}

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments