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;
}
}

android 下载文件的更多相关文章

  1. Android 下载文件及写入SD卡

    Android 下载文件及写入SD卡,实例代码 <?xml version="1.0" encoding="utf-8"?> <LinearL ...

  2. 基于Android 下载文件时,更新UI简单帮助类

    因为在项目开发时.有这种简单需求,问谷歌,网络上也有好多Utils工具类,可是比較冗余.自己就简单的写了一个简单帮助类. /** * 下载文件,更新UI简单帮助类 * * @author jarlen ...

  3. android 下载文件,file的读写应用

    先看代码: public class MainActivity extends AppCompatActivity { String TAG = MainActivity.class.getCanon ...

  4. Android 下载文件 显示进度条

    加入两个权限 一个是联网,另一个是读写SD卡 <uses-permission android:name="android.permission.INTERNET">& ...

  5. Android下载文件提示文件不存在。。。 java.io.FileNotFoundException

    遇到这个错误java.io.FileNotFoundException,事实上文件是存在的,把地址复制到手机浏览器都能够直接下载的,但为嘛不能下载呢. Error in downloadBitmap ...

  6. Android下载文件到SD卡

    HttpURLConnection 上传方式: 尝试理解这两种流的区别: InputStreamReader 的读取方式: //创建一个URL对象 URL url = new URL(urlStrin ...

  7. 主线程中一定不能放耗时操作,必须要开子线程,比如下载文件,不然会不让你拿到输入流--报错显示android.os.NetworkOnMainThreadException

    1.必须要开子线程来操作耗时操作,android.os.NetworkOnMainThreadException new Thread(new Runnable() { @Override publi ...

  8. Android之访问下载文件

    1.SD卡操作类 FileUtils.java package com.example.mars_1500_download; import java.io.File; import java.io. ...

  9. Android利用Http下载文件

    Android利用Http下载文件 一.场景 下载存文本文件和下载如mp3等大容量的文件 界面 二.代码编写 1.AndroidMainfest.xml中配置 主要是解决网络权限和写SDCard的权限 ...

随机推荐

  1. clang 搭建和编译boost 和zero ICE库 (Ubuntu10 64)

    相关介绍资料如下: Boost编译http://sourceforge.net/projects/boost/files/boost/1.55.0/boost_1_55_0.tar.gz/downlo ...

  2. Round in Oracle/VBA

    VBA的 Round采用的是银行家算法(rounds to the nearest even number) Round(1.5) = 2 Round(0.5) = 在Oracle中实现银行家算法 S ...

  3. GitHub使用指南

    文章地址:http://www.worldhello.net/gotgithub/index.html

  4. Android自定义ScrollView分段加载大文本数据到TextView

    以下内容为原创,转载时请注明链接地址:http://www.cnblogs.com/tiantianbyconan/p/3311658.html 这是我现在碰到的一个问题,如果需要在TextView中 ...

  5. Sqlite3中存储类型和数据类型结合文档解析。

    sqlite3是个很小的数据库,运行在手机,机顶盒上....那它就不可能像musql,sqlserver那么规范,有很多的数据类型,之前我也以为它定义了很多数据类型,其实不是他就5个存储类,那么多数据 ...

  6. Swift开发第十二篇——protocol组合&static和class

    本篇分为两部分: 一.Swift 中 protocol 组合的使用 二.Swfit 中 static和class 的使用 一.Swift 中 protocol 组合的使用 在 Swift 中我们可以使 ...

  7. 使用xmarks同步 chrome ie firefox safari书签

    xmarks是什么? Install Xmarks on each computer you use, and it seamlessly integrates with your web brows ...

  8. C# 得到sqlserver 数据库存储过程,触发器,视图,函数 的定义

    经常从 生产环境 到测试环境, 需要重新弄一整套的数据库环境, 除了表结构以及表结构数据,可以用动软代码生成器 生成之外, 像 存储过程,触发器,等,好像没有批量操作的,意义哥哥农比较麻烦, 所以最近 ...

  9. mysql 命令行操作入门(详细讲解版)

    之前分享过多次Mysql主题,今天继续分享mysql命令行入门   1. 那么多mysql客户端工具,为何要分享命令行操作? -快捷.简单.方便 -在没有客户端的情况下怎么办 -如果是mysql未开启 ...

  10. C+命令行+方向键=简易版扫雷

    前言: 想起来做这个是因为那时候某天知道了原来黑框框里面的光标是可以控制的,而且又经常听人说起这个,就锻炼一下好了. 之前就完成了那1.0的版本,现在想放上来分享却发现有蛮多问题的,而且最重要的是没什 ...