由于代码注释很详细,所以就不讲解了,大家都能看的懂。
分为两个类来实现:FileUtils 和 HttpDownload。
首先讲解HttpDownload这个类,代码如下:
package cn.jinbi.HttpURL;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**根据URL下载文件:前提是这个文件当中的内容是文本,函数的返回值就是这个文件中的内容
* 1.创建一个URL对象
* 2.通过URL对象,创建HttpURLConnection对象
* 3.得到InputStream
* 4.从InputStream当中读取文件
* @author think
*
*/
public class HttpDownload {
private URL url;
public String Download(String urlStr){
StringBuffer buffer = new StringBuffer();
BufferedReader br = null;
String line = null;
try {
url = new URL(urlStr);
//基于Http协议连接对象
HttpURLConnection urlCon = (HttpURLConnection)url.openConnection();
//设置请求等待响应时间
urlCon.setConnectTimeout(5000);
//设置请求方式
urlCon.setRequestMethod("GET");
//如果请求的状态码等于200表示请求成功
System.out.println("OK");
if (urlCon.getResponseCode() == 200)
{
System.out.println("OK");
br = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
while ((line=br.readLine())!=null){
System.out.println("OK");
buffer.append(line);
}
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return buffer.toString();
}
/**
* @param urlStr 链接
* @param path 文件存放的目录
* @param fileName 文件的文件名
* @return 整数-1,表明下载文件出错;0表示成功;1代表已经存在了
*/
public int downFile(String urlStr,String path,String fileName){
InputStream inputStream = null;
try{
FileUtils fileUtils = new FileUtils();
if (fileUtils.isFileExist(path + fileName)){
return 1;
}else{
inputStream = getInputStreamFromUrl(urlStr);
File resultFile = fileUtils.write2SDFromInput(path, fileName, inputStream);
if (resultFile == null){
return -1;
}
}
}catch(Exception e){
e.printStackTrace();
return -1;
}finally{
try{
inputStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
return 0;
}
/**
* 根据URL得到输入流
* @param urlStr
* @return
* @throws MalformedURLException
* @throws IOException
*/
public InputStream getInputStreamFromUrl(String urlStr)
throws MalformedURLException,IOException{
url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
InputStream inputStream = urlConn.getInputStream();
return inputStream;
}
}
然后是FileUtils这个类的代码:
package cn.jinbi.HttpURL;
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(){
//得到当前外部存储设备的目录
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;
}
/**
* 判断文件是否存在
* @param fileName
*/
public boolean isFileExist(String fileName){
File file = new File(SDPATH + fileName);
return file.exists();
}
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);
}
output.flush();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
output.close();
}catch(Exception e){
e.printStackTrace();
}
}
return file;
}
}
最后需要注意的是:别忘了加上权限:
1。访问网络的权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>