从网络下载xml保存到SD卡后出现多余的内容,请教原因?
获取xml的inputstream代码:
public static InputStream getStream(String path) {
/**
* 得到数据
* @param args
* @return
*/
InputStream stream = null;
URL url;
try {
url = new URL(path);
//得到打开的链接对象
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5*1000);
conn.setConnectTimeout(5*1000);
//设置请求超时与请求方式
stream = conn.getInputStream();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stream;
}
存储到手机卡上的代码:
public static void write2SDFromInput(String path, String name,InputStream inputStream) {
File file = new File(path + name);
OutputStream output = null;
try {
File file1 = new File(path);
// 判断SD卡中目录是否存在
if ( !file1.exists()) {
System.out.println("creatdir:----------------");
file1.mkdirs();
}
if ( !file.exists()) {
System.out.println("creatfile:------------------");
file.createNewFile();
}
output = new FileOutputStream(file);
byte buffer [] = new byte[1 * 1024];
while((inputStream.read(buffer)) != -1){
output.write(buffer);
}
output.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这两端代码我都是很通用的代码,没有什么问题啊。得到的inputstream直接用dom4j解析没问题。但是我存到SD卡之后再读取我在程序里面解析出现错误,是xml的格式错误。导出来一看在后面多了一些内容。
这个xml需要备份一下,有什么办法能把这个xml保存下了?望不吝指教,多谢。
public static InputStream getStream(String path) {
/**
* 得到数据
* @param args
* @return
*/
InputStream stream = null;
URL url;
try {
url = new URL(path);
//得到打开的链接对象
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5*1000);
conn.setConnectTimeout(5*1000);
//设置请求超时与请求方式
stream = conn.getInputStream();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stream;
}
存储到手机卡上的代码:
public static void write2SDFromInput(String path, String name,InputStream inputStream) {
File file = new File(path + name);
OutputStream output = null;
try {
File file1 = new File(path);
// 判断SD卡中目录是否存在
if ( !file1.exists()) {
System.out.println("creatdir:----------------");
file1.mkdirs();
}
if ( !file.exists()) {
System.out.println("creatfile:------------------");
file.createNewFile();
}
output = new FileOutputStream(file);
byte buffer [] = new byte[1 * 1024];
while((inputStream.read(buffer)) != -1){
output.write(buffer);
}
output.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这两端代码我都是很通用的代码,没有什么问题啊。得到的inputstream直接用dom4j解析没问题。但是我存到SD卡之后再读取我在程序里面解析出现错误,是xml的格式错误。导出来一看在后面多了一些内容。
这个xml需要备份一下,有什么办法能把这个xml保存下了?望不吝指教,多谢。
...全文
相关推荐
android中将下载的mp3文件存放到虚拟sd卡中
卡涉及以下几个关键步骤:申请读写权限、下载文件、保存到外部存储以及扫描文件至媒体库。
下载图片保存到SD卡上
根据提供的文件信息,本文将详细解释如何在Android应用中使用Java语言下载图片并保存到SD卡上。这一过程包括了网络请求、图片处理以及文件存储等环节。### 一、项目背景与概述#### 1.
下载网络图片展示和保存到SD卡
在Android应用开发中,"下载网络图片展示和保存到SD卡"是一个常见的需求。这个话题主要涉及以下几个关键知识点:1.
异步下载图片保存到sd卡
在Android开发中,异步下载图片并保存到SD卡是一个常见的需求,特别是在构建涉及大量图像显示的应用时。这个过程涉及到网络请求、线程管理、内存和磁盘缓存策略等多个技术点。
从网络下载视频文件保存到本地
**文件下载**: 下载文件时,我们需要创建一个File对象,指定本地存储的位置,通常是SD卡(外部存储)。


