有三个java类

Download.java

View Code
 1 package tk.download;
 2 
 3 
 4 import tk.utils.HttpDownloader;
 5 import android.os.Bundle;
 6 import android.R.integer;
 7 import android.app.Activity;
 8 import android.view.Menu;
 9 import android.view.View;
10 import android.view.View.OnClickListener;
11 import android.widget.Button;
12 
13 public class Download extends Activity {
14     
15     /**
16      *创建一个HttpURLConnection对象
17      *HttpURLConnection urlConn=(HttpURLConnection)url.openConnection();
18      *获得一个InputStream对象
19      *urlConn.getInputStream()
20      *访问网络的权限(在mainfest中声明)
21      *android.permission.INTERNET 
22      * 
23      *访问SDCARD
24      *得到当前设备SD卡的目录
25      *Environment.getExternalStorageDirectory()
26      *访问SD卡的权限
27      *android.permission.WRITE_EXTERNAL_STORAGE
28      * 
29      * /
30     /**Called when the activity is first created.*/
31     
32     private Button downloadTxtButton;
33     private Button downloadMp3Button;
34 
35     
36     @Override
37     public void onCreate(Bundle savedInstanceState) {
38         super.onCreate(savedInstanceState);
39         setContentView(R.layout.main);
40         downloadMp3Button=(Button)findViewById(R.id.downloadMp3);
41         downloadMp3Button.setOnClickListener(new DownloadMp3Listener());
42         downloadTxtButton=(Button)findViewById(R.id.downloadTxt);
43         downloadTxtButton.setOnClickListener(new DownloadTxtListener());
44         
45         
46     }
47     
48     class DownloadTxtListener implements OnClickListener{
49 
50         @Override
51         public void onClick(View v) {
52             HttpDownloader httpDownloader=new HttpDownloader();
53             System.out.println("begin");
54             String lrc = httpDownloader.download("http://125.222.201.90/home/download4.asp?FileName=hello.txt");
55             System.out.println(lrc);
56             System.out.println("end");
57             
58             
59         }
60         
61         
62     }
63     
64     class DownloadMp3Listener implements OnClickListener{
65 
66         @Override
67         public void onClick(View v) {
68             HttpDownloader httpDownloader=new HttpDownloader();
69             System.out.println("begin");
70             int result = httpDownloader.downFile("http://125.222.201.90/home/download4.asp?FileName=Layout_04.apk", "tk/", "test.apk");
71             System.out.println(result);
72             
73         }
74 
75     }
76     
77     
78 
79     @Override
80     public boolean onCreateOptionsMenu(Menu menu) {
81         getMenuInflater().inflate(R.menu.main, menu);
82         return true;
83     }
84 }

FileUtils.java

View Code
 1 package tk.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 
19     public FileUtils() {
20         //得到当前外部存储设备的目录  /SDCARD
21         SDPATH = Environment.getExternalStorageDirectory() + "/";
22         //SDPATH ="/";
23     }
24 
25 /**
26 * 在SD卡上创建目录
27 * 
28 * @param dirName
29 */
30 
31     public File creatSDDir(String dirName) {
32         File file = new File(SDPATH + dirName);
33         file.mkdirs();
34         return file;
35     }
36 
37 /**
38 * 在SD卡上创建文件
39 * 
40 * @throws IOException
41 */
42 
43     public File creatSDFile(String fileName) throws IOException {
44         File file = new File(SDPATH + fileName);
45         file.createNewFile();
46         return file;
47     }
48 
49  
50 
51 /**
52 * 判断SD卡上的文件夹是否存在
53 */
54 
55     public boolean isFileExist(String fileName){
56         File file = new File(SDPATH + fileName);
57         return file.exists();
58     }
59 
60 /**
61 * 将一个InputStream里面的数据写入到SD卡中
62 */
63 
64     public File write2SDFromInput(String path,String fileName,InputStream inputStream){
65     
66         File file = null;
67         OutputStream outputStream = null;
68         
69         try{
70             creatSDDir(path);
71             file = creatSDFile(path + fileName);
72             outputStream = new FileOutputStream(file);
73             byte buffer [] = new byte[4 * 1024];
74             while((inputStream.read(buffer)) != -1){
75                 
76                 outputStream.write(buffer);
77                 
78             }
79             outputStream.flush();
80             
81         }catch(Exception e){
82             
83             e.printStackTrace();
84             
85         }finally{
86             
87             try{
88                 outputStream.close();
89             }catch(Exception e){
90                 e.printStackTrace();
91             }
92             
93         }
94         
95         return file;
96     
97     }
98 
99 }

HttpDownloader.java

View Code
  1 package tk.utils;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.File;
  5 import java.io.IOException;
  6 import java.io.InputStream;
  7 import java.io.InputStreamReader;
  8 import java.net.HttpURLConnection;
  9 import java.net.MalformedURLException;
 10 import java.net.URL;
 11 
 12 public class HttpDownloader {
 13 
 14     private URL url = null;
 15     
 16     /**
 17      * 根据URL下载文件,前提是这个文件当中的内容是文本,函数的返回值就是文件当中的内容
 18      * 1.创建一个URL对象
 19      * 2.通过URL对象,创建一个HttpURLConnection对象
 20      * 3.得到Inputstream
 21      * 4.从InputStream当中读取数据
 22      * @param urlStr
 23      * @return
 24      */
 25     public String download(String urlStr){
 26         StringBuffer sb = new StringBuffer();
 27         String line = null;
 28         
 29         BufferedReader buffer = null;
 30         
 31         try{
 32             //创建一个Http对象
 33             url = new URL(urlStr);
 34             //创建一个Http连接
 35             HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
 36             //使用IO流读取数据
 37             buffer = new BufferedReader(new InputStreamReader(urlconn.getInputStream()));
 38             
 39             while((line = buffer.readLine())!=null){
 40                 sb.append(line);
 41             }
 42         }catch(Exception e){
 43             e.printStackTrace();            
 44         }finally{
 45             try{
 46                 buffer.close();
 47             }catch(Exception e){
 48                 e.printStackTrace();
 49             }
 50         }
 51         
 52         return sb.toString();
 53     }
 54     /**
 55      *该函数返回整形-1;代表下载文件出错,0代表下载文件成功,1 代表文件已存在
 56      */
 57     public int downFile(String urlStr, String path, String fileName) {
 58     
 59         InputStream inputStream = null;
 60         
 61         try {
 62             FileUtils fileUtils = new FileUtils(); 
 63             if (fileUtils.isFileExist(path + fileName)) {
 64                 return 1; //文件已经存在
 65             } else {
 66                 inputStream =getInputStreamFromUrl(urlStr);
 67                 if(inputStream==null)return 2;
 68                 File resultFile =fileUtils.write2SDFromInput(path,fileName,inputStream);  
 69                 if (resultFile == null){
 70                     return -1;  //下载文件出错
 71                 }
 72             }
 73         } catch (Exception e) {
 74             e.printStackTrace();
 75             return -1;
 76         } finally {
 77             try {
 78                 inputStream.close();
 79             } catch (Exception e) {
 80                 e.printStackTrace();
 81             }
 82         }
 83     return 0;
 84     }
 85     
 86      
 87     
 88     public InputStream getInputStreamFromUrl(String urlStr) throws MalformedURLException, IOException {
 89     
 90         InputStream inputStream=null;
 91         
 92         try{
 93             // 创建一个URL对象
 94            url = new URL(urlStr);
 95            // 创建一个Http连接
 96            HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
 97            // 使用IO流读取数据
 98            inputStream = urlConn.getInputStream();
 99            return inputStream;
100         }catch (Exception e) {
101             e.printStackTrace();
102             return null;
103         }finally {
104            inputStream.close();
105         }
106     }
107 
108 }

androidmainfest.xml

View Code

main.xml

View Code
 1 <?xml version= "1.0" encoding ="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation= "vertical"
 4     android:layout_width= "fill_parent"
 5     android:layout_height= "fill_parent"
 6     >
 7     
 8     <Button 
 9         android:id="@+id/downloadTxt"
10         android:layout_width="fill_parent"
11         android:layout_height="wrap_content"
12         android:text="downloadtxt"
13         />
14     
15      <Button 
16         android:id="@+id/downloadMp3"
17         android:layout_width="fill_parent"
18         android:layout_height="wrap_content"
19         android:text="downloadMP3"
20         />
21 </LinearLayout>

 

 

 

 

 

posted on 2012-08-21 19:27  tiankonguse  阅读(741)  评论(0编辑  收藏  举报