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)