【Android学习】利用HTTP协议下载

主要内容:

---使用HTTP协议下载文件

---将下载的文件写入SDCARD

文件下载步骤:

1.创建一个HttpURLConnection对象 

HttpURLConnection urlCnn=(HttpURLConnection)url.openConnection()

2.获得一个InputStream对象

urlConn.getInputStream()

3.访问网络的权限

android.permission.INTERNET

具体实现如下:


HttpDoemload.java:

package com.down;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpDownload {
	private URL url=null;
	
	public String dowmload(String urlString){
		StringBuffer sBuffer=new StringBuffer();
		String lineString=null;
		BufferedReader bufferedReader=null;
		try {
			url=new URL(urlString); 
			HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
			bufferedReader=new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
			
			while ((lineString=bufferedReader.readLine())!=null) {
				sBuffer.append(lineString);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				bufferedReader.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return sBuffer.toString();
	}
	
	/**
	 * 该函数返回整形 -1:代表下载文件出错 0:代表下载文件成功 1:代表文件已经存在
	 */
	
	public int downFile(String urlString,String path,String fileName){
		InputStream inputStream=null;
		try {
			FileUtil fileUtil=new FileUtil();
			if(fileUtil.isFileExist(path+fileName)){
				return 1;
			}else{
				url=new URL(urlString);
				HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
				inputStream=httpURLConnection.getInputStream();
				File result=fileUtil.write2SDFromInput(path, fileName, inputStream);
				if(result==null){
					return -1;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}finally{
			try {
				inputStream.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return 0;
	}

}

FileUtil.java:

package com.down;

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 FileUtil {
	private String SDPATH;

	public String getSDPATH() {
		return SDPATH;
	}
	public FileUtil() {
		//得到当前外部存储设备的目录
		// /SDCARD
		SDPATH = Environment.getExternalStorageDirectory() + "/";
	}
	/**
	 * 在SD卡上创建文件
	 * 
	 * @throws IOException
	 */
	public File creatSDFile(String fileName) throws IOException {
		File file = new File(SDPATH + fileName);
		file.createNewFile();
		return file;
	}
	
	/**
	 * 在SD卡上创建目录
	 * 
	 * @param dirName
	 */
	public File creatSDDir(String dirName) {
		File dir = new File(SDPATH + dirName);
		dir.mkdirs();
		return dir;
	}

	/**
	 * 判断SD卡上的文件夹是否存在
	 */
	public boolean isFileExist(String fileName){
		File file = new File(SDPATH + fileName);
		return file.exists();
	}
	
	/**
	 * 将一个InputStream里面的数据写入到SD卡中
	 */
	public File write2SDFromInput(String path,String fileName,InputStream input){
		File file = null;
		OutputStream output = null;
		try{
			creatSDDir(path);
			file = creatSDFile(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;
	}

}

MainActivity.java:

package com.ye_yun_lin.httpdownload;

import java.net.HttpURLConnection;

import com.down.HttpDownload;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
	private Button downlrcButton;
	private Button downmp3Button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		downlrcButton=(Button)findViewById(R.id.button1);
		downmp3Button=(Button)findViewById(R.id.button2);
		
		downlrcButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				HttpDownload httpDownload=new HttpDownload();
				String lrc=httpDownload.dowmload("http://27.17.29.147:8080/mp3/a1.lrc");
				System.out.println(lrc);
			}
		});
		
		downmp3Button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				HttpDownload httpDownload=new HttpDownload();
				int result=httpDownload.downFile("http://27.17.29.147:8080/mp3/a1.mp3", "mp3", "a1.mp3");
				System.out.println(result);
			}
		});
	}
}

在ActivityManifest中配置:

   <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


【Android学习】利用HTTP协议下载,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。