网络上下载东西的时候要记得在AndroidManifest.xml中申明<uses-permission android:name="android.permission.INTERNET" />
1.string类型的内容下载
2.下载其他任意类型的文件如MP3。
1 package com.download; 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.URL; 10 11 import com.utils.FileUtils; 12 13 public class HttpDownloader { 14 public String download(String urlStr) { 15 StringBuffer sb = new StringBuffer(); 16 String line = null; 17 BufferedReader buffer = null; 18 try { 19 /* 20 * 创建URL,调用openConnection()方法打开连接,用getInputStream() 21 * 获取字节流,转化成InputStreamReader的字符流,在套一个BufferedReader 22 * 可以调用BufferedReader的readLine()方法,使用起来更方便 23 */ 24 URL url = new URL(urlStr); 25 HttpURLConnection urlConn = (HttpURLConnection) url 26 .openConnection(); 27 buffer = new BufferedReader(new InputStreamReader( 28 urlConn.getInputStream())); 29 while ((line = buffer.readLine()) != null) { 30 sb.append(line); 31 } 32 } catch (Exception e) { 33 // TODO Auto-generated catch block 34 e.printStackTrace(); 35 } finally { 36 try { 37 buffer.close(); 38 } catch (IOException e) { 39 // TODO Auto-generated catch block 40 e.printStackTrace(); 41 } 42 } 43 return sb.toString(); 44 } 45 46 public int downFile(String urlStr, String path, String fileName) { 47 InputStream inputstream = null; 48 try { 49 FileUtils fileUtils = new FileUtils(); 50 if (fileUtils.isFileExist(fileName, path)) 51 return 1; 52 else { 53 URL url = new URL(urlStr); 54 HttpURLConnection urlconn = (HttpURLConnection) url 55 .openConnection(); 56 inputstream = urlconn.getInputStream(); 57 File result = fileUtils.write2SDFromInput(path, fileName, 58 inputstream); 59 if (result == null) 60 return -1; 61 } 62 63 } catch (Exception e) { 64 // TODO: handle exception 65 } finally { 66 try { 67 inputstream.close(); 68 } catch (Exception e2) { 69 // TODO: handle exception 70 } 71 } 72 return 0; 73 } 74 }
3.sd的操作
1 package mars.utils; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.io.OutputStream; 8 import java.util.ArrayList; 9 import java.util.List; 10 11 import mars.model.Mp3Info; 12 import android.os.Environment; 13 14 public class FileUtils { 15 private String SDCardRoot; 16 17 public FileUtils() { 18 // 得到当前外部存储设备的目录 19 SDCardRoot = Environment.getExternalStorageDirectory() 20 .getAbsolutePath() 21 + File.separator; 22 //SDCardRoot = Environment.getExternalStorageDirectory() + "/"; 23 } 24 25 /** 26 * 在SD卡上创建文件 27 * 28 * @throws IOException 29 */ 30 public File createFileInSDCard(String fileName, String dir) 31 throws IOException { 32 File file = new File(SDCardRoot + dir + File.separator + fileName); 33 //File file = new File(SDCardRoot + fileName); 34 System.out.println("file---->" + file); 35 file.createNewFile(); 36 return file; 37 } 38 39 /** 40 * 在SD卡上创建目录 41 * 42 * @param dirName 43 */ 44 public File creatSDDir(String dir) { 45 File dirFile = new File(SDCardRoot + dir + File.separator); 46 //File dirFile = new File(SDCardRoot + dir ); 47 System.out.println(dirFile.mkdirs()); 48 return dirFile; 49 } 50 51 /** 52 * 判断SD卡上的文件夹是否存在 53 */ 54 public boolean isFileExist(String fileName, String path) { 55 File file = new File(SDCardRoot + path + File.separator + fileName); 56 //File file = new File(SDCardRoot + fileName); 57 return file.exists(); 58 } 59 60 /** 61 * 将一个InputStream里面的数据写入到SD卡中 62 */ 63 public File write2SDFromInput(String path, String fileName, 64 InputStream input) { 65 File file = null; 66 OutputStream output = null; 67 try { 68 creatSDDir(path); 69 file = createFileInSDCard(fileName, path); 70 output = new FileOutputStream(file); 71 byte buffer[] = new byte[4 * 1024]; 72 int temp; 73 //int read( )读取一个字节,返回值为所读的字节 74 while ((temp = input.read(buffer)) != -1) { 75 //void write( byte b[ ], int off, int len ), 76 //把字节数组b中从下标off开始,长度为len的字节写入流中 77 output.write(buffer, 0, temp); 78 } 79 output.flush(); 80 } catch (Exception e) { 81 e.printStackTrace(); 82 } finally { 83 try { 84 output.close(); 85 } catch (Exception e) { 86 e.printStackTrace(); 87 } 88 } 89 return file; 90 } 91 92 /** 93 * 读取目录中的Mp3文件的名字和大小 94 */ 95 public List<Mp3Info> getMp3Files(String path) { 96 List<Mp3Info> mp3Infos = new ArrayList<Mp3Info>(); 97 File file = new File(SDCardRoot + File.separator + path); 98 File[] files = file.listFiles(); 99 FileUtils fileUtils = new FileUtils(); 100 for (int i = 0; i < files.length; i++) { 101 if (files[i].getName().endsWith("mp3")) { 102 Mp3Info mp3Info = new Mp3Info(); 103 mp3Info.setMp3Name(files[i].getName()); 104 mp3Info.setMp3Size(files[i].length() + ""); 105 String temp [] = mp3Info.getMp3Name().split("\\."); 106 String eLrcName = temp[0] + ".lrc"; 107 if(fileUtils.isFileExist(eLrcName, "/mp3")){ 108 mp3Info.setLrcName(eLrcName); 109 } 110 mp3Infos.add(mp3Info); 111 } 112 } 113 return mp3Infos; 114 } 115 116 }
2012-10-12