简单的文件下载后读取显示,该方法可返回内容的结果集。一般适用于文本文档的下载,以供学习交流。
/**
* 远程下载文件并读取返回p
* @param filePath 文件网络地址 如http://www.baidu.com/1.txt
* @return String
*/
public String DownAndReadFile(String filePath){
String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
File savePath = new File("D://"+date);//创建新文件
if (!savePath.exists()) {
savePath.mkdir();
}
String[] urlname = filePath.split("/");
int len = urlname.length-1;
String uname = urlname[len];//获取文件名
try {
File file = new File(savePath+"//"+uname);//创建新文件
if(file!=null && !file.exists()){
file.createNewFile();
}
OutputStream oputstream = new FileOutputStream(file);
URL url = new URL(filePath);
HttpURLConnection uc = (HttpURLConnection) url.openConnection();