Android学习笔记 - 下载、存储篇

1.使用HTTP协议下载文件
 //创建URL链接
 URL url = new URL("
http://www.study.com/test.txt");
 
 //创建一个HttpURLConnection对象
 HttpURLConnection conn = (HttpURLConnection)url.openConnection();
 
 //获得一个InputStream对象
 BufferReader reader = new BufferReader(new InputStreamReader(conn.getInputStream());
 
 //读取内容(文本类型)
 String line = null;
 while((line = reader.readLine()) != null){
  System.out.println(line);
 }
 
2.将下载的文件写入SDCARD
 //得到当前设备SD卡的目录
 String SDCARD = Environment.getExternalStorageDirectory() + "/";
 
 //AndroidMainfest.xml中添加访问互联网和SD卡的权限
 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 
 //下载文件
 URL url = new URL("http://...");
 HttpURLConnection conn = (HttpURLConnection)url.openConnection();
 InputStream fileStream = conn.getInputStream();
 
 //创建目录
 File dir = new File(SDCARD + dirName);
 dir.mkdir();
 
 //创建文件
 File dir = new File(SDCARD + dirName + fileName);
 File file = dir.createNewFile();
 
 //写入文件
 OutputStream stream = new FileOutputStream(file);
 byte buff[] = new byte[4 * 1024];
 while((fileStream.read(buff)) != -1){
  stream.write(buff);
 }
 stream.flush();
 stream.close();

 

//下载辅助类完整示例

public class DownloadHelper {

	// 获取指定URL返回的文档结果
	public static String downloadText(String navUrl) {
		BufferedReader reader = null;
		StringBuffer sb = null;
		String line = null;
		try {
			sb = new StringBuffer();
			reader = new BufferedReader(new InputStreamReader(getInputStream(navUrl)));
			while ((line = reader.readLine()) != null) {
				sb.append(line);
			}
			reader.close();
			return sb.toString();
		} catch (Exception err) {
			Log.e("Exception", err.getMessage());
			return null;
		}
	}

	// 从指定URL下载文件
	public static int downloadFile(String navUrl) {
		try {

			InputStream stream = getInputStream(navUrl);
			StorageHelper storage = new StorageHelper();
			return storage.writeFile("study/", navUrl.substring(navUrl.lastIndexOf("/")), stream);
		} catch (Exception err) {
			Log.e("Exception", err.getMessage());
			return -1;
		}
	}

	// 获取指定URL的输入流
	private static InputStream getInputStream(String navUrl) {
		try {
			URL url = new URL(navUrl);
			HttpURLConnection http = (HttpURLConnection) url.openConnection();
			return http.getInputStream();
		} catch (Exception err) {
			Log.e("Exception", err.getMessage());
			return null;
		}
	}
}


//存储辅助类完整示例

public class StorageHelper {
	private String SDCARD = null;

	// 构造函数,获取SDCARD路径
	public StorageHelper() {
		SDCARD = Environment.getExternalStorageDirectory() + "/";
	}

	// 属性:SDCARD路径
	public String getSDCardPath() {
		return SDCARD;
	}

	// 在SDCARD里创建目录
	public boolean createDirectory(String path) {
		File dir = new File(SDCARD + path);
		return dir.mkdir();
	}

	// 在SDCARD里指定目录创建新文件
	public File createFile(String path, String fileName) {
		try {
			File file = new File(SDCARD + path + fileName);
			if (file.createNewFile()) {
				return file;
			} else {
				return null;
			}
		} catch (Exception err) {
			Log.e("Exception", err.getMessage());
			return null;
		}
	}

	// 判断SDCARD里指定目录中是否有指定名称的文件
	public boolean isExistsFile(String path, String fileName) {
		File file = new File(SDCARD + path + fileName);
		return file.exists();
	}

	// 往SDCARD里指定目录、指定名称写入流
	public int writeFile(String path, String fileName, InputStream fileStream) {
		OutputStream writer = null;
		try {
			createDirectory(path); // 不管3721,先创建目录

			if (!isExistsFile(path, fileName)) { // 判断文件是否存在
				File file = createFile(path, fileName);
				writer = new FileOutputStream(file);
				byte[] data = new byte[4 * 1024];
				while (fileStream.read(data) != -1) {
					writer.write(data);
				}
				writer.flush();
				writer.close();
				return 1;
			} else {
				return 0;
			}
		} catch (Exception err) {
			Log.e("Exception", err.getMessage());
			return -1;
		}
	}
}


 

你可能感兴趣的:(android,exception,String,File,存储,Path)