@activity_main.xml代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity" >
<Button
android:id="@+id/button_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载文本文件" />
<Button
android:id="@+id/button_mp3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载MP3文件" />
</LinearLayout>
@Androidmanifest.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidstudy"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<!-- 访问网络权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- 操作SD卡权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.androidstudy.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
@下载帮助类HttpDownloadHelper代码:
package com.example.androidstudy;
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;
public class HttpDownloadHelper {
private URL url = null;
/**
* 根据出入的参数URL保存输出流
*/
public String downLoad(String urlString){
//实例化一个StringBuffer类,用来存储字符串
StringBuffer stringBuffer = new StringBuffer();
String line = null;
//实例化一个BufferedReader类,用来读取输入流
BufferedReader bufferedReader = null;
try {
//实例化一个URL解析
url = new URL(urlString);
//实例化一个HttpURLConnection,并打开连接
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//将打开连接中的输入流,缓存到bufferdReader中
bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
//循环读取数据,保存到line中,直到全部读取
while ((line = bufferedReader.readLine()) != null) {
//将line中的内容添加到stringBuffer中
stringBuffer.append(line);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
//关闭缓冲字节输入流
bufferedReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return stringBuffer.toString();
}
/**
* -1:下载文件出错
* 0:文件下载成功
* 1:文件已经存在
*/
public int downFile(String urlString, String path, String fileName){
//定义一个InputStream,用来读取输入流
InputStream inputStream = null;
//自定义的文件操作类
FileUtils fileUtils = new FileUtils();
if(fileUtils.isFileExist(path + fileName)){
return 1;
}else{
//从资源中读取数据
inputStream = getInputStream(urlString);
//创建文件
File resultFile = fileUtils.saveFile(path, fileName, inputStream);
if(resultFile == null){
return -1;
}
}
try {
//关闭输入流
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
/**
* 根据URL得到输入流
*/
public InputStream getInputStream(String urlString){
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
url = new URL(urlString);
urlConnection = (HttpURLConnection) url.openConnection();
inputStream = urlConnection.getInputStream();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return inputStream;
}
}
@文件操作类FileUtils 代码:
package com.example.androidstudy;
import java.io.File;
import java.io.FileNotFoundException;
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;
//定义缓存区大小
private int FILESIZE = 4 * 1024;
public String getSDPAHT(){
return SDPATH;
}
/**
* 获取SD卡路径
*/
public FileUtils(){
SDPATH = Environment.getExternalStorageState()+"/";
}
/**
* 在SD卡上创建文件
*/
public File createFile(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 saveFile(String path, String fileName, InputStream inputStream){
File file = null;
//定义一个输出流,用来写数据
OutputStream outputStream = null;
//创建文件夹
createFile(path);
//保存文件
file = createFile(path + fileName);
try {
//构造一个新的文件输出流写入文件
outputStream = new FileOutputStream(file);
//创建一个缓存区
byte[] buffer = new byte[FILESIZE];
//从输入流中读取的数据存入缓存区
while ((inputStream.read(buffer)) != -1) {
//将缓存区的数据写入输出流
outputStream.write(buffer);
}
//刷新流,清空缓冲区数据
outputStream.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
//关闭流
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return file;
}
}
@主程序类MainActivity代码:
package com.example.androidstudy;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//实例化组件
Button button_text = (Button) findViewById(R.id.button_text);
Button button_mp3 = (Button) findViewById(R.id.button_mp3);
//添加监听
button_text.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
HttpDownloadHelper downloadHelper = new HttpDownloadHelper();
String lrc = downloadHelper.downLoad("http://192.168.1.249:8080/test/1.lrc");
System.out.println(lrc);
}
});
button_mp3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
HttpDownloadHelper downloadHelper = new HttpDownloadHelper();
int result = downloadHelper.downFile("http://192.168.1.249:8080/test/1.mp3", "test/", "1.mp3");
System.out.println(result);
}
});
}
}