java从url下载文件_从URL下载Java文件

2023-02-17 00:00:00 java 文件 下载

java从url下载文件

Today we will learn how to download a file from URL in java. We can use java.net.URL openStream() method to download file from URL in java program. We can use Java NIO Channels or Java IO InputStream to read data from the URL open stream and then save it to file.

今天,我们将学习如何从Java中的URL下载文件。 我们可以使用java.net.URL openStream()方法从java程序中的URL下载文件。 我们可以使用Java NIO Channels或Java IO InputStream从URL打开流中读取数据,然后将其保存到文件中。

从URL下载Java文件 (Java Download File from URL)

《java从url下载文件_从URL下载Java文件》

Here is the simple java download file from URL example program. It shows both ways to download file from URL in java.


这是来自URL示例程序的简单Java下载文件。 它显示了从Java中的URL下载文件的两种方法。

JavaDownloadFileFromURL.java

JavaDownloadFileFromURL.java

package com.journaldev.files;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

public class JavaDownloadFileFromURL {

    public static void main(String[] args) {
        String url = "https://www.journaldev.com/sitemap.xml";
        
        try {
            downloadUsingNIO(url, "/Users/pankaj/sitemap.xml");
            
            downloadUsingStream(url, "/Users/pankaj/sitemap_stream.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void downloadUsingStream(String urlStr, String file) throws IOException{
        URL url = new URL(urlStr);
        BufferedInputStream bis = new BufferedInputStream(url.openStream());
        FileOutputStream fis = new FileOutputStream(file);
        byte[] buffer = new byte[1024];
        int count=0;
        while((count = bis.read(buffer,0,1024)) != -1)
        {
            fis.write(buffer, 0, count);
        }
        fis.close();
        bis.close();
    }

    private static void downloadUsingNIO(String urlStr, String file) throws IOException {
        URL url = new URL(urlStr);
        ReadableByteChannel rbc = Channels.newChannel(url.openStream());
        FileOutputStream fos = new FileOutputStream(file);
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        fos.close();
        rbc.close();
    }

}

downloadUsingStream: In this method of java download file from URL, we are using URL openStream method to create the input stream. Then we are using a file output stream to read data from the input stream and write to the file.

downloadUsingStream :在这种从URL下载Java文件的方法中,我们使用URL openStream方法创建输入流。 然后,我们使用文件输出流从输入流中读取数据并写入文件。

downloadUsingNIO: In this download file from URL method, we are creating byte channel from URL stream data. Then use the file output stream to write it to file.

downloadUsingNIO :在此从URL下载文件的方法中,我们正在从URL流数据创建字节通道。 然后使用文件输出流将其写入文件。

You can use any of these methods to download the file from URL in java program. If you are looking for performance, then do some analysis by using both methods and see what suits your need.

您可以使用这些方法中的任何一种从java程序中的URL下载文件。 如果您正在寻找性能,则可以使用这两种方法进行一些分析,然后找出适合您需求的方法。

GitHub Repository.
GitHub存储库中签出更多Java IO示例。

翻译自: https://www.journaldev.com/924/java-download-file-url

java从url下载文件

    原文作者:cunchi4221
    原文地址: https://blog.csdn.net/cunchi4221/article/details/107471086
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。

相关文章