http://www.unood.me/2013/09/android-http.html

 

[Android程式範例] http網頁下載檔案或文字

主要在使用時只要一句話! 
呼叫的方法是: 

1
int result = httpDownloader.downFile("http://tpdb.speed2.hinet.net/test_010m.zip", "Android/yilan/", "hinet.zip");


會需要一些自訂類別的支持分別有下載用的HTTPDownload還有寫入檔案到SD卡的FileUtils
首先先展示一下HTTPDownload類別的定義 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
 
public class HTTPDownload
{
    /**
     * 根據URL下載文件,前提是這個文件當中的內容是文本,函數的返回值就是文件當中的內容
     * 1.創建一個URL對象
     * 2.通過URL對象,創建一個HttpURLConnection對象
     * 3.得到InputStram
     * 4.從InputStream當中讀取數據
     */
    private URL url = null;
 
    public String downStr(String urlStr)//下載字元流的方法
    {
 /**
  * String和StringBuffer他們都可以存儲和操作字元串,即包含多個字元的字元串數據。
  * String類是字元串常量,是不可更改的常量。而StringBuffer是字元串變數,它的對象是可以擴充和修改的。
  */
 StringBuffer sb = new StringBuffer();
 String line = null;
 BufferedReader buffer = null;//BufferedReader類用於從緩衝區中讀取內容
 
 try
 {
     /**
      * 因為直接使用InputStream不好用,多以嵌套了BufferedReader,這個是讀取字元流的固定格式。
      */
 
     url = new URL(urlStr);// 創建一個URL對象 
     HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();// 創建一個Http連接
     buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));// 使用IO流讀取數據
 
     while ((line = buffer.readLine()) != null)
     {
  sb.append(line);
     }
 }
 catch (Exception e)
 {
     e.printStackTrace();
 }
 finally
 {
     try
     {
  buffer.close();
     }
     catch (Exception e)
     {
  e.printStackTrace();
     }
 }
 return sb.toString();
    }
 
    /**
     * -1:代表下載文件出錯
     * 0:代表下載文件成功
     * 1:代表文件已經存在
     */
    public int downFile(String urlStr, String path, String fileName)//下載文件的方法
    {
 InputStream inputStream = null;
 try
 {
     FileUtils fileUtils = new FileUtils();
 
     if (fileUtils.isFileExist(path + fileName))
     {
  boolean is = fileUtils.isFileExistDelete(path + fileName);
  if (is == false) return -1;
     }
     inputStream = getInputStreamFromUrl(urlStr);
     File resultFile = fileUtils.write2SDFromInput(path, fileName, inputStream);
     if (resultFile == null)
     {
  return -1;
     }
 
 }
 catch (Exception e)
 {
     e.printStackTrace();
     return -1;
 }
 finally
 {
     try
     {
  inputStream.close();
     }
     catch (Exception e)
     {
  e.printStackTrace();
     }
 }
 return 0;
    }
 
    /**
     * 根據URL得到輸入流
     */
    public InputStream getInputStreamFromUrl(String urlStr) throws MalformedURLException, IOException
    {
 url = new URL(urlStr);// 創建一個URL對象
 HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();// 創建一個Http連接
 InputStream inputStream = urlConn.getInputStream();//得到輸入流
 
 return inputStream;
    }
}


再來展示一下FileUtils類別的定義 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.unood.yilan;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 
import android.os.Environment;
 
public class FileUtils
{
    private String SDPATH;
 
    public String getSDPATH()
    {
 return SDPATH;
    }
 
    public FileUtils()
    {
 //得到當前SDCARD存儲設備的目錄 /SDCARD, Environment.getExternalStorageDirectory()這個方法比較通用
 SDPATH = Environment.getExternalStorageDirectory() + "/";
    }
 
    /**
     * 在SD卡上創建文件
     */
    public File creatSDFile(String fileName) throws IOException
    {
 File file = new File(SDPATH + fileName);
 file.createNewFile();
 return file;
    }
 
    /**
     * 在SD卡上創建目錄
     */
    public File creatSDDir(String dirName)
    {
 File dir = new File(SDPATH + dirName);
 dir.mkdir();
 return dir;
    }
 
    /**
     * 判斷SD卡上的文件夾是否存在
     */
    public boolean isFileExist(String fileName)
    {
 File file = new File(SDPATH + fileName);
 return file.exists();
    }
     
    /**
     * 判斷SD卡上的文件夾是否存在且刪除
     */
    public boolean isFileExistDelete(String fileName)
    {
 File file = new File(SDPATH + fileName);
 return file.delete();
    }
 
    /**
     * 將一個InputStream裡面的數據寫入到SD卡中
     */
    public File write2SDFromInput(String path, String fileName, InputStream input)
    {
 File file = null;
 OutputStream output = null;
 try
 //InputStream裡面的數據寫入到SD卡中的固定方法
 {
     creatSDDir(path);
     file = creatSDFile(path + fileName);
     output = new FileOutputStream(file);
     byte buffer[] = new byte[4 * 1024];
     while ((input.read(buffer)) != -1)
     {
  output.write(buffer);
     }
     output.flush();
 }
 catch (Exception e)
 {
     e.printStackTrace();
 }
 finally
 {
     try
     {
  output.close();
     }
     catch (Exception e)
     {
  e.printStackTrace();
     }
 }
 return file;
    }
}

[Android程式範例] http網頁下載檔案或文字

主要在使用時只要一句話! 
呼叫的方法是: 

1
int result = httpDownloader.downFile("http://tpdb.speed2.hinet.net/test_010m.zip", "Android/yilan/", "hinet.zip");

會需要一些自訂類別的支持分別有下載用的HTTPDownload還有寫入檔案到SD卡的FileUtils
首先先展示一下HTTPDownload類別的定義 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
 
public class HTTPDownload
{
    /**
     * 根據URL下載文件,前提是這個文件當中的內容是文本,函數的返回值就是文件當中的內容
     * 1.創建一個URL對象
     * 2.通過URL對象,創建一個HttpURLConnection對象
     * 3.得到InputStram
     * 4.從InputStream當中讀取數據
     */
    private URL url = null;
 
    public String downStr(String urlStr)//下載字元流的方法
    {
 /**
  * String和StringBuffer他們都可以存儲和操作字元串,即包含多個字元的字元串數據。
  * String類是字元串常量,是不可更改的常量。而StringBuffer是字元串變數,它的對象是可以擴充和修改的。
  */
 StringBuffer sb = new StringBuffer();
 String line = null;
 BufferedReader buffer = null;//BufferedReader類用於從緩衝區中讀取內容
 
 try
 {
     /**
      * 因為直接使用InputStream不好用,多以嵌套了BufferedReader,這個是讀取字元流的固定格式。
      */
 
     url = new URL(urlStr);// 創建一個URL對象 
     HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();// 創建一個Http連接
     buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));// 使用IO流讀取數據
 
     while ((line = buffer.readLine()) != null)
     {
  sb.append(line);
     }
 }
 catch (Exception e)
 {
     e.printStackTrace();
 }
 finally
 {
     try
     {
  buffer.close();
     }
     catch (Exception e)
     {
  e.printStackTrace();
     }
 }
 return sb.toString();
    }
 
    /**
     * -1:代表下載文件出錯
     * 0:代表下載文件成功
     * 1:代表文件已經存在
     */
    public int downFile(String urlStr, String path, String fileName)//下載文件的方法
    {
 InputStream inputStream = null;
 try
 {
     FileUtils fileUtils = new FileUtils();
 
     if (fileUtils.isFileExist(path + fileName))
     {
  boolean is = fileUtils.isFileExistDelete(path + fileName);
  if (is == false) return -1;
     }
     inputStream = getInputStreamFromUrl(urlStr);
     File resultFile = fileUtils.write2SDFromInput(path, fileName, inputStream);
     if (resultFile == null)
     {
  return -1;
     }
 
 }
 catch (Exception e)
 {
     e.printStackTrace();
     return -1;
 }
 finally
 {
     try
     {
  inputStream.close();
     }
     catch (Exception e)
     {
  e.printStackTrace();
     }
 }
 return 0;
    }
 
    /**
     * 根據URL得到輸入流
     */
    public InputStream getInputStreamFromUrl(String urlStr) throws MalformedURLException, IOException
    {
 url = new URL(urlStr);// 創建一個URL對象
 HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();// 創建一個Http連接
 InputStream inputStream = urlConn.getInputStream();//得到輸入流
 
 return inputStream;
    }
}

再來展示一下FileUtils類別的定義 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.unood.yilan;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 
import android.os.Environment;
 
public class FileUtils
{
    private String SDPATH;
 
    public String getSDPATH()
    {
 return SDPATH;
    }
 
    public FileUtils()
    {
 //得到當前SDCARD存儲設備的目錄 /SDCARD, Environment.getExternalStorageDirectory()這個方法比較通用
 SDPATH = Environment.getExternalStorageDirectory() + "/";
    }
 
    /**
     * 在SD卡上創建文件
     */
    public File creatSDFile(String fileName) throws IOException
    {
 File file = new File(SDPATH + fileName);
 file.createNewFile();
 return file;
    }
 
    /**
     * 在SD卡上創建目錄
     */
    public File creatSDDir(String dirName)
    {
 File dir = new File(SDPATH + dirName);
 dir.mkdir();
 return dir;
    }
 
    /**
     * 判斷SD卡上的文件夾是否存在
     */
    public boolean isFileExist(String fileName)
    {
 File file = new File(SDPATH + fileName);
 return file.exists();
    }
     
    /**
     * 判斷SD卡上的文件夾是否存在且刪除
     */
    public boolean isFileExistDelete(String fileName)
    {
 File file = new File(SDPATH + fileName);
 return file.delete();
    }
 
    /**
     * 將一個InputStream裡面的數據寫入到SD卡中
     */
    public File write2SDFromInput(String path, String fileName, InputStream input)
    {
 File file = null;
 OutputStream output = null;
 try
 //InputStream裡面的數據寫入到SD卡中的固定方法
 {
     creatSDDir(path);
     file = creatSDFile(path + fileName);
     output = new FileOutputStream(file);
     byte buffer[] = new byte[4 * 1024];
     while ((input.read(buffer)) != -1)
     {
  output.write(buffer);
     }
     output.flush();
 }
 catch (Exception e)
 {
     e.printStackTrace();
 }
 finally
 {
     try
     {
  output.close();
     }
     catch (Exception e)
     {
  e.printStackTrace();
     }
 }
 return file;
    }
}
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()