标签:
---恢复内容开始---
1.AndroidMainfest.xml中设置权限
1 <uses-permission android:name="android.permission.INTERNET"></uses-permission> 2 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
2.在FileUtils中封装将数据保存到SD卡的方法
1 package com.vino.utils; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.io.OutputStream; 8 9 import android.os.Environment; 10 11 public class FileUtils { 12 13 private String SDPATH; 14 15 public String getSDPATH() { 16 return SDPATH; 17 } 18 public FileUtils() { 19 //得到当前外部存储设备的目录 20 // /SDCARD 21 SDPATH = Environment.getExternalStorageDirectory() + "/"; 22 } 23 /** 24 * 在SD卡上创建文件 25 * 26 * @throws IOException 27 */ 28 public File creatSDFile(String fileName) throws IOException { 29 File file = new File(SDPATH + fileName); 30 file.createNewFile(); 31 return file; 32 } 33 34 /** 35 * 在SD卡上创建目录 36 * 37 * @param dirName 38 */ 39 public File creatSDDir(String dirName) { 40 File dir = new File(SDPATH + dirName); 41 dir.mkdir(); 42 return dir; 43 } 44 45 /** 46 * 判断SD卡上的文件夹是否存在 47 */ 48 public boolean isFileExist(String fileName){ 49 File file = new File(SDPATH + fileName); 50 return file.exists(); 51 } 52 53 /** 54 * 将一个InputStream里面的数据写入到SD卡中 55 */ 56 public File write2SDFromInput(String path,String fileName,InputStream input){ 57 File file = null; 58 OutputStream output = null; 59 try{ 60 creatSDDir(path); 61 file = creatSDFile(path + fileName); 62 output = new FileOutputStream(file); 63 byte buffer [] = new byte[4 * 1024]; 64 while((input.read(buffer)) != -1){ 65 output.write(buffer); 66 } 67 output.flush(); 68 } 69 catch(Exception e){ 70 e.printStackTrace(); 71 } 72 finally{ 73 try{ 74 output.close(); 75 } 76 catch(Exception e){ 77 e.printStackTrace(); 78 } 79 } 80 return file; 81 } 82 }
3.新建HttpDownloader,用于在MainActivity中调用FileUtils内的方法
1 package com.vino.utils; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.net.HttpURLConnection; 7 import java.net.MalformedURLException; 8 import java.net.URL; 9 10 public class HttpDownloader { 11 12 private URL url=null; 13 FileUtils fileUtils=new FileUtils(); 14 public int downfile(String urlStr,String path,String fileName) 15 { 16 if(fileUtils.isFileExist(path+fileName)) 17 {return 1;} 18 else{ 19 20 try { 21 InputStream input=null; 22 input = getInputStream(urlStr); 23 File resultFile=fileUtils.write2SDFromInput(path, fileName, input); 24 if(resultFile==null) 25 { 26 return -1; 27 } 28 } catch (IOException e) { 29 // TODO Auto-generated catch block 30 e.printStackTrace(); 31 } 32 33 } 34 return 0; 35 } 36 //由于得到一个InputStream对象是所有文件处理前必须的操作,所以将这个操作封装成了一个方法 37 public InputStream getInputStream(String urlStr) throws IOException 38 { 39 InputStream is=null; 40 try { 41 url=new URL(urlStr); 42 HttpURLConnection urlConn=(HttpURLConnection)url.openConnection(); 43 is=urlConn.getInputStream(); 44 45 } catch (MalformedURLException e) { 46 // TODO Auto-generated catch block 47 e.printStackTrace(); 48 } 49 50 return is; 51 } 52 }
4.在MainActivity中运行实例
1 package com.vino.download; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.Menu; 6 import android.view.MenuItem; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button; 10 11 import com.vino.utils.HttpDownloader; 12 13 public class MainActivity extends Activity { 14 15 private Button button; 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 button = (Button)findViewById(R.id.button); 21 button.setOnClickListener(new DownloadListener()); 22 } 23 24 class DownloadListener implements OnClickListener{ 25 26 @Override 27 public void onClick(View arg0) { 28 29 Thread thread = new Thread(){ 30 @Override 31 public void run(){ 32 HttpDownloader httpDownLoader=new HttpDownloader(); 33 httpDownLoader.downfile("http://192.168.0.102:8080/data/data.txt", "data/", "data.txt"); 34 //使用Toast时相当于修改了UI,程序报错 35 // if(result==0) 36 // { 37 // Toast.makeText(MainActivity.this, "下载成功!", Toast.LENGTH_SHORT).show(); 38 // } 39 // else if(result==1) { 40 // Toast.makeText(MainActivity.this, "已有文件!", Toast.LENGTH_SHORT).show(); 41 // } 42 // else if(result==-1){ 43 // Toast.makeText(MainActivity.this, "下载失败!", Toast.LENGTH_SHORT).show(); 44 // } 45 } 46 }; 47 thread.start(); 48 49 } 50 51 } 52 @Override 53 public boolean onCreateOptionsMenu(Menu menu) { 54 // Inflate the menu; this adds items to the action bar if it is present. 55 getMenuInflater().inflate(R.menu.main, menu); 56 return true; 57 } 58 59 @Override 60 public boolean onOptionsItemSelected(MenuItem item) { 61 // Handle action bar item clicks here. The action bar will 62 // automatically handle clicks on the Home/Up button, so long 63 // as you specify a parent activity in AndroidManifest.xml. 64 int id = item.getItemId(); 65 if (id == R.id.action_settings) { 66 return true; 67 } 68 return super.onOptionsItemSelected(item); 69 } 70 }
5.在adb中查看结果,data.txt即为下载的文件
注意:不能在主线程中通过Http下载文件
标签:
原文地址:http://www.cnblogs.com/vinozly/p/4758473.html