1 import mars.utils.HttpDownloader; 2 import android.app.Activity; 3 import android.os.Bundle; 4 import android.view.View; 5 import android.view.View.OnClickListener; 6 import android.widget.Button; 7 8 public class Download extends Activity { 9 /** Called when the activity is first created. */ 10 //定义两个按键 11 private Button downloadTxtButton; 12 private Button downloadMp3Button; 13 @Override 14 //创建主窗口 15 public void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.main); 18 downloadTxtButton = (Button)findViewById(R.id.downloadTxt); 19 downloadTxtButton.setOnClickListener(new DownloadTxtListener()); 20 downloadMp3Button = (Button)findViewById(R.id.downloadMp3); 21 downloadMp3Button.setOnClickListener(new DownloadMp3Listener()); 22 } 23 24 //实现OnClickListener接口 25 class DownloadTxtListener implements OnClickListener{ 26 27 @Override 28 public void onClick(View v) { 29 //新建自己定义的HttpDownloader对象 30 HttpDownloader httpDownloader = new HttpDownloader(); 31 //得到下载的文本文件的字符串 http://image.baidu.com/channel/listdownload?word=download&ie=utf8&countop=0&fr=detail&url=http://g.hiphotos.baidu.com/album/w%3D2048/sign=4106f3c3d000baa1ba2c40bb7328b812/0e2442a7d933c8950f78a654d01373f08202009e.jpg&col=%E7%BE%8E%E5%A5%B3&image_id=4815797214&tag=%E5%B0%8F%E6%B8%85%E6%96%B0 32 String lrc = httpDownloader.download("http://image.baidu.com/channel/listdownload?word=download&ie=utf8&countop=0&fr=detail&url=http://g.hiphotos.baidu.com/album/w%3D2048/sign=4106f3c3d000baa1ba2c40bb7328b812/0e2442a7d933c8950f78a654d01373f08202009e.jpg&col=%E7%BE%8E%E5%A5%B3&image_id=4815797214&tag=%E5%B0%8F%E6%B8%85%E6%96%B0"); 33 System.out.println(lrc); 34 } 35 36 } 37 class DownloadMp3Listener implements OnClickListener{ 38 39 @Override 40 public void onClick(View v) { 41 // TODO Auto-generated method stub 42 HttpDownloader httpDownloader = new HttpDownloader(); 43 int result = httpDownloader.downFile("http://image.baidu.com/channel/listdownload?word=download&ie=utf8&countop=0&fr=detail&url=http://g.hiphotos.baidu.com/album/w%3D2048/sign=4106f3c3d000baa1ba2c40bb7328b812/0e2442a7d933c8950f78a654d01373f08202009e.jpg&col=%E7%BE%8E%E5%A5%B3&image_id=4815797214&tag=%E5%B0%8F%E6%B8%85%E6%96%B0", "voa/", "a1.jpg"); 44 System.out.println(result); 45 } 46 47 } 48 49 }
自定义的类HttpDownloader
1 import java.io.BufferedReader; 2 import java.io.File; 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.InputStreamReader; 6 import java.net.HttpURLConnection; 7 import java.net.MalformedURLException; 8 import java.net.URL; 9 10 11 public class HttpDownloader { 12 private URL url = null; 13 14 /** 15 * 根据URL下载文件,前提是这个文件当中的内容是文本,函数的返回值就是文件当中的内容 16 * 1.创建一个URL对象 17 * 2.通过URL对象,创建一个HttpURLConnection对象 18 * 3.得到InputStram 19 * 4.从InputStream当中读取数据 20 * @param urlStr 21 * @return 22 */ 23 public String download(String urlStr) { 24 //字符缓冲区 25 StringBuffer sb = new StringBuffer(); 26 String line = null; 27 //io里的字符缓冲流对象 28 BufferedReader buffer = null; 29 try { 30 // 创建一个URL对象 31 url = new URL(urlStr); 32 // 创建一个Http连接 33 HttpURLConnection urlConn = (HttpURLConnection) url 34 .openConnection(); 35 // 使用IO流读取数据,调用HttpURLConnection对象的getInputStream方法得到下载对象的InputStream,再套上其他IO管 36 buffer = new BufferedReader(new InputStreamReader(urlConn 37 .getInputStream())); 38 //读取一行 39 while ((line = buffer.readLine()) != null) { 40 //加到字符缓冲区的字符后面 41 sb.append(line); 42 } 43 } catch (Exception e) { 44 e.printStackTrace(); 45 } finally { 46 try { 47 buffer.close(); 48 } catch (Exception e) { 49 e.printStackTrace(); 50 } 51 } 52 return sb.toString(); 53 } 54 55 /** 56 * 该函数返回整形 -1:代表下载文件出错 0:代表下载文件成功 1:代表文件已经存在。(文件URL地址,文件要存储的路径,文件名字) 57 */ 58 public int downFile(String urlStr, String path, String fileName) { 59 InputStream inputStream = null; 60 try { 61 //新建文件工具对象 62 FileUtils fileUtils = new FileUtils(); 63 //先判断文件是否存在 64 if (fileUtils.isFileExist(path + fileName)) { 65 return 1; 66 } else { 67 //从URL中得到输出管道inputstream即输入流 68 inputStream = getInputStreamFromUrl(urlStr); 69 //调用文件工具的写数据到SD卡方法返回一个文件对象(完整文件) 70 File resultFile = fileUtils.write2SDFromInput(path,fileName, inputStream); 71 if (resultFile == null) { 72 return -1; 73 } 74 } 75 } catch (Exception e) { 76 e.printStackTrace(); 77 return -1; 78 } finally { 79 try { 80 inputStream.close(); 81 } catch (Exception e) { 82 e.printStackTrace(); 83 } 84 } 85 return 0; 86 } 87 88 /** 89 * 根据URL得到输入流 90 * 91 * @param urlStr 92 * @return 93 * @throws MalformedURLException 94 * @throws IOException 95 */ 96 public InputStream getInputStreamFromUrl(String urlStr) 97 throws MalformedURLException, IOException { 98 //根据string新建URL 99 url = new URL(urlStr); 100 //调用URL的openConnection方法并下行转换成HttpURLConnection对象 101 HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); 102 //再调用HttpURLConnection对象的getInputStream方法取得URL的输入流即输出管道 103 InputStream inputStream = urlConn.getInputStream(); 104 //返回管道 105 return inputStream; 106 } 107 }
自定义文件工具类
1 import java.io.File; 2 import java.io.FileOutputStream; 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.OutputStream; 6 7 import android.os.Environment; 8 9 public class FileUtils { 10 private String SDPATH; 11 12 //得到SD卡文件所在的目录即SD卡路径 13 public String getSDPATH() { 14 return SDPATH; 15 } 16 public FileUtils() { 17 //得到当前外部存储设备的目录 18 // /SDCARD 19 SDPATH = Environment.getExternalStorageDirectory() + "/"; 20 } 21 /** 22 * 在SD卡上创建文件 23 * 24 * @throws IOException 25 */ 26 public File creatSDFile(String fileName) throws IOException { 27 //首先先创建File文件对象 28 File file = new File(SDPATH + fileName); 29 //调用File的createNewFile方法创建一个空的文件 30 file.createNewFile(); 31 //返回文件对象 32 return file; 33 } 34 35 /** 36 * 在SD卡上创建目录 37 * 38 * @param dirName 39 */ 40 public File creatSDDir(String dirName) { 41 File dir = new File(SDPATH + dirName); 42 dir.mkdirs(); 43 return dir; 44 } 45 46 /** 47 * 判断SD卡上的文件夹是否存在 48 */ 49 public boolean isFileExist(String fileName){ 50 File file = new File(SDPATH + fileName); 51 return file.exists(); 52 } 53 54 /** 55 * 将一个InputStream里面的数据写入到SD卡中,即从InputStream管道读取数据写到SD卡中 56 */ 57 public File write2SDFromInput(String path,String fileName,InputStream input){ 58 File file = null; 59 OutputStream output = null; 60 try{ 61 //建立文件目录 62 creatSDDir(path); 63 //创建空文件 64 file = creatSDFile(path + fileName); 65 //把新建的输入管道插到新建的空文件上 66 output = new FileOutputStream(file); 67 //定义缓冲区大小为4K 68 byte buffer [] = new byte[4 * 1024]; 69 //从输出管道读取数据到缓冲区中,然后通过输入管道从缓冲区写入数据到文件中 70 while((input.read(buffer)) != -1){ 71 output.write(buffer); 72 } 73 //管道清空 74 output.flush(); 75 } 76 catch(Exception e){ 77 e.printStackTrace(); 78 } 79 finally{ 80 try{ 81 output.close(); 82 } 83 catch(Exception e){ 84 e.printStackTrace(); 85 } 86 } 87 return file; 88 } 89 90 }
最后不要忘了manifest加SDcard权限:
1 <uses-sdk android:minSdkVersion="4" /> 2 3 <uses-permission android:name="android.permission.INTERNET"/> 4 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>