下载/保存可直接从给定URL获取的文件

时间:2014-11-22 13:11:14

标签: java download

我有this link之类的链接,它直接询问要保存的文件名,并开始在浏览器中下载。

如何以编程方式下载或保存此文件?

我尝试了以下方法:

static void DownloadFile(String url, String fileName) throws MalformedURLException, IOException
{
    url = "http://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=DESCRIBE+<"+ url +">&format=text%2Fcsv";
    URL link = new URL(url); //The file that you want to download

    InputStream in = new BufferedInputStream(link.openStream());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int n = 0;
    while (-1!=(n=in.read(buf)))
    {
       out.write(buf, 0, n);
    }
    out.close();
    in.close();
    byte[] response = out.toByteArray();

    FileOutputStream fos = new FileOutputStream(fileName);
    fos.write(response);
    fos.close();
    System.out.println("Finished");
}

但这会将只有第一行的文件保存为“”主题“,”谓词“,”对象“ “而不是完整的档案。

修改

正如在答案中所建议的那样,我尝试了以下内容,但这也只给出了文件的第一行:

static void DownloadFile(String s_url, String fileName) throws MalformedURLException, IOException
{
    s_url = "http://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=DESCRIBE+<"+ s_url +">&format=text%2Fcsv";
    //url = "http://dbpedia.org/data/Sachin_Tendulkar.rdf";
    try {   
        URL url = new URL(s_url); //The file that you want to download

        // read text returned by server
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        PrintWriter out = new PrintWriter(fileName);

        String line;
        while ((line = in.readLine()) != null) {
            out.println(line);
        }
        in.close();
        out.close();
    }
    catch (MalformedURLException e) {
        System.out.println("Malformed URL: " + e.getMessage());
    }
    catch (IOException e) {
        System.out.println("I/O Error: " + e.getMessage());
    }
    System.out.println("Finished");
}

修改

我也尝试使用Apache FileUtils,但这也只给了文件的第一行。

static void DownloadFile(String s_url, String fileName) throws MalformedURLException, IOException
{
    s_url = "http://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=DESCRIBE+<"+ s_url +">&format=text%2Fcsv";
    URL url = new URL(s_url); //The file that you want to download
    FileUtils.copyURLToFile(url, new File(fileName));
    System.out.println("Finished");
}

1 个答案:

答案 0 :(得分:0)

如果你想下载一个文件,Apache Commons就是你想要的,效果很好!

org.apache.commons.io.FileUtils.copyURLToFile(new URL("URL")), new File("path/to/file"));

如果网址返回文字,您可以尝试这样的内容:

 try {   
        URL url = new URL("http://www.google.com:80/");

        // read text returned by server
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        PrintWriter out = new PrintWriter("filename.txt");

        String line;
        while ((line = in.readLine()) != null) {
            out.println(line);
        }
        in.close();
        out.close();
    }
    catch (MalformedURLException e) {
        System.out.println("Malformed URL: " + e.getMessage());
    }
    catch (IOException e) {
        System.out.println("I/O Error: " + e.getMessage());
    }