java http 下载文件,服务器上的文件时一行一行的,下载下来却没有换行
源码:try{HttpURLConnectionconn;InputStreamin;conn=(HttpURLConnection)newURL(url1).openC...
源码:
try {
HttpURLConnection conn;
InputStream in;
conn = (HttpURLConnection) new URL(url1)
.openConnection();
String pathName="/sdcard/app/download/";
File path = new File(pathName);
File file = new File(pathName + fileName);
if( !path.exists()) {
// Log.d(CDG, "Create the path:" + pathName);
path.mkdir();
}
if( !file.exists()) {
// Log.d(CDG, "Create the file:" + fileName);
file.createNewFile();
}
in = conn.getInputStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(in,"utf-8"));
StringBuffer sb = new StringBuffer();
String data = "";
while((data = br.readLine())!=null){
sb.append(data);
}
System.out.println("textContent:"+sb.toString());
FileOutputStream out = new FileOutputStream(file);
byte[] buf = sb.toString().getBytes();
out.write(buf);
out.close();
in.close();
out.close();
conn.disconnect(); 展开
try {
HttpURLConnection conn;
InputStream in;
conn = (HttpURLConnection) new URL(url1)
.openConnection();
String pathName="/sdcard/app/download/";
File path = new File(pathName);
File file = new File(pathName + fileName);
if( !path.exists()) {
// Log.d(CDG, "Create the path:" + pathName);
path.mkdir();
}
if( !file.exists()) {
// Log.d(CDG, "Create the file:" + fileName);
file.createNewFile();
}
in = conn.getInputStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(in,"utf-8"));
StringBuffer sb = new StringBuffer();
String data = "";
while((data = br.readLine())!=null){
sb.append(data);
}
System.out.println("textContent:"+sb.toString());
FileOutputStream out = new FileOutputStream(file);
byte[] buf = sb.toString().getBytes();
out.write(buf);
out.close();
in.close();
out.close();
conn.disconnect(); 展开
1个回答
2015-06-02
展开全部
while((data = br.readLine())!=null){
sb.append(data);
sb.append("\r\n");
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询