1 . 使用 http 协议下载文件
A ) : 创建一个 HttpURLConection 对象
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
B ) : 获取一个 InputStream
urlConn.getInputStream();
C ) : 访问网络的权限
android.permission.INTERNET
1 . 声明控件对象 ( 略 )
2 . 获得控件对象 ( 略 )
3 . 绑定事件 ( 略 )
4 . 创建监听器对象
// 封装方法 httpDownloader
public class DownloadHelper { private URL url = null; public String download(String newUrl){ StringBuffer sb = new StringBuffer(); String line = null; BufferedReader br = null; //创建一个url对象; try { url = new URL(newUrl); //创建一个http连接 HttpURLConnection urlConn = (HttpURLConnection)url.openConnection(); br = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); while((line = br.readLine())!=null){ sb.append(line); System.out.println(sb.toString()); } } catch (Exception e) { e.printStackTrace(); } finally{ try{ br.close(); }catch (Exception e) { e.printStackTrace(); } } return sb.toString(); } //返回-1下载文件出错,返回0下载成功,返回1文件已经存在 public int downFile(String urlStr,String path,String fileName){ try { InputStream is = null; FileUtils fileUtils = new FileUtils(); if(fileUtils.existSDFile(path+fileName)){ return 1; }else{ //inputStream = 上个从网络上获得的输入流 is = getInputStreamFromUrl(urlStr); File resultFile = fileUtils.write2SDCARDFromInputSteam(path, fileName, is); if(resultFile==null)return -1; } } catch (Exception e) { e.printStackTrace(); } return 0; } public InputStream getInputStreamFromUrl(String newUrl){ URL url = null; HttpURLConnection httpURLConnection = null; InputStream is = null; try { url = new URL(newUrl); httpURLConnection = (HttpURLConnection)url.openConnection(); is = httpURLConnection.getInputStream(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return is; } }
public class FileUtils { private String SDCARD = null; public FileUtils() { SDCARD = Environment.getExternalStorageDirectory() + "/"; } // 创建一个目录 public File createSDDir(String dirName) { File fileDir = new File(SDCARD + dirName); fileDir.mkdir(); return fileDir; } // 创建一个文件; public File createSDFile(String fileName) throws IOException { File file = new File(SDCARD+fileName); //注意在这里一定要加上主目录 SDCARD中,才可以,不然会找不到目录 。 file.createNewFile(); return file; } // 判断SD卡上的文件是不是存在; public boolean existSDFile(String fileName) { File file = new File(SDCARD + fileName); return file.exists(); } // 将一个流对象写入SDCARD public File write2SDCARDFromInputSteam(String path, String fileName, InputStream is) { File file = null; OutputStream os = null; try { createSDDir(path); file = createSDFile(path + fileName); os = new FileOutputStream(file); byte[] buffer = new byte[4 * 1024]; while (is.read(buffer) != -1) { os.write(buffer); } os.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { os.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } } return file; } }
DownloadActivity.java
package com.example.android.apis; import com.example.android.download.DownloadHelper; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class DownloadActivity extends Activity { /** Called when the activity is first created. */ Button down_txt = null; Button down_mp3 = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); down_txt = (Button)findViewById(R.id.download_txt); down_mp3 = (Button)findViewById(R.id.download_mp3); down_txt.setOnClickListener(new MyDownload_txt()); down_mp3.setOnClickListener(new MyDownload_mp3()); } class MyDownload_txt implements OnClickListener{ @Override public void onClick(View v) { DownloadHelper downloadHelper = new DownloadHelper(); downloadHelper.download("http://192.168.137.3:8080/test.lrc"); } } class MyDownload_mp3 implements OnClickListener{ @Override public void onClick(View v) { DownloadHelper downloadHelper = new DownloadHelper(); downloadHelper.downFile("http://192.168.137.3:8080/test.lrc", "test/","test.mp3"); } } }