简体   繁体   中英

How to download file from internet in java

I have a problem when I want to download file from internet in java. I try to used something like this:

    String stringUrl = "http://imageshack.us/a/img841/7762/formd.png";
    File file = new File("");       
        try {
            URL url = new URL(stringUrl);
            FileUtils.copyURLToFile(url, file);
        }

but I got an I/O exception. What is the best way to download file from internet and put it into 'File' object?

That's because you haven't given the file a name, and writing to a file with no name makes no sense.

File file = new File("");  

If you replace that line with something like:

File file = new File("x.png");

...then it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM