Android網路檔案下載模組整理,android檔案下載

來源:互聯網
上載者:User

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老師,表示感謝

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.