使用HTTP協議 下載檔案
檔案下載步驟:
1. 建立一個HttpURLConnection物件 HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
2. 獲得一個InputStream物件 urlConn.getInputStream();
下載的檔案當中的內容是文字,函式的返回值就是檔案當中的內容:
答:步驟1.建立一個URL物件, 2.通過URL物件,建立一個HttpURLConnection物件
3.得到InputStream, 4.從InputStream當中讀取資料
將下載的檔案寫入SDCARD:
得到當前裝置SD卡的目錄:Environment.getExternalStorageDirectory();
訪問SD卡的許可權,見注意。
HttpDownloader 類
package com.examples.utils;
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;
import android.app.backup.FileBackupHelper;
public class HttpDownloader {
URL url = null;
//下載文件檔案
public String download(String urlstr)
{
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader buffer = null;
try {
url = new URL(urlstr);
HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
buffer= new BufferedReader(new InputStreamReader(httpConn.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 input = null;
try {
FileUtils fileUtils = new FileUtils();
if(fileUtils.isFileExist(path + filename))
{
return 1;
}
else
{
input = getInputStreamFromUrl(urlstr);
File resultFile = fileUtils.write2SDFromInput(path, filename, input);
if(resultFile == null)
{
return -1;
}
}
} catch (IOException e) {
e.printStackTrace();
return -1;
}
return 0;
}
public InputStream getInputStreamFromUrl(String urlstr) throws IOException
{
url = new URL(urlstr);
HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
InputStream input = httpConn.getInputStream();
return input;
}
}
FileUtils 類
package com.examples.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import android.os.Environment;
public class FileUtils {
private String SDPATH;
//建構函式
public FileUtils()
{
//得到當前外部儲存裝置的目錄
SDPATH = Environment.getExternalStorageDirectory()+ "/"; //getPath方法看可以不要不
}
//返回SD卡目錄
public String getSDPATH()
{
return SDPATH;
}
//在SD卡上建立檔案
public File createSDFile(String filename) throws IOException
{
File file = new File(SDPATH +filename);
file.createNewFile();
return file;
}
//在SD卡上建立目錄
public File createSDDir(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();
}
//將一個InputStream裡的資料寫入SD卡中
public File write2SDFromInput(String path,String filename,InputStream input)
{
OutputStream output = null;
File file =null;
try
{
createSDDir(path);
file = createSDFile(path + filename);
output = new FileOutputStream(file);//構造FileOutputStream用於給檔案中寫入內容
byte buffer[] = new byte[4*1024];
while(input.read(buffer) !=-1)
{
output.write(buffer);
}
output.flush();
}catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
output.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
return file;
}
}
download 類
package com.examples.download;
import com.examples.utils.HttpDownloader;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class download extends Activity {
/** Called when the activity is first created. */
private Button downTxt;
private Button downFile;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
downTxt = (Button)findViewById(R.id.downtxt);
downFile = (Button)findViewById(R.id.downfile);
downTxt.setOnClickListener(new downTxtListener());
downFile.setOnClickListener(new downFileListener());
}
class downTxtListener implements OnClickListener
{
@Override
public void onClick(View v) {
HttpDownloader httpdownloader = new HttpDownloader();
String srt=httpdownloader.download("http://202.193.82.14:8080/vao1500/1.srt");
System.out.println(srt);
}
}
class downFileListener implements OnClickListener
{
@Override
public void onClick(View v) {
HttpDownloader httpdownloader = new HttpDownloader();
int result=httpdownloader.downfile("http://202.193.80.14:8080/vao1500/a1.mp3", "voa/", "a1.mp3");
System.out.println(result);
}
}
}
注意:訪問網路的許可權(在AndroidManifest.xml中設定) <uses-permission android:name="android.permission.INTERNET" />
具有寫的權利 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
InputStream是讀取資料,OutputStream是寫入資料。