Package test;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;
Import java.net.HttpURLConnection;
Import Java.net.URL;
Import Java.util.Calendar; public class Filedownload1 {/** * @ feature Downloads Temporary material interface * @param filePath file will save the directory * @param method request methods, including post and get * @pa The path to the RAM URL request * @return/public static File Saveurlas (String url,string filepath,string method) {Fileoutputstre
AM fileout = null;
HttpURLConnection conn = null;
InputStream inputstream = null;
File File=null;
try {//create a link URL httpurl=new url (url);
Conn= (HttpURLConnection) httpurl.openconnection ();
Submit the form as post and the default get mode Conn.setrequestmethod (method);
Conn.setdoinput (TRUE);
Conn.setdooutput (TRUE);
The Post method cannot use the cache Conn.setusecaches (false);
Connect the specified resource Conn.connect ();
Get the network input stream Inputstream=conn.getinputstream (); Bufferedinputstream bis = new Bufferedinputstream (InputStream);
Determines whether the file's save path is followed by/End if (!filepath.endswith ("/")) {FilePath = = "/";
String extname = url.substring (Url.lastindexof ("."));
Java.util.GregorianCalendar Gcalendar = new Java.util.GregorianCalendar ();
String year = Gcalendar.get (calendar.year) + "";
String month = gcalendar.get (calendar.month) +1 + "";
String day = Gcalendar.get (calendar.day_of_month) + "";
String newfilename =new java.util.Date (). GetTime () + "_" + (int) (1000 *math.random ()) + Extname;
String Dirpath = year + file.separator +month + file.separator + day + file.separator;
Write to file (note that the file name must be appended to the file save path)//create a different folder directory File=new file (FilePath +dirpath);
Determine if the folder exists if (!file.exists ()) {//If the folder does not exist, create a new folder File.mkdirs ();
} fileout = new FileOutputStream (FilePath +dirpath +newfilename); Bufferedoutputstream BOS = new Bufferedoutputstream (fileouT);
byte[] buf = new byte[4096];
int length = Bis.read (BUF);
Save file while (length!=-1) {bos.write (buf, 0, length);
Length = Bis.read (BUF);
} bos.close ();
Bis.close ();
Conn.disconnect ();
catch (Exception e) {e.printstacktrace (); System.out.println ("throws an exception..
");
} return file; public static void Main (string[] args) {String fileUrl = ' https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/
Img/logo/bd_logo1_31bdc765.png ";
String fileUrl = "File:///D:/test/test1.txt"; * * ERROR: Java.lang.ClassCastException:sun.net.www.protocol.file.FileURLConnection cannot be cast to Java.net.HttpURLCo
Nnection */String FilePath = "D:/test1";
File File = Saveurlas (FileUrl, FilePath, "get"); /* If post, error: Java.io.IOException:Server returned HTTP response code:405 for Url:https://ss0.bdstatic ...
* * SYSTEM.OUT.PRINTLN ("Run ok!/n<br>get URL File" + file);
}
}
Note that this method is of greater constraint:
1. This method can only be used with get, not post;
2. You can only download files from other Web sites using the HTTP protocol, and you cannot download server local files with the file protocol.