赞
踩
1.AndroidMainfest.xml中设置权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
2.在FileUtils中封装将数据保存到SD卡的方法
package com.vino.utils;
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
SDPATH = Environment.getExternalStorageDirectory() + "/";
}
/**
* 在SD卡上创建文件
*
* @throws IOException
*/
public File creatSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
file.createNewFile();
return file;
}
/**
* 在SD卡上创建目录
*
* @param dirName
*/
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();
}
/**
* 将一个InputStream里面的数据写入到SD卡中
*/
public File write2SDFromInput(String path,String fileName,InputStream input){
File file = null;
OutputStream output = null;
try{
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;
}
}
3.新建HttpDownloader,用于在MainActivity中调用FileUtils内的方法
package com.vino.utils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpDownloader {
private URL url=null;
FileUtils fileUtils=new FileUtils();
public int downfile(String urlStr,String path,String fileName)
{
if(fileUtils.isFileExist(path+fileName))
{return 1;}
else{
try {
InputStream input=null;
input = getInputStream(urlStr);
File resultFile=fileUtils.write2SDFromInput(path, fileName, input);
if(resultFile==null)
{
return -1;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return 0;
}
//由于得到一个InputStream对象是所有文件处理前必须的操作,所以将这个操作封装成了一个方法
public InputStream getInputStream(String urlStr) throws IOException
{
InputStream is=null;
try {
url=new URL(urlStr);
HttpURLConnection urlConn=(HttpURLConnection)url.openConnection();
is=urlConn.getInputStream();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return is;
}
}
4.在MainActivity中运行实例
package com.vino.download;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.vino.utils.HttpDownloader;
public class MainActivity extends Activity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new DownloadListener());
}
class DownloadListener implements OnClickListener{
@Override
public void onClick(View arg0) {
Thread thread = new Thread(){
@Override
public void run(){
HttpDownloader httpDownLoader=new HttpDownloader();
httpDownLoader.downfile("http://192.168.0.102:8080/data/data.txt", "data/", "data.txt");
//使用Toast时相当于修改了UI,程序报错
// if(result==0)
// {
// Toast.makeText(MainActivity.this, "下载成功!", Toast.LENGTH_SHORT).show();
// }
// else if(result==1) {
// Toast.makeText(MainActivity.this, "已有文件!", Toast.LENGTH_SHORT).show();
// }
// else if(result==-1){
// Toast.makeText(MainActivity.this, "下载失败!", Toast.LENGTH_SHORT).show();
// }
}
};
thread.start();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
5.在adb中查看结果,data.txt即为下载的文件
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。