How to download file from URL in java
摘要:在这篇文章中,我们将了解如何从java中的URL下载文件。当您想要使用java从URL自动下载任何文件时,可以使用它。
有很多方法可以做到这一点,其中一些是:
- Using Java input output stream
- Using apache common IO
- Using NIO
Java Program:
package cn.micai.io;import org.apachemons.io.FileUtils;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;/*** 描述:How to download file from URL in java* <p>
* Using Java input output streamUsing apache common IOUsing 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 IOdownloadFileFromUrlWithJavaIO(dirName + "/sampleFile1.zip",".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 IOdownloadFileFromUrlWithCommonsIO(dirName + "/sampleFile2.zip",".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 NIOdownloadFileFromURLUsingNIO(dirName + "/sampleFile3.zip",".zip");System.out.println("Downloaded file from github using NIO");System.out.println("---------------------------");}// Using Java IOprivate 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 IOprivate static void downloadFileFromUrlWithCommonsIO(String fileName, String fileUrl) {try {FileUtils.copyURLToFile(new URL(fileUrl), new File(fileName));} catch (IOException e) {e.printStackTrace();}}// Using NIOprivate 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();}}}
当您运行上述程序时,文件将被下载到上述目录中。您将得到以下输出:
How to download file from URL in java
摘要:在这篇文章中,我们将了解如何从java中的URL下载文件。当您想要使用java从URL自动下载任何文件时,可以使用它。
有很多方法可以做到这一点,其中一些是:
- Using Java input output stream
- Using apache common IO
- Using NIO
Java Program:
package cn.micai.io;import org.apachemons.io.FileUtils;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;/*** 描述:How to download file from URL in java* <p>
* Using Java input output streamUsing apache common IOUsing 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 IOdownloadFileFromUrlWithJavaIO(dirName + "/sampleFile1.zip",".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 IOdownloadFileFromUrlWithCommonsIO(dirName + "/sampleFile2.zip",".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 NIOdownloadFileFromURLUsingNIO(dirName + "/sampleFile3.zip",".zip");System.out.println("Downloaded file from github using NIO");System.out.println("---------------------------");}// Using Java IOprivate 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 IOprivate static void downloadFileFromUrlWithCommonsIO(String fileName, String fileUrl) {try {FileUtils.copyURLToFile(new URL(fileUrl), new File(fileName));} catch (IOException e) {e.printStackTrace();}}// Using NIOprivate 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();}}}
当您运行上述程序时,文件将被下载到上述目录中。您将得到以下输出:
发布评论