Android downloads HttpUrlConnection-based files,

Source: Internet
Author: User
Tags file separator

Android downloads HttpUrlConnection-based files,

1/** 2 * get method File Download 3 * <p> 4 * Special Note: The progressBar in android is the only control that google has processed to update the UI in the Child thread 5*6 * @ param path 7 */8 private void httpDown (final String path) {9 new Thread () {10 @ Override11 public void run () {12 URL; 13 HttpURLConnection connection; 14 try {15 // unified resource 16 url = new URL (path); 17 // open the link 18 connection = (HttpURLConnection) url. openConnection (); 19 // set connection timeout 20 connection. setConnectTimeout (4 000); 21 // set to allow the server's input stream. The default value is true. You do not need to set 22 connection. setDoInput (true); 23 // The setting allows data to be written to the server. Generally, the get method is not set. It is mostly used in the post method. The default setting is false24 connection. setDoOutput (true); // This is only for method description 25 // Set Request Method 26 connection. setRequestMethod ("GET"); 27 // sets the request's character encoding 28 connection. setRequestProperty ("Charset", "UTF-8"); 29 // set connection to open the connection resource 30 connection. connect (); 31 32 // get the file path 33 String urlFilePath = connection in the link address. getURL (). getFile (); 34 // obtain the separatorChar parameter of the total file name of the url address, which indicates the file separator 35 String fileName = urlFilePath. substring (urlFilePath. lastIndexOf (File. separatorChar) + 1); 36 // create a file object to store the downloaded file getFilesDir () methods can be called only when the class inherited to the Context class 37 // you can directly call other classes and must use the Context object, in the internal storage, the file path under this application package is 38 // if external storage is used, you need to add the file read and write permissions, more than 5.0 of systems require dynamic access to permissions. 39 File file = new File (getFilesDir (), fileName); 40 // create a file output stream 41 FileOutputStream outputStream = new FileOutputStream (File ); 42 43 // get the link response code 200 is successful 44 int responseCode = connection. getResponseCode (); 45 if (responseCode = HttpURLConnection. HTTP_ OK) {46 // The input stream 47 InputStream inputStream = connection. getInputStream (); 48 // get the total length of the request content 49 int contentLength = connection. getContentLength (); 50 51 // set The Max52 mPb of progressBar. setMax (contentLength); 53 54 55 // create a buffer input stream object, which is more efficient than inputStream 56 BufferedInputStream bfi = new BufferedInputStream (inputStream ); 57 // The len here indicates the length of the content read in each loop is 58 int len; 59 // The total length read is 60 int totle = 0; 61 // bytes is used to store the content read every time 62 byte [] bytes = new byte [1024]; 63 while (len = bfi. read (bytes ))! =-1) {64 // Add len to totle after each read is complete 65 totle + = len; 66 // update progressBar67 mPb every read. setProgress (totle); 68 // write the data read from the server through the file output stream 69 outputStream. write (bytes, 0, len); 70} 71 // close the stream object 72 outputStream. close (); 73 inputStream. close (); 74 bfi. close (); 75 76 runOnUiThread (new Runnable () {77 @ Override78 public void run () {79 Toast. makeText (MainActivity. this, "Download complete! ", Toast. LENGTH_SHORT ). show (); 80} 81}); 82} 83 84} catch (Exception e) {85 e. printStackTrace (); 86} 87} 88 }. start (); 89}

You are welcome to leave a message if you have any questions.

Related Article

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.