Android利用Http下載文件javascript
1、場景java
下載存文本文件和下載如mp3等大容量的文件android
界面網絡
2、代碼編寫app
1.AndroidMainfest.xml中配置ide
主要是解決網絡權限和寫SDCard的權限this
Java代碼
<?xml version="1.0" encoding="utf-8"?> spa
<manifest xmlns:android="http://schemas.android.com/apk/res/android" .net
package="linys.download" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Download" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<!-- 訪問網絡和操做SD卡 加入的兩個權限配置 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
2.Activity編寫
利用Http協議下載文件並存儲到SDCard
1.建立一個URL對象
2.經過URL對象,建立一個HttpURLConnection對象
3.獲得InputStream
4.從InputStream當中讀取數據
存到SDCard
1.取得SDCard路徑
2.利用讀取大文件的IO讀法,讀取文件
Java代碼
package linys.download;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
*
* @Project : Android_MyDownload
* @Desciption: 利用Http協議下載文件並存儲到SDCard
1.建立一個URL對象
2.經過URL對象,建立一個HttpURLConnection對象
3.獲得InputStream
4.從InputStream當中讀取數據
存到SDCard
1.取得SDCard路徑
2.利用讀取大文件的IO讀法,讀取文件
*
* @Author: LinYiSong
* @Date: 2011-3-25~2011-3-25
*/
public class MyDownload extends Activity {
private Button downFileBtn;
private Button downMP3Btn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
downFileBtn=(Button)this.findViewById(R.id.downFile);
downMP3Btn=(Button)this.findViewById(R.id.downMP3);
downFileBtn.setOnClickListener(new DownFileClickListener());
downMP3Btn.setOnClickListener(new DownMP3ClickListener());
}
/**
*
* @Project: Android_MyDownload
* @Desciption: 只能讀取文本文件,讀取mp3文件會出現內存溢出現象
* @Author: LinYiSong
* @Date: 2011-3-25~2011-3-25
*/
class DownFileClickListener implements OnClickListener{
@Override
public void onClick(View v) {
String urlStr="http://172.17.54.91:8080/download/down.txt";
try {
/*
* 經過URL取得HttpURLConnection
* 要網絡鏈接成功,需在AndroidMainfest.xml中進行權限配置
* <uses-permission android:name="android.permission.INTERNET" />
*/
URL url=new URL(urlStr);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
//取得inputStream,並進行讀取
InputStream input=conn.getInputStream();
BufferedReader in=new BufferedReader(new InputStreamReader(input));
String line=null;
StringBuffer sb=new StringBuffer();
while((line=in.readLine())!=null){
sb.append(line);
}
System.out.println(sb.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
*
* @Project: Android_MyDownload
* @Desciption: 讀取任意文件,並將文件保存到手機SDCard
* @Author: LinYiSong
* @Date: 2011-3-25~2011-3-25
*/
class DownMP3ClickListener implements OnClickListener{
@Override
public void onClick(View v) {
String urlStr="http://172.17.54.91:8080/download/1.mp3";
String path="file";
String fileName="2.mp3";
OutputStream output=null;
try {
/*
* 經過URL取得HttpURLConnection
* 要網絡鏈接成功,需在AndroidMainfest.xml中進行權限配置
* <uses-permission android:name="android.permission.INTERNET" />
*/
URL url=new URL(urlStr);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
//取得inputStream,並將流中的信息寫入SDCard
/*
* 寫前準備
* 1.在AndroidMainfest.xml中進行權限配置
* <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
* 取得寫入SDCard的權限
* 2.取得SDCard的路徑: Environment.getExternalStorageDirectory()
* 3.檢查要保存的文件上是否已經存在
* 4.不存在,新建文件夾,新建文件
* 5.將input流中的信息寫入SDCard
* 6.關閉流
*/
String SDCard=Environment.getExternalStorageDirectory()+"";
String pathName=SDCard+"/"+path+"/"+fileName;//文件存儲路徑
File file=new File(pathName);
InputStream input=conn.getInputStream();
if(file.exists()){
System.out.println("exits");
return;
}else{
String dir=SDCard+"/"+path;
new File(dir).mkdir();//新建文件夾
file.createNewFile();//新建文件
output=new FileOutputStream(file);
//讀取大文件
byte[] buffer=new byte[4*1024];
while(input.read(buffer)!=-1){
output.write(buffer);
}
output.flush();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
output.close();
System.out.println("success");
} catch (IOException e) {
System.out.println("fail");
e.printStackTrace();
}
}
}
}
}
Android_MyDownload.rar (45.8 KB)
下載次數: 354
Android_Download.rar (50.3 KB)
下載次數: 461