Several ways Java downloads files from the web

Source: Internet
Author: User

Package com.github.pandafang.tool;

Import Java.io.BufferedOutputStream;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.net.URL;
Import Java.nio.channels.Channels;
Import Java.nio.channels.FileChannel;
Import Java.nio.channels.ReadableByteChannel;
Import Java.nio.file.Files;
Import Java.nio.file.Path;
Import java.nio.file.Paths;
Import java.nio.file.StandardCopyOption;

Import Org.apache.commons.io.FileUtils;

/**
* File Tool class
* @author Panda Fang
* @date 2017-08-26
* @version 1.0
*/
public class Filetool {

/**
* Download files using traditional IO stream
* @param URL
* @param savedir
* @param fileName
*/
public static void Download (string url, String savedir, String fileName) {

Bufferedoutputstream BOS = NULL;
InputStream is = null;
try {
byte[] buff = new byte[8192];
is = new URL (URL). OpenStream ();
File File = new file (Savedir, fileName);
File.getparentfile (). Mkdirs ();
BOS = new Bufferedoutputstream (new FileOutputStream (file));
int count = 0;
while ((count = Is.read (buff))! =-1) {
Bos.write (Buff, 0, count);
}
}
catch (IOException e) {
E.printstacktrace ();
}
finally {
if (is = null) {
try {
Is.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}

if (BOS! = NULL) {
try {
Bos.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}

}

}

/**
* Download files using Commonio Library, rely on Apache Common IO, official website https://commons.apache.org/proper/commons-io/
* @param URL
* @param savedir
* @param fileName
*/
public static void Downloadbyapachecommonio (string url, String savedir, String fileName) {
try {
Fileutils.copyurltofile (new URL), new File (Savedir, fileName));
} catch (IOException e) {
E.printstacktrace ();
}
}

/**
* Download files using NIO, requires JDK 1.4+
* @param URL
* @param savedir
* @param fileName
*/
public static void Downloadbynio (string url, String savedir, String fileName) {
Readablebytechannel RBC = null;
FileOutputStream fos = null;
FileChannel FOUTC = null;
try {
RBC = channels.newchannel (new URL (URL). OpenStream ());
File File = new file (Savedir, fileName);
File.getparentfile (). Mkdirs ();
FOS = new FileOutputStream (file);
FOUTC = Fos.getchannel ();
Foutc.transferfrom (RBC, 0, Long.max_value);

} catch (IOException e) {
E.printstacktrace ();
} finally {
if (RBC! = null) {
try {
Rbc.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
if (FOUTC! = null) {
try {
Foutc.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}

}

/**
* Download files using NIO, requires JDK 1.7+
* @param URL
* @param savedir
* @param fileName
*/
public static void DownloadByNIO2 (string url, String savedir, String fileName) {
Try (inputstream ins = new URL (URL). OpenStream ()) {
Path target = Paths.get (Savedir, fileName);
Files.createdirectories (Target.getparent ());
Files.copy (INS, target, standardcopyoption.replace_existing);

} catch (IOException e) {
E.printstacktrace ();
}

}
}

Several ways Java downloads files from the Web (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.