源碼如下:
[java]
<span style="font-size:18px">
</span>
[java] view plaincopyprint?
<span style="font-size:18px">public class LoadTestActivity extends Activity {
private TextView tv;
private URL url;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
String urlStr="http://www.eoeandroid.com/thread-108676-1-1.html";
tv.setText(getWebText(urlStr));
//loadToSdcard(urlStr,"/TTTTT","Ada的文件.txt");
loadToLocation(urlStr,"Abc.txt");
}
//初始化組件
public void init(){
tv=(TextView)findViewById(R.id.tv);
}
//獲取文件流
private InputStream getInputStream(String urlStr){
InputStream is = null;
try {
url=new URL(urlStr);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5000);
conn.connect();
is=conn.getInputStream();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return is;
}
//獲取網頁文本內容
private String getWebText(String urlStr){
InputStream is=getInputStream(urlStr);
StringBuffer sb=new StringBuffer();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String s="";
try {
while((s=br.readLine())!=null){
sb.append(s);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("TAG", "流文件讀寫錯誤");
}
finally{
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return sb.toString();
}
//下載文件
private void downloader(InputStream is,String path,String filename){
String filepath=null;
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
filepath=Environment.getExternalStorageDirectory()+path;
}
else{
Toast.makeText(LoadTestActivity.this, "SDCard異常,請檢查SDCard是否安裝正確!", Toast.LENGTH_LONG).show();
}
if(!filepathExist(filepath)){
createFilepath(filepath);
}
if(!fileExist(filepath+"/"+filename)){
createFile(is,filepath,filename);
}
}
//判斷文件路徑是否存在
private boolean filepathExist(String filepath){
File file=new File(filepath);
return file.exists();
}
//創建文件路徑
private void createFilepath(String filepath){
File file=new File(filepath);
file.mkdirs();
}
//判斷文件是否存在
private boolean fileExist(String filename){
return filepathExist(filename);
}
//創建文件
private void createFile(InputStream is,String filepath,String filename){
File file=new File(filepath+"/"+filename);
OutputStream os=null;
try {
os=new FileOutputStream(file);
int len=0;
byte[] buffer=new byte[1024];
while((len=is.read(buffer))!=-1){
os.write(buffer, 0, len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//下載文件到SDCard
private void loadToSdcard(String url,String filepath,String filename){
InputStream is=getInputStream(url);
downloader(is,filepath,filename);
}
//下載文件到應用所在的本地目錄
private void loadToLocation(String urlStr,String filename){
InputStream is=getInputStream(urlStr);
OutputStream os=null;
try {
os=this.openFileOutput(filename, Context.MODE_WORLD_READABLE|Context.MODE_WORLD_WRITEABLE);
byte[] buffer=new byte[1024];
int len=0;
while((len=is.read(buffer))!=-1){
os.write(buffer, 0, len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}</span>
[java]
<span style="font-size:18px">
</span>
不要忘記了在AndroidManifest.xml中添加權限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
注意:
一、loadToLocation(String urlStr,String filename)這個方法是把下載的內容放到以filename命名的文件當中,這個以filename命名的文件被放在應用所在的本地目錄,可以通過以下方法來找到並查看它當中的內容:
在Eclipse中按以下路徑進行查找:Window---Open perspective---Hierarchy View---File Explorer---data---data---<package name,該程序所在包的包名>---files
通過上面的步驟就可以找到該文件了。
二、loadToSdcard(String url,String filepath,String filename)這個方法是把下載的內容放到sdcard中的以filepath命名的文件夾中的那個以filename命名的文件當中。可以打開android手機當中的“文件管理器”---sdcard---以filepath命名的文件夾,找到以filename命名的文件來查看所下載的內容。