文章分类:移动开发
1.使用HTTP协议下载文件
2.将下载的文件写入SDCARD
- package net.wl.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;
- /**
- * 文件下载工具类
- *
- * @author wanglin
- *
- */
- public class FileUtils {
- private String SDPATH;
- public String getSDPATH() {
- return SDPATH;
- }
- public FileUtils() {
- // 得到当前外包存储设备的目录
- SDPATH = Environment.getExternalStorageDirectory() + "/";
- }
- /**
- * 在SD卡上创建文件
- *
- * @param fileName
- * @return
- * @throws IOException
- */
- public File createSDFile(String fileName) throws IOException {
- File file = new File(SDPATH + fileName);
- file.createNewFile();
- return file;
- }
- /**
- * 在SD卡上创建目录
- *
- * @param dirName
- * @return
- */
- public File createSDDir(String dirName) {
- File dir = new File(SDPATH + dirName);
- dir.mkdir();
- return dir;
- }
- /**
- * 判断文件是否存在
- *
- * @param fileName
- * @return
- */
- public boolean isFileExist(String fileName) {
- File file = new File(SDPATH + fileName);
- return file.exists();
- }
- /**
- * 将一个InputStream的数据写入到SD卡中
- *
- * @param path
- * @param fileName
- * @param input
- * @return
- */
- public File writeDataToSDCrard(String path, String fileName,
- InputStream input) {
- File file = null;
- OutputStream output = null;
- try {
- // 创建目录
- this.createSDDir(path);
- // 创建文件
- file = this.createSDFile(path + fileName);
- // 创建文件输出流
- output = new FileOutputStream(file);
- // 4个字节的输出
- byte[] b = new byte[4 * 1024];
- while ((input.read(b)) != -1) {
- output.write(b);
- }
- // 刷新流
- output.flush();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- output.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return file;
- }
- }
package net.wl.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;
/**
* 文件下载工具类
*
* @author wanglin
*
*/
public class FileUtils {
private String SDPATH;
public String getSDPATH() {
return SDPATH;
}
public FileUtils() {
// 得到当前外包存储设备的目录
SDPATH = Environment.getExternalStorageDirectory() + "/";
}
/**
* 在SD卡上创建文件
*
* @param fileName
* @return
* @throws IOException
*/
public File createSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
file.createNewFile();
return file;
}
/**
* 在SD卡上创建目录
*
* @param dirName
* @return
*/
public File createSDDir(String dirName) {
File dir = new File(SDPATH + dirName);
dir.mkdir();
return dir;
}
/**
* 判断文件是否存在
*
* @param fileName
* @return
*/
public boolean isFileExist(String fileName) {
File file = new File(SDPATH + fileName);
return file.exists();
}
/**
* 将一个InputStream的数据写入到SD卡中
*
* @param path
* @param fileName
* @param input
* @return
*/
public File writeDataToSDCrard(String path, String fileName,
InputStream input) {
File file = null;
OutputStream output = null;
try {
// 创建目录
this.createSDDir(path);
// 创建文件
file = this.createSDFile(path + fileName);
// 创建文件输出流
output = new FileOutputStream(file);
// 4个字节的输出
byte[] b = new byte[4 * 1024];
while ((input.read(b)) != -1) {
output.write(b);
}
// 刷新流
output.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
}
- package net.wl.utils;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import android.content.Context;
- import android.widget.Toast;
- /**
- * HTTP下载类
- * @author wanglin
- *
- */
- public class HttpDownloader {
- private URL url = null;
- /**
- * 该函数返回整型: -1:代表出错 , 0:代表成功 , 1:代表已经存在
- *
- * @description 下载的通用方法
- * @param urlStr
- * @param path
- * @param fileName
- * @return
- */
- public int downFile(Context context, String urlStr, String path,
- String fileName) {
- InputStream inputStream = null;
- FileUtils fileUtils = new FileUtils();
- if (fileUtils.isFileExist(path + fileName)) {
- Toast.makeText(context, "文件已经存在", Toast.LENGTH_LONG).show();
- return 1;
- } else {
- try {
- inputStream = getInputSreamFromUrl(urlStr);
- File resultFile = fileUtils.writeDataToSDCrard(path, fileName,
- inputStream);
- if (resultFile == null) {
- Toast.makeText(context, "下载失败", Toast.LENGTH_LONG).show();
- return -1;
- }
- } catch (IOException e) {
- e.printStackTrace();
- return -1;
- } finally {
- try {
- inputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- return -1;
- }
- }
- }
- Toast.makeText(context, "下载成功", Toast.LENGTH_LONG).show();
- return 0;
- }
- /**
- * 得到输入流
- *
- * @param urlstr
- * @return
- * @throws IOException
- */
- public InputStream getInputSreamFromUrl(String urlstr) throws IOException {
- url = new URL(urlstr);
- HttpURLConnection urlconn = (HttpURLConnection) url.openConnection();
- InputStream is = urlconn.getInputStream();
- return is;
- }
- }