How to download file from URL in java

摘要在这篇文章中,我们将了解如何从java中的URL下载文件。当您想要使用java从URL自动下载任何文件时,可以使用它。

有很多方法可以做到这一点,其中一些是:

Java Program:

package cn.micai.io;

import org.apache.commons.io.FileUtils;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

/**
 * 描述:How to download file from URL in java
 * <p>
*    Using Java input output stream
     Using apache common IO
     Using NIO
 *
 * @author: 赵新国
 * @date: 2018/6/7 10:15
 */
public class FileDownloadFromURLMain {

    public static void main(String [] args) {

        String dirName = "D://";

        System.out.println("---------------------------");
        System.out.println("Downloading file from github using java file IO");

        // Using java IO
        downloadFileFromUrlWithJavaIO(dirName + "/sampleFile1.zip",
                "https://github.com/arpitmandliya/SpringRestfulWebServicesWithJSONExample/archive/master.zip");

        System.out.println("Downloaded file from github using java file IO");
        System.out.println("---------------------------");

        System.out.println("Downloading file from github using apache common IO");
        // Using Apache common IO
        downloadFileFromUrlWithCommonsIO(dirName + "/sampleFile2.zip",
                "https://github.com/arpitmandliya/SpringRestfulWebServicesWithJSONExample/archive/master.zip");
        System.out.println("Downloaded file from github using apache common IO");
        System.out.println("---------------------------");

        System.out.println("Downloading file from github using NIO");
        // Using NIO
        downloadFileFromURLUsingNIO(
                dirName + "/sampleFile3.zip",
                "https://github.com/arpitmandliya/SpringRestfulWebServicesWithJSONExample/archive/master.zip");
        System.out.println("Downloaded file from github using NIO");
        System.out.println("---------------------------");

    }

    // Using Java IO
    private static void downloadFileFromUrlWithJavaIO(String fileName, String fileUrl) {
        BufferedInputStream inputStream = null;
        FileOutputStream outputStream = null;
        try {
            URL url = new URL(fileUrl);
            inputStream = new BufferedInputStream(url.openStream());
            outputStream = new FileOutputStream(fileName);

            byte data[] = new byte[1024];
            int count;
            while ((count = inputStream.read(data, 0, 1024)) != -1) {
                outputStream.write(data, 0, count);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // Using Apache common IO
    private static void downloadFileFromUrlWithCommonsIO(String fileName, String fileUrl) {
        try {
            FileUtils.copyURLToFile(new URL(fileUrl), new File(fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // Using NIO
    private static void downloadFileFromURLUsingNIO(String fileName, String fileUrl) {
        try {
            URL url = new URL(fileUrl);
            ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream());
            FileOutputStream fileOutputStream = new FileOutputStream(fileName);
            fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
            fileOutputStream.close();
            readableByteChannel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

当您运行上述程序时,文件将被下载到上述目录中。您将得到以下输出:




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迷彩的博客

你的鼓励将是我最大的创作动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值