Yesterday, when I was working on a project, I used the knowledge of downloading files from the server and saving them to the local area. I have never been exposed to them before. I worked on them for a day yesterday and this small function was implemented. Let’s briefly talk about the implementation process below;
1. Basic knowledge
When we want to download a resource on the website, we will obtain a url, which is a description of the server's resource location. The download process has the following steps:
(1) The client initiates a url request to obtain the connection object.
(2) The server parses the url and returns the specified resource to the client.
(3) Create the stored directory and saved file name.
(4) Write data is output.
(5) Turn off the input stream and output stream.
2. Methods to implement code
/** * @Function download temporary material interface* @param filePath The directory to be saved in the file* @param method Request method, including POST and GET * @param url The path to request * @return */ public static File saveUrlAs(String url,String filePath,String method){ //System.out.println("fileName---->"+filePath); //Create different folder directories File file=new File(filePath); //Determine if the folder exists if (!file.exists()) { //If the folder does not exist, create a new folder file.mkdirs(); } FileOutputStream fileOut = null; HttpURLConnection conn = null; InputStream inputStream = null; try { // Create link URL httpUrl=new URL(url); conn=(HttpURLConnection) httpUrl.openConnection(); // Submit the form in Post mode, the default get method is conn.setRequestMethod(method); conn.setDoInput(true); conn.setDoOutput(true); // The cached conn.setUseCaches(false); //Connect the specified resource connected.connect(); //Get the network input stream inputStream=conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(inputStream); //Determine whether the file's saving path ends with / if (!filePath.endsWith("/")) { filePath += "/"; } //Write to the file (note that the file name must be added after the file's saving path) fileOut = new FileOutputStream(filePath+"123.png"); BufferedOutputStream bos = new BufferedOutputStream(fileOut); byte[] buf = new byte[4096]; int length = bis.read(buf); //Save the 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("Throw an exception!!"); } return file; } 3. Code test class (main function)
/** * @param args */ public static void main(String[] args) { String photoUrl = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png"; String fileName = photoUrl.substring(photoUrl.lastIndexOf("/")); //System.out.println("fileName---->"+fileName); String filePath = "d:"; File file = saveUrlAs(photoUrl, filePath + fileName,"GET"); System.out.println("Run ok!/n<BR>Get URL file " + file); }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.