这是我自学时看到的一个下载的方法,写出来大家看看,不保证完全完美,有好的建议可以留言,非常感谢
分隔符上建议使用java File类提供的 File.separator 常量
步骤:1.创建一个HttpURLConnection 对象,通过一个URL对象调用openConnection()方法 (当然,是通过HTTP协议来执行的,不解释)
HttoURLConnecton urlConn = (HttpURLConnection)url.openConnecton();
2.获得一InputStream对象
urlConn.getInputStream();
3.访问网络的权限
<uses-permission android:name="android.permission.INTERNET"/>
4.得到sd卡的目录(得到外部存储设备目录)
Environment.getExternalStorageDirectory()
一般的手机sd卡目录/SDCARD,为了保险,还是调用Android给我们的方法
在使用sdcard前最好判断是否有sdcard
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTEDS)
5.访问sd卡的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
java 代码
一个按钮监听器
file.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
HttpDownloader httpDownloader = new HttpDownloader();
String result = httpDownloader.downFile("下载的链接", " 自定义一个目录", "自定义文件名");
Toast toast = Toast.makeText(Download.this, result, Toast.LENGTH_LONG);
toast.show();
}});
HttpDownloader是我自己定义的一个类,在这个类里面
private URL url = null;
public String downFile(String urlstr,String path,String fileName){
InputStream inputStream = null;
//String s = "";
try {
//FileUtils为自定义的一个工具类,详细代码看下面
FileUtils fileUtils = new FileUtils();
//判断文件是否存在
if(fileUtils.isFileExist(path+fileName)){
return "file is EXIST";
}else{
inputStream = getInputStreamFromUrl(urlstr);
File resultFile = fileUtils.writeToSDFromInput(path, fileName, inputStream);
if(resultFile == null){
return "down is breaked";
}
}
} catch (Exception e) {
e.printStackTrace();
return "down is not run";
}finally{
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return "OK";
}
/*
* 根据URL得到输入流
*/
public InputStream getInputStreamFromUrl(String urlstr) throws IOException{
url = new URL(urlstr);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConn.getInputStream();
return inputStream;
}
当然 FileUtils 也是我自己定义的一个工具类
public class FileUtils {
private String SDPATH;
public String getSDPATH(){
return SDPATH;
}
public FileUtils(){
//得到外部存储设备目录 ,当然“/”最好用File.separator 替换
SDPATH = Environment.getExternalStorageDirectory() + "/";
}
/*
* *在SD卡上创建文件
*/
public File createSDFile(String fileName) throws IOException{
File file = new File(SDPATH+fileName);
file.createNewFile();
return file;
}
/*
* *在SD 卡上创建目录
*/
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();
}
/*
* *将一个InputStream里的数据写入到SD卡中
*/
public File writeToSDFromInput(String path,String fileName,InputStream input){
File file = null;
OutputStream output = null;
try {
//创建目录
createSDDir(path);
//创建文件 /SDCARD/dd/aa.mp3
file = createSDFile(path+fileName);
//写入数据
output = new FileOutputStream(file);
// 每4k写一次
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 (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return file;
}
}