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 downFile(String urlStr, String path, String fileName) {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 }
FileUtils.java
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 9 import android.os.Environment;10 11 public class FileUtils {12 private String SDPATH;13 14 public String getSDPATH() {15 return SDPATH;16 }17 public FileUtils() {18 //得到当前外部存储设备的目录19 // /SDCARD20 SDPATH = Environment.getExternalStorageDirectory() + "/";21 }22 /**23 * 在SD卡上创建文件24 * 25 * @throws IOException26 */27 public File creatSDFile(String fileName) throws IOException {28 File file = new File(SDPATH + fileName);29 file.createNewFile();30 return file;31 }32 33 /**34 * 在SD卡上创建目录35 * 36 * @param dirName37 */38 public File creatSDDir(String dirName) {39 File dir = new File(SDPATH + dirName);40 dir.mkdirs();41 return dir;42 }43 44 /**45 * 判断SD卡上的文件夹是否存在46 */47 public boolean isFileExist(String fileName){48 File file = new File(SDPATH + fileName);49 return file.exists();50 }51 52 /**53 * 将一个InputStream里面的数据写入到SD卡中54 */55 public File write2SDFromInput(String path,String fileName,InputStream input){56 File file = null;57 OutputStream output = null;58 try{59 creatSDDir(path);60 file = creatSDFile(path + fileName);61 output = new FileOutputStream(file);62 byte buffer [] = new byte[4 * 1024];63 while((input.read(buffer)) != -1){64 output.write(buffer);65 }66 output.flush();67 }68 catch(Exception e){69 e.printStackTrace();70 }71 finally{72 try{73 output.close();74 }75 catch(Exception e){76 e.printStackTrace();77 }78 }79 return file;80 }81 82 }
AndroidManifest.xml
1 26 7 159 1410 1311 12 16 17 18