使用HTTP协议 下载文件
来源:互联网 发布:淘宝导航条隐藏恢复 编辑:程序博客网 时间:2024/05/09 16:51
文件下载步骤:
1. 创建一个HttpURLConnection对象 HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
2. 获得一个InputStream对象 urlConn.getInputStream();
下载的文件当中的内容是文本,函数的返回值就是文件当中的内容:
答:步骤1.创建一个URL对象, 2.通过URL对象,创建一个HttpURLConnection对象
3.得到InputStream, 4.从InputStream当中读取数据
将下载的文件写入SDCARD:
得到当前设备SD卡的目录:Environment.getExternalStorageDirectory();
访问SD卡的权限,见注意。
HttpDownloader 类
package com.examples.utils;
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;
import android.app.backup.FileBackupHelper;
public class HttpDownloader {
URL url = null;
//下载文档文件
public String download(String urlstr)
{
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader buffer = null;
try {
url = new URL(urlstr);
HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
buffer= new BufferedReader(new InputStreamReader(httpConn.getInputStream()));//IO包的封装
while((line=buffer.readLine())!=null)
{
sb.append(line);
}
} catch (Exception e) {
e.printStackTrace();
}
finally
{
try {
buffer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return sb.toString();
}
//返回-1:表示下载文件出错;返回0:表示下载成功;返回1:表示文件已经存在
public int downfile(String urlstr,String path,String filename)
{
InputStream input = null;
try {
FileUtils fileUtils = new FileUtils();
if(fileUtils.isFileExist(path + filename))
{
return 1;
}
else
{
input = getInputStreamFromUrl(urlstr);
File resultFile = fileUtils.write2SDFromInput(path, filename, input);
if(resultFile == null)
{
return -1;
}
}
} catch (IOException e) {
e.printStackTrace();
return -1;
}
return 0;
}
public InputStream getInputStreamFromUrl(String urlstr) throws IOException
{
url = new URL(urlstr);
HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
InputStream input = httpConn.getInputStream();
return input;
}
}
FileUtils 类
package com.examples.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import android.os.Environment;
public class FileUtils {
private String SDPATH;
//构造函数
public FileUtils()
{
//得到当前外部存储设备的目录
SDPATH = Environment.getExternalStorageDirectory()+ "/"; //getPath方法看可以不要不
}
//返回SD卡目录
public String getSDPATH()
{
return SDPATH;
}
//在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;
}
//判断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)
{
OutputStream output = null;
File file =null;
try
{
createSDDir(path);
file = createSDFile(path + filename);
output = new FileOutputStream(file);//构造FileOutputStream用于给文件中写入内容
byte buffer[] = new byte[4*1024];
while(input.read(buffer) !=-1)
{
output.write(buffer);
}
output.flush();
}catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
output.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
return file;
}
}
download 类
package com.examples.download;
import com.examples.utils.HttpDownloader;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class download extends Activity {
/** Called when the activity is first created. */
private Button downTxt;
private Button downFile;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
downTxt = (Button)findViewById(R.id.downtxt);
downFile = (Button)findViewById(R.id.downfile);
downTxt.setOnClickListener(new downTxtListener());
downFile.setOnClickListener(new downFileListener());
}
class downTxtListener implements OnClickListener
{
@Override
public void onClick(View v) {
HttpDownloader httpdownloader = new HttpDownloader();
String srt=httpdownloader.download("http://202.193.82.14:8080/vao1500/1.srt");
System.out.println(srt);
}
}
class downFileListener implements OnClickListener
{
@Override
public void onClick(View v) {
HttpDownloader httpdownloader = new HttpDownloader();
int result=httpdownloader.downfile("http://202.193.80.14:8080/vao1500/a1.mp3", "voa/", "a1.mp3");
System.out.println(result);
}
}
}
注意:访问网络的权限(在AndroidManifest.xml中设置) <uses-permission android:name="android.permission.INTERNET" />
具有写的权利 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
InputStream是读取数据,OutputStream是写入数据。
- 使用HTTP协议 下载文件
- 使用HTTP协议下载文件
- Android 使用http协议下载文件
- Android文件下载使用Http协议
- VC使用HTTP协议下载文件
- HTTP协议文件下载
- C++ HTTP协议 下载文件
- HTTP协议实现文件下载
- Android——使用HTTP协议下载文件
- 使用Http下载文件
- 使用http下载文件
- [VB.NET]通过http协议下载文件
- http协议 文件下载原理详解
- 文件下载原理详解 http协议
- http协议 文件下载原理详解
- http协议 文件下载原理详解
- http协议 文件下载原理详解
- java多线程下载http协议文件
- 好玩的面试题
- 一般文件操作函数的使用示例
- inet_addr, inet_ntoa, in_addr 的用法示例
- vc++ mfc 非空指针删除出错
- Effective java 中关于性能和架构的一段话
- 使用HTTP协议 下载文件
- C#生成MD5哈希码
- 内核空间和用户空间,内核态和用户态
- 不用多态可以重新父类的方法和变量
- 虽然总是能够没心没肺的笑着,但是有时候突然也会很伤心!
- 7月13一个不平凡的日子
- DBA之路:小小DBA一年工作总结
- ListView排序
- C#生成随机数