现在在做一个电信营业厅信息展示的项目,服务器端提供一个db数据库的下载地址,android客户端需要下载并读取这个数据库里的一些信息,请问是直接将数据库下载到sd卡中,然后在代码中去直接去操作这个数据库,还是需要一些其他的处理。还有在每次登陆的时候,需要更新数据库,请问如何实现,能给个思路最好了!还有就是我写的这个方法对不对,怎么没有下载成功呢?下载后缀名.db的数据库没有什么特别要求吧。求大神们能耐心的看一下。谢谢public void downFile(String url, String path, String fileName)
throws IOException {
if (fileName == null || fileName == "")
this.FileName = url.substring(url.lastIndexOf("/") + 1);
else
this.FileName = fileName; // 取得文件名,如果输入新文件名,则使用新文件名
URL Url = new URL(url);
URLConnection conn = Url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
this.fileSize = conn.getContentLength();// 根据响应获取文件大小
if (this.fileSize <= 0) { // 获取内容长度为0
throw new RuntimeException("无法获知文件大小 ");
}
if (is == null) { // 没有下载流
throw new RuntimeException("无法获取文件");
}
FileOutputStream FOS = new FileOutputStream(path + this.FileName); // 创建写入文件内存流,通过此流向目标写文件
byte buf[] = new byte[1024];
int numread;
while ((numread = is.read(buf)) != -1) {
FOS.write(buf, 0, numread);
}
try {
is.close();
} catch (Exception ex) {
}
}
throws IOException {
if (fileName == null || fileName == "")
this.FileName = url.substring(url.lastIndexOf("/") + 1);
else
this.FileName = fileName; // 取得文件名,如果输入新文件名,则使用新文件名
URL Url = new URL(url);
URLConnection conn = Url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
this.fileSize = conn.getContentLength();// 根据响应获取文件大小
if (this.fileSize <= 0) { // 获取内容长度为0
throw new RuntimeException("无法获知文件大小 ");
}
if (is == null) { // 没有下载流
throw new RuntimeException("无法获取文件");
}
FileOutputStream FOS = new FileOutputStream(path + this.FileName); // 创建写入文件内存流,通过此流向目标写文件
byte buf[] = new byte[1024];
int numread;
while ((numread = is.read(buf)) != -1) {
FOS.write(buf, 0, numread);
}
try {
is.close();
} catch (Exception ex) {
}
}
解决方案 »
- ViewFlipper一切换图片,其他view调用的layout方法就失效
- android控制铃声开关的函数
- 请大家进来看看,十分重要,音频合并,多谢!!!
- 郁闷这句话报错new AlertDialog.Builder(this);
- android中的webview的使用,在线等
- android下载图片,如何之建立一次连接下载多幅图片,对HTTP协议要有了解。PS:人品好才是真的好,结帖率100%
- android的ViewFlipper的触屏事件无法获得响应
- edittext
- gridview里图片过大,出现oom问题,咋样处理?
- HttpURLConnection.connect()超时问题
- 请教如何像QQ一样关闭Activity后网络服务仍可继续运行
- 旋转屏幕后,如何恢复多选数据
http://www.cnblogs.com/-run/archive/2011/11/30/2269390.html
希望能够帮助到你。