MicrosoftInternetExplorer402DocumentNotSpecified7.8Normal0
// 步骤
A ) : 创建一个HttpURLConection对象
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
B ) : 获取一个InputStream
urlConn.getInputStream();
C ) : 访问网络的权限
android.permission.INTERNET
例 : 1 . 声明控件对象(略)
2 . 获得控件对象(略)
3 . 绑定事件(略)
4 . 创建监听器对象
// 封装方法httpDownloader
Public class HttpDownloader {
Private URL url = null;
Public String download(String urlStr) {
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader buffer = null;
Try {
// 创建一个URL对象
Url = new URL(urlStr);
// 创建一个Http连接
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
// 使用IO流读取数据
Buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
While ((line = buffer.readline()) != null) {
Sb.append(line);
} catch (Execption e) {
e.printStackTrace();
} finally {
Try {
Buffer.close();
} catch (Execption e) {
e.printStackTrace();
}
}
Return db.ToString();
}
}
// 返回值 -1 . 下载文件出错 0 下载成功 1 文件已存在
Public int downFile(String urlStr , String path , String fileName) {
InputStream inputStream = null;
Try {
FileUtils fileUtils = new FileUtils();
If(fileUtils.isFileExists(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 (Execption e) {
e.printStackTrace();
}
}
Return 0;
}
// 根据url得到输入流
Public InputStream getInputStreamFromUrl (String urlStr)
Throws MalfromedURLExecption , IOExecption {
Url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
InputStream inputStream = urlConn.getInputStream();
Return inputStream;
}
}
Public void onClick(View v) {
HttpDownloader httpDownloader = new HttpDownloader();
String lrc = httpDownloader.download("http://192.168.0.1:8080/XXX.txt");
System.out.println(lrc);
}
2 . 将下载的文件写入SDCARD
访问SDCARD
// 得到当前设备sdka的目录
Environment.getExternalStorageDirectory();
// 访问SD卡的权限
Android.permission.WRITE_EXTERNAL_STORAGE
例 : // 一个完整的访问封装类
Public class FileUtils {
Private String SDPATH;
Public String getSDPATH() {
Return SDPATH;
}
Public FileUtils() {
SDPATH = Environment.getExternalStorageDirectory() + "/";
}
// 在SD卡上创建文件
Public File createSDFile(Stirng 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;
}
// 判断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 {
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;
}
}
最后在AndroidManifest,xml中加入标签
<user-permission android:name="android.permission. WRITE_EXTERNAL_STORAGE" />
本文转自My_King1 51CTO博客,原文链接:http://blog.51cto.com/apprentice/1360569,如需转载请自行联系原作者