標籤:
首先建立一個HttpDownloader類,擷取下載檔案的網路地址,將檔案下載下來以String流的方式返回:
public String download(String urlStr){
//urlStr為檔案的網路地址,如http://192.168.1.105:8080/mp3/resources.xml
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader buffer = null;
try
{
//建立一個URL對象
url = new URL(urlStr);
//建立一個Http串連
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
//使用IO流讀取資料
buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
while((line = buffer.readLine()) != null){
sb.append(line);
}
}
catch(Exception ex){
ex.printStackTrace();
}
finally{
try{
buffer.close();
}
catch(Exception ex){
ex.printStackTrace();
}
}
return sb.toString();
}
}
仍在此類下,定義下載檔案並寫入手機SD卡:
public int downFile(String urlStr,String path,String filename){
//urlStr網路,path:寫入SD卡上的地址,filename下載檔案名稱
InputStream inputStream = null;
try{
//聲明FileUtils對象
FileUtils fileUtils = new FileUtils();
if(fileUtils.isFileExist(filename,path)){
return 1; //檔案已經存在則返回1
}
else{
//將下載檔案存入inputStream流中
inputStream = getInputStreamFromUrl(urlStr);
//將下載的檔案寫入SD卡
File resultFile = fileUtils.write2SDFromInput(path, filename, inputStream);
if(resultFile == null){
return -1; //寫入失敗則返回-1
}
}
}
catch(Exception e){
e.printStackTrace();
return -1;
}
finally{
try{
inputStream.close();
}
catch(Exception e){
e.printStackTrace();
}
}
return 0; //下載寫入SD卡成功則返回0
}
public InputStream getInputStreamFromUrl(String urlStr) throws IOException{
url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConn.getInputStream();
return inputStream;
}
將下載的inputStream寫入SD卡中,定義類FileUtils:
public class FileUtils {
private String SDCardRoot;
public FileUtils() {
// 得到當前外部存放裝置的目錄
SDCardRoot = Environment.getExternalStorageDirectory()
.getAbsolutePath();
}
/**
* 在SD卡上建立檔案
*
* @throws IOException
*/
public File createFileInSDCard(String fileName, String dir)
throws IOException {
File file = new File(SDCardRoot + dir + File.separator + fileName);
file.createNewFile();
return file;
}
/**
* 在SD卡上建立目錄
*
* @param dirName
*/
public File createSDDir(String dir) {
File dirFile = new File(SDCardRoot + dir + File.separator);
dirFile.mkdirs();
return dirFile;
}
/**
* 判斷SD卡上的檔案夾是否存在
*/
public boolean isFileExist(String fileName, String path) {
File file = new File(SDCardRoot + path + File.separator + fileName);
return file.exists();
}
/**
* 將另一個InputStream裡面的資料寫入到SD中
*/
public File write2SDFromInput(String path, String fileName,
InputStream input) {
File file = null;
OutputStream output = null;
try {
createSDDir(path);
file = createFileInSDCard(fileName, path);
//檔案寫入流,就是將資料寫入file檔案
output = new FileOutputStream(file);
//定義buffer大小為4K,就是每讀取4K的內容就像檔案中寫入一次
byte buffer[] = new byte[1024 * 4];
int temp;
while ((temp = input.read(buffer)) != -1) {
output.write(buffer, 0, temp);
}
output.flush(); //清空緩衝
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
output.close(); //關閉寫入流
} catch (Exception ex) {
ex.printStackTrace();
}
}
return file;
}
}
Android從網路某個地址下載檔案、寫入SD卡