Android&MyDownload

com.zachary.download:

Download.java
 1 package com.zachary.download;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.Menu;
 6 import android.view.View;
 7 import android.view.View.OnClickListener;
 8 import android.widget.Button;
 9 import android.widget.EditText;
10 import android.widget.TextView;
11 import android.widget.Toast;
12 
13 import com.zachary.utils.HttpDownloader;
14 
15 public class Download extends Activity {
16     private Button downloadTxtButton = null;
17     private Button downloadMp3Button = null;
18     private EditText myEditText = null;
19     private TextView myTextView = null;
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.activity_download);
24         downloadTxtButton = (Button)findViewById(R.id.downloadTxtButton);
25         downloadMp3Button = (Button)findViewById(R.id.downloadMp3Button);
26         myEditText = (EditText)findViewById(R.id.myEditText);
27         myTextView = (TextView)findViewById(R.id.myTextView);
28         downloadTxtButton.setOnClickListener(new OnClickListener(){
29 
30             @Override
31             public void onClick(View arg0) {
32                 // TODO Auto-generated method stub
33                 String urlStr = myEditText.getText().toString();
34                 Toast.makeText(Download.this, "Begin to read the file", Toast.LENGTH_LONG).show();
35                 HttpDownloader myHttpDownloader = new HttpDownloader();
36                 String txt = myHttpDownloader.download(urlStr);
37                 Toast.makeText(Download.this, "Download the file", Toast.LENGTH_LONG).show();                
38                 myTextView.setText(txt);
39             }
40             
41         });
42         
43         downloadMp3Button.setOnClickListener(new OnClickListener(){
44 
45             @Override
46             public void onClick(View v) {
47                 // TODO Auto-generated method stub
48                 String urlStr = myEditText.getText().toString();
49                 HttpDownloader myHttpDownloader = new HttpDownloader();
50                 if(myHttpDownloader.downFile(urlStr, "myDownload/","a.mp3") == 0)
51                     myTextView.setText("Success!!!");
52                 else myTextView.setText("Oh,No!");
53             }
54             
55         });
56     }
57 
58     @Override
59     public boolean onCreateOptionsMenu(Menu menu) {
60         // Inflate the menu; this adds items to the action bar if it is present.
61         getMenuInflater().inflate(R.menu.download, menu);
62         return true;
63     }
64 
65 }

com.zachary.utils:

FileUtils.java
 1 package com.zachary.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     private String SDPATH;
13     
14     public String getSDPATH(){
15         return SDPATH;
16     }
17     public FileUtils(){
18         SDPATH = Environment.getExternalStorageDirectory() + "/";
19     }
20     
21     /**
22      * Create the file named filename on the SDCard
23      * @param filename
24      * @return
25      * @throws IOException
26      */
27     public File creatSDFile(String filename)    throws IOException{
28         File file = new File(SDPATH + filename);
29         file.createNewFile();
30         return file;
31     }
32     
33     /**
34      * Creat the dir from the dirName
35      * @param dirName
36      * @return    the File
37      */
38     public File creatSDDir(String dirName){
39         File dir = new File(SDPATH + dirName);
40         dir.mkdir();
41         return dir;
42     }
43     
44     public boolean isFileExist(String fileName){
45         File file = new File(SDPATH + fileName);
46         return file.exists();
47     }
48     
49     public File write2SDFromInput(String path, String fileName, InputStream input){
50         File file = null;
51         OutputStream output = null;
52         
53         try {
54             creatSDDir(path);
55             file = creatSDFile(path + fileName);
56             output = new FileOutputStream(file);
57             byte buffer[] = new byte[4 * 1024];
58             while((input.read(buffer)) != -1)
59                 output.write(buffer);
60             output.flush();
61         } catch (IOException e) {
62             // TODO Auto-generated catch block
63             e.printStackTrace();
64         }
65         finally{
66             try {
67                 output.close();
68             } catch (IOException e) {
69                 // TODO Auto-generated catch block
70                 e.printStackTrace();
71             }
72         }
73         return file;
74     }
75 }
HttpDownloader.java
 1 package com.zachary.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 
13 public class HttpDownloader {
14     private URL url = null;
15     
16     /**
17      * Using the URL to download the file of txt, and the return is the content of the file
18      * @param urlStr
19      * @return    
20      */
21     public String  download(String urlStr){
22         StringBuffer sb = new StringBuffer();
23         String line = null;
24         BufferedReader buffer = null;
25         try {
26             url = new URL(urlStr);
27             HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
28             
29             //在此使用了一种叫做装饰者的设计模式
30             //InputStream one Byte one times
31             //InputStreamReader one Char one times
32             //BufferedReader one line one times
33             buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
34             while((line = buffer.readLine())!=null)
35                 sb.append(line);
36         
37             
38         } catch (MalformedURLException e) {
39             // TODO Auto-generated catch block
40             e.printStackTrace();
41         } catch (IOException e) {
42             // TODO Auto-generated catch block
43             e.printStackTrace();
44         }finally{
45             try {
46                 buffer.close();
47             } catch (IOException e) {
48                 // TODO Auto-generated catch block
49                 e.printStackTrace();
50             }
51         }
52         return sb.toString();
53     }
54     
55     /**
56      * @param urlStr
57      * @param path
58      * @param fileName
59      * @return    -1:the file has an error; 0:the file has downloaded; 1:the file has exited 
60      */
61     public int downFile(String urlStr, String path, String fileName){
62         InputStream inputStream = null;
63         FileUtils fileUtils = new FileUtils();
64         if(fileUtils.isFileExist(path + fileName))
65             return 1;
66         else {
67             try {
68                 inputStream = getInputStreamFromUrl(urlStr);
69                 File resultFile = fileUtils.write2SDFromInput(path ,fileName, inputStream);
70                 if(resultFile == null)
71                     return -1;
72             } catch (IOException e) {
73                 // TODO Auto-generated catch block
74                 e.printStackTrace();
75                 return -1;
76             }finally{
77                 try {
78                     inputStream.close();
79                 } catch (IOException e) {
80                     // TODO Auto-generated catch block
81                     e.printStackTrace();
82                 }
83             }
84         }
85         return 0; 
86     }
87     public InputStream getInputStreamFromUrl(String urlStr) throws IOException{
88         url =new URL(urlStr);
89         HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
90         InputStream inputStream = urlConn.getInputStream();
91         return inputStream;
92     }
93 }
activity_download.xml
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context=".Download"
10     android:orientation="vertical" >
11 
12     <EditText
13         android:id="@+id/myEditText"
14         android:layout_width="fill_parent"
15         android:layout_height="wrap_content"
16         android:hint="Please enter the URL" />
17     <Button
18         android:id="@+id/downloadTxtButton"
19         android:layout_width="fill_parent"
20         android:layout_height="wrap_content"
21         android:text="Download Txt"/>
22     <Button
23         android:id="@+id/downloadMp3Button"
24         android:layout_width="fill_parent"
25         android:layout_height="wrap_content"
26         android:text="Download Mp3"/>"
27     <TextView
28         android:id="@+id/myTextView"
29         android:layout_width="fill_parent"
30         android:layout_height="wrap_content"
31         android:text="Something about download"/>
32 </LinearLayout>

 

另外,AndroidManifest.xml:加入以下权限

<uses-permission android:name="android.permission.INTERNET"/>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

posted on 2013-03-28 12:36  Zachary_wiz  阅读(198)  评论(0编辑  收藏  举报

导航