12文件下载
来源:互联网 发布:财经网数据 编辑:程序博客网 时间:2024/06/05 08:57
1、DDMS
2、文件下载
a、创建一个HttpURLConnection对象
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
b、获得一个InputStream对象
urlConn.getInputStream()
c、访问网络的权限
android.permission.INTERNET
3、访问SD-CARD
a、得到当前设备SD卡的目录
Environment.getExternalStorageDirectory()
b、访问SD卡的权限
android.permission.WRITE_EXTERNAL_STORAGE
//////////////////////////////////////////////
HttpDownloader.java
public class HttpDownloader {
private URL url = null;
public String download(String urlStr){
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader buffer = null;
try{
url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
while ((line =buffer.readLine())!=null){
sb.append(line);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
buffer.close();
}catch(Exception e){
e.printStackTrace();
}
}
return sb.toString();
}
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;
}
public InputStream getInputStreamFromUrl(String urlStr)throws IOException{
url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
InputStream inputStream = urlConn.getInputStream();
return inputStream;
}
}
//////////////////////////////////////////////
FileUtils.java
package hy.com;
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.getExternalStorageState()+"/";
}
public File createSDFile(String fileName)throws IOException{
File file = new File(SDPATH+fileName);
file.createNewFile();
return file;
}
public File createSDDir(String dirName){
File dir = new File(SDPATH+dirName);
dir.mkdir();
return dir;
}
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;
}
}
///////////////////////////////////////
//下载text
HttpDownloader httpDownloader = new HttpDownloader();
String lrc = httpDownloader.download("http://127.0.0.1:80/test.txt");
//下载MP3
HttpDownloader httpDownloader = new HttpDownloader();
int result = httpDownloader.downFile("http://127.0.0.1:80/test.mp3", "music/", "test.mp3");