1.创建HttpURLConnection对象
2.获得一个InputSream对象
3.访问网络权限
<uses-permission android:name="android.permission.INTERNET"/>
代码:
设置两个按钮,一个下载文本文件,另一个下载MP3。
public class MainActivity extends Activity {
private Button textButton;
private Button mp3Button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textButton = (Button)findViewById(R.id.my_tvbt);
mp3Button = (Button)findViewById(R.id.my_mp3bt);
textButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
HttpDownLoder loder = new HttpDownLoder();
String result = loder.downLoad("");
System.out.println(result);
}
});
mp3Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
HttpDownLoder loader = new HttpDownLoder();
FileUtils utils = new FileUtils();
int result = loader.dowLoadFile("weimiao","可念不可说.mp3","http://music.163.com/#/m/song?id=30871158&userid=286327298");
System.out.println(result);
}
});
}
}
public class FileUtils {
public String SDPATH = null;
public FileUtils() {
SDPATH = Environment.getExternalStorageState() + "/";
}
public String getSDPATH() {
return SDPATH;
}
//创建File文件
public File createFile(String fileName) throws Exception{
File file = new File(getSDPATH()+fileName);
file.createNewFile();
return file;
}
//在sd卡上创建目录
public File createSDDir(String dirName) throws Exception{
File file = new File(getSDPATH()+ dirName);
file.mkdir();
return file;
}
//判断文件在sd卡上是否存在
public boolean isExit(String fileName){
File file = new File(getSDPATH()+ fileName);
return file.exists();
}
//将inputStream写到sd卡中
public File write2SdInput(String path, String fileName, InputStream inputStream){
OutputStream os = null;
File file = null;
try{
createSDDir(path);
file = createFile(path + fileName);
os = new FileOutputStream(file);
byte[] bytes = new byte[4 * 1024];
while((inputStream.read(bytes) )!= -1){
os.write(bytes);
}
os.flush();
}catch (Exception e){
e.printStackTrace();
}finally {
try {
os.close();
}catch (Exception e){
e.printStackTrace();
}
}
return file;
}
}
public class HttpDownLoder{
private URL url = null;
//下载文本文件
public String downLoad(String urlStr){
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader br = null;
//创建url对象
try {
url = new URL(urlStr);
//创建HTTP链接
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
//获取输入流
br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while((line = br.readLine())!= null){
sb.append(line);
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
br.close();
}catch (Exception e){
e.printStackTrace();
}
}
return sb.toString();
}
//下载mp3
public int dowLoadFile(String path,String fileName,String urlStr){
InputStream inputStream = null;
try{
FileUtils utils = new FileUtils();
if(utils.isExit(path + fileName)){
return 1;
}else{
inputStream = getInputStreamUri(urlStr);
File resultFile = utils.write2SdInput(path,fileName,inputStream);
if(resultFile == null){
return -1;
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return 0;
}
//得到inputStream
public InputStream getInputStreamUri(String urlStr) throws IOException {
url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
InputStream inputStream = connection.getInputStream();
return inputStream;
}
}