android实现文件下载功能

家电维修 2023-07-16 19:16www.caominkang.com家电维修技术

android 在网络上下载文件,供大家参考,具体内容如下

步骤 :

1.使用HTTP协议下载文件

- 创建一个HttpURLConnection对象 : HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
- 获取一个InputStream对象 : urlConn.getInputStream()
- 访问网络的权限 : android.permission.INTERNET

2.将下载的文件写入SDCARD
- 得到当前设备SD卡的目录 : Environment.getExternalStrageDirectory()
- 访问SD卡的权限 : android.permission.WRITE_EXTERNAL_STORAGE

代码 : 

activity_main.xml

 
 
  

MainActivity.java

package .chay.donload; 
 
import .chay.utils.HttpDonloader; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.vie.Vie; 
import android.vie.Vie.OnClickListener; 
import android.idget.Button; 
 
public class MainActivity extends Activity { 
   
  String urlStr_txt = "http://192.168.56.1:8080/mp3/an.lrc"; 
  String urlStr_mp3 = "http://192.168.56.1:8080/mp3/an.mp3"; 
  String path = "mp3/"; 
  String fileName = "an.mp3"; 
  private Button donloadTxtButton; 
  private Button donloadMp3Button; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentVie(R.layout.activity_main); 
 donloadTxtButton = (Button) findVieById(R.id.btn_txt); 
 donloadTxtButton.setonClickListener(ne DonloadTxtListener()); 
 donloadMp3Button = (Button) findVieById(R.id.btn_mp3); 
 donloadMp3Button.setonClickListener(ne DonloadMp3Listener()); 
  } 
 
  //下载歌词文件按钮监听器 
  class DonloadTxtListener implements onClickListener { 
 @Override 
 public void onClick(Vie v) { 
   Thread t = ne DonloadTxtThread();  
   t.start(); 
   System.out.println("txt--->"); 
 } 
  } 
  //下载歌词文件线程 
  class DonloadTxtThread extends Thread{ 
 @Override 
 public void run() { 
   HttpDonloader httpDonloader = ne HttpDonloader(); 
   String lrc = httpDonloader.donload(urlStr_txt); 
   System.out.println(lrc); 
 } 
  } 
  //下载MP3文件按钮监听器 
  class DonloadMp3Listener implements onClickListener { 
 @Override 
 public void onClick(Vie v) { 
   Thread h = ne DonMp3Thread(); 
   h.start(); 
   System.out.println("mp3--->"); 
 } 
  } 
  //下载MP3文件线程 
  class DonMp3Thread extends Thread{ 
 @Override 
 public void run() { 
   HttpDonloader httpDonloader = ne HttpDonloader(); 
   int result = httpDonloader.donFile(urlStr_mp3, path, fileName); 
   System.out.println(result); 
 } 
  }   
} 

工具类:

HttpDonloader.java

package .chay.utils; 
 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java..HttpURLConnection; 
import java..MalformedURLException; 
import java..URL; 
 
public class HttpDonloader { 
  private URL url = null; 
 
   
  public String donload(String urlStr) { 
 StringBuffer sb = ne StringBuffer(); 
 String line = null; 
 BufferedReader buffer = null; 
 try { 
   // 创建一个URL对象 
   url = ne URL(urlStr); 
   // 创建一个Http连接 
   HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); 
   // 使用IO流读取数据 
   buffer = ne BufferedReader(ne InputStreamReader(urlConn.getInputStream())); 
   hile ((line = buffer.readLine()) != null) { 
 sb.append(line); 
   } 
 } catch (Exception e) { 
   e.printStackTrace(); 
 } finally { 
   try { 
 buffer.close(); 
   } catch (Exception e) { 
 e.printStackTrace(); 
   } 
 } 
 return sb.toString(); 
  } 
 
   
  public int donFile(String urlStr, String path, String fileName) { 
 InputStream inputStream = null; 
 try { 
   FileUtils fileUtils = ne FileUtils(); 

   if (fileUtils.isFileExist(path + fileName)) { 
 return 1; 
   } else { 
 inputStream = getInputStreamFromUrl(urlStr); 
 File resultFile = fileUtils.rite2SDFromInput(path,fileName, inputStream); 
 if (resultFile == null) { 
   return -1; 
 } 
   } 
 } catch (Exception e) { 
   e.printStackTrace(); 
   return -1; 
 } finally { 
   try { 
 inputStream.close(); 
   } catch (Exception e) { 
 e.printStackTrace(); 
   } 
 } 
 return 0; 
  } 
 
   
  public InputStream getInputStreamFromUrl(String urlStr) 
   thros MalformedURLException, IOException { 
 url = ne URL(urlStr); 
 HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); 
 InputStream inputStream = urlConn.getInputStream(); 
 return inputStream; 
  } 
} 

FileUtils.java

package .chay.utils; 
 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
 
import android.os.Environment; 
 
public class FileUtils { 
  private String SDPATH; 
 
  public String getSDPATH() { 
 return SDPATH; 
  } 
  public FileUtils() { 
 //得到当前外部存储设备的目录 
 // /SDCARD 
 SDPATH = Environment.getExternalStorageDirectory() + "/"; 
  } 
   
  public File creatSDFile(String fileName) thros IOException { 
 File file = ne File(SDPATH + fileName); 
 file.createNeFile(); 
 return file; 
  } 
   
   
  public File creatSDDir(String dirName) { 
 File dir = ne File(SDPATH + dirName); 
 dir.mkdirs(); 
 return dir; 
  } 
 
   
  public boolean isFileExist(String fileName){ 
 File file = ne File(SDPATH + fileName); 
 return file.exists(); 
  } 
   
   
  public File rite2SDFromInput(String path,String fileName,InputStream input){ 
 File file = null; 
 OutputStream output = null; 
 try{ 
   creatSDDir(path); 
   file = creatSDFile(path + fileName); 
   output = ne FileOutputStream(file); 
   byte buffer [] = ne byte[4  1024]; 
   hile((input.read(buffer)) != -1){ 
 output.rite(buffer); 
   } 
   output.flush(); 
 } 
 catch(Exception e){ 
   e.printStackTrace(); 
 } 
 finally{ 
   try{ 
 output.close(); 
   } 
   catch(Exception e){ 
 e.printStackTrace(); 
   } 
 } 
 return file; 
  } 
} 

 如果不能下载,请先将indos防火墙关闭。

Copyright © 2016-2025 www.jianfeikang.com 建飞家电维修 版权所有 Power by