简体   繁体   English

如何使用Java从Internet下载文件

[英]How to download file from internet in java

I have a problem when I want to download file from internet in java. 我想从Java中的Internet下载文件时遇到问题。 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. 但是我有一个I / O异常。 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. ...然后它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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