android 下载文件

import com.example.android.R;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity3 extends Activity {
private Button downfile = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
downfile = (Button) findViewById(R.id.downfile);

new Thread() {
@Override
public void run() {
// 你要执行的方法
downfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
HttpDownloader httpDownLoader = new HttpDownloader();
int result = httpDownLoader
.downfile(
"http://192.168.5.60:8080/statics/images//bdxz_btn.jpg",
"test/", "test.jpg");
// if (result == 0) {
// Toast.makeText(MainActivity3.this, "下载成功!",
// Toast.LENGTH_SHORT).show();
// } else if (result == 1) {
// Toast.makeText(MainActivity3.this, "已有文件!",
// Toast.LENGTH_SHORT).show();
// } else if (result == -1) {
// Toast.makeText(MainActivity3.this, "下载失败!",
// Toast.LENGTH_SHORT).show();
// }
}
});
// 执行完毕后给handler发送一个空消息
handler.sendEmptyMessage(0);
}
}.start();
}

// 定义Handler对象
private Handler handler = new Handler() {
@Override
// 当有消息发送出来的时候就执行Handler的这个方法
public void handleMessage(Message msg) {
super.handleMessage(msg);
// 处理UI
Log.e("MAINACTIVITY3", "" + msg);
}

};
}
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 createSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
file.createNewFile();
return file;
}

/**
* 在SD卡上创建目录
*
* @param dirName
*/
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();
}

/**


* 该函数返回整形-1:代表下载文件出错。
* 0:代表下载文件成功
* 1:代表下载文件经存在

 


* 将一个InputStream里面的数据写入到SD卡中
*/
public File write2SDFromInput(String path, String fileName,
InputStream input) {
File file = null;
OutputStream output = null;
try {
createSDDir(path);
file = createSDFile(path + fileName);
output = new FileOutputStream(file);
byte buffer[] = new byte[4 * 1024];
// while ((input.read(buffer)) != -1) {
// output.write(buffer);
// }

while (true) {
int temp = input.read(buffer, 0, buffer.length);
if (temp == -1) {
break;
}
output.write(buffer, 0, temp);
}

output.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return file;
}
}
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();
urlConn.setRequestProperty("Connection", "close");
urlConn.connect();
is = urlConn.getInputStream();

} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return is;
}
}

posted @ 2017-01-03 13:38  一点点征服  阅读(877)  评论(0编辑  收藏  举报