Android網路檔案下載模組整理,android檔案下載
一、知識基礎
tomcat伺服器配置
理解http協議
理解javaIO操作相關知識
SDcard操作知識
Android 許可權配置
二、實現步驟
1、從網上擷取資源
1 public String download(String urlStr) { 2 StringBuffer sb = new StringBuffer(); 3 String line = null; 4 BufferedReader buffer = null; 5 try { 6 // 建立一個URL對象 7 url = new URL(urlStr); 8 // 建立一個Http串連 9 HttpURLConnection urlConn = (HttpURLConnection) url10 .openConnection();11 // 使用IO流讀取資料12 buffer = new BufferedReader(new InputStreamReader(urlConn13 .getInputStream()));14 while ((line = buffer.readLine()) != null) {15 sb.append(line);16 }17 } catch (Exception e) {18 e.printStackTrace();19 } finally {20 try {21 buffer.close();22 } catch (Exception e) {23 e.printStackTrace();24 }25 }26 return sb.toString();27 }28 29 /**30 * 該函數返回整形 -1:代表下載檔案出錯 0:代表下載檔案成功 1:代表檔案已經存在31 */32 public int downFile(String urlStr, String path, String fileName) {33 InputStream inputStream = null;34 try {35 FileUtils fileUtils = new FileUtils();36 37 if (fileUtils.isFileExist(path + fileName)) {38 return 1;39 } else {40 inputStream = getInputStreamFromUrl(urlStr);41 File resultFile = fileUtils.write2SDFromInput(path,fileName, inputStream);42 if (resultFile == null) {43 return -1;44 }45 }46 } catch (Exception e) {47 e.printStackTrace();48 return -1;49 } finally {50 try {51 inputStream.close();52 } catch (Exception e) {53 e.printStackTrace();54 }55 }56 return 0;57 }58 59 /**60 * 根據URL得到輸入資料流61 * 62 * @param urlStr63 * @return64 * @throws MalformedURLException65 * @throws IOException66 */67 public InputStream getInputStreamFromUrl(String urlStr)68 throws MalformedURLException, IOException {69 url = new URL(urlStr);70 HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();71 InputStream inputStream = urlConn.getInputStream();72 return inputStream;73 }74 }從網路下載檔案
2、將資源寫入SDcard
public class FileUtils { private String SDPATH; public String getSDPATH() { return SDPATH; } public FileUtils() { //得到當前外部存放裝置的目錄 // /SDCARD SDPATH = Environment.getExternalStorageDirectory() + "/"; } /** * 在SD卡上建立檔案 * * @throws IOException */ public File creatSDFile(String fileName) throws IOException { File file = new File(SDPATH + fileName); file.createNewFile(); return file; } /** * 在SD卡上建立目錄 * * @param dirName */ public File creatSDDir(String dirName) { File dir = new File(SDPATH + dirName); dir.mkdirs(); 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{ creatSDDir(path); file = creatSDFile(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; }}SDCard 讀寫操作
三、版本說明
程式源碼來源Mars老師,表示感謝