(转自Mars) Android - 文件下载类 HttpDownloader,内存卡操作类 FileUtils
来源:互联网 发布:mac开机白屏问号修复 编辑:程序博客网 时间:2024/05/19 17:23
download.java
package zlj.download;
import zlj.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 downloadText = null;
private Button downloadMp3 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
downloadText = (Button)findViewById(R.id.downloadText);
downloadText.setOnClickListener(new downloadTextClicklistener());
downloadMp3 = (Button)findViewById(R.id.downloadMp3);
downloadMp3.setOnClickListener(new downloadMp3Clicklistener());
}
class downloadTextClicklistener implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
HttpDownloader httpDownloader = new HttpDownloader();
//http://www.lrc123.com/download/lrc/86547-30.aspx ,流星
System.out.println("开始下载文本");
String lrc = httpDownloader.download("http://115.156.228.135:8080/Mp3/liuxing.lrc");//localhost手机无法识别
// String lrc1 = httpDownloader.download("http://www.lrc123.com/download/lrc/86547-30.aspx");
System.out.println("lrc->" + lrc);
// System.out.println("lrc1->" + lrc1);
}
}
class downloadMp3Clicklistener implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
HttpDownloader httpDownloader = new HttpDownloader();
System.out.println("开始下载Mp3");
int result = httpDownloader.downloadFile("http://115.156.228.135:8080/Mp3/liuxing.mp3","Mp3/","liuxing.mp3");
System.out.println(result);
}
}
}
HttpDownloader.java
1 package mars.utils; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.io.InputStreamReader; 8 import java.net.HttpURLConnection; 9 import java.net.MalformedURLException;10 import java.net.URL;11 12 13 public class HttpDownloader {14 private URL url = null;15 16 /**17 * 根据URL下载文件,前提是这个文件当中的内容是文本,函数的返回值就是文件当中的内容18 * 1.创建一个URL对象19 * 2.通过URL对象,创建一个HttpURLConnection对象20 * 3.得到InputStram21 * 4.从InputStream当中读取数据22 * @param urlStr23 * @return24 */25 public String download(String urlStr) {26 StringBuffer sb = new StringBuffer();27 String line = null;28 BufferedReader buffer = null;29 try {30 // 创建一个URL对象31 url = new URL(urlStr);32 // 创建一个Http连接33 HttpURLConnection urlConn = (HttpURLConnection) url34 .openConnection();35 // 使用IO流读取数据36 buffer = new BufferedReader(new InputStreamReader(urlConn37 .getInputStream()));38 while ((line = buffer.readLine()) != null) {39 sb.append(line);40 }41 } catch (Exception e) {42 e.printStackTrace();43 } finally {44 try {45 buffer.close();46 } catch (Exception e) {47 e.printStackTrace();48 }49 }50 return sb.toString();51 }52 53 /**54 * 该函数返回整形 -1:代表下载文件出错 0:代表下载文件成功 1:代表文件已经存在55 */56 public int downloadFile(String urlStr, String path, String fileName) {//Mark,下载文件57 InputStream inputStream = null;58 try {59 FileUtils fileUtils = new FileUtils();60 61 if (fileUtils.isFileExist(path + fileName)) {62 return 1;63 } else {64 inputStream = getInputStreamFromUrl(urlStr);65 File resultFile = fileUtils.write2SDFromInput(path,fileName, inputStream);66 if (resultFile == null) {67 return -1;68 }69 }70 } catch (Exception e) {71 e.printStackTrace();72 return -1;73 } finally {74 try {75 inputStream.close();76 } catch (Exception e) {77 e.printStackTrace();78 }79 }80 return 0;81 }82 83 /**84 * 根据URL得到输入流85 * 86 * @param urlStr87 * @return88 * @throws MalformedURLException89 * @throws IOException90 */91 public InputStream getInputStreamFromUrl(String urlStr)92 throws MalformedURLException, IOException {93 url = new URL(urlStr);94 HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();95 InputStream inputStream = urlConn.getInputStream();96 return inputStream;97 }98 }
- (转自Mars) Android - 文件下载类 HttpDownloader,内存卡操作类 FileUtils
- 文件下载类 HttpDownloader,内存卡操作类 FileUtils
- Android 文件操作帮助类FileUtils文件
- 文件操作工具类FileUtils
- 文件操作工具类FileUtils
- 文件操作工具类FileUtils
- 文件操作工具类FileUtils
- 文件操作工具类 FileUtils常用方法
- 封装的HttpDownloader类
- 文件工具类FileUtils
- FileUtils文件工具类
- [cocos2dx 3.0 (一)] 对文件读写操作 +FileUtils类
- cocos2dx 3.0 对文件读写操作 +FileUtils类
- [cocos2dx 3.0] 对文件读写操作 +FileUtils类
- org.apache.commons-io文件工具类FileUtils常用操作
- FileUtils 基本文件操作
- FileUtils 对文件操作
- FileUtils文件操作
- HTML5 postMessage
- Go学习第一课
- PHPCMS config.php 学习笔记
- 用dynamips模拟交换机时查看vlan信息的命令
- 加快您的网站的最佳实践(Best Practices for Speeding Up Your Web Site)
- (转自Mars) Android - 文件下载类 HttpDownloader,内存卡操作类 FileUtils
- 操作系统、数据库和网络03(mySQL变量类型)
- 欢迎使用CSDN-markdown编辑器
- 寻找克隆人
- Postfix邮件服务器搭建(六)
- C表达式求值做加法和乘法
- 二维码相关---java生成二维码名片,并且自动保存到手机通讯录中
- IOS 四种保存数据的方式
- hadoop mapred(hive)执行目录 文件权限问题