[Java]远程下载文件并读取实例方法
2013-12-04 14:20
656 查看
简单的文件下载后读取显示,该方法可返回内容的结果集。一般适用于文本文档的下载,以供学习交流。
[java] view
plaincopy
/**
* 远程下载文件并读取返回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();
uc.setDoInput(true);//设置是否要从 URL 连接读取数据,默认为true
uc.connect();
InputStream iputstream = uc.getInputStream();
System.out.println("file size is:"+uc.getContentLength());//打印文件长度
byte[] buffer = new byte[4*1024];
int byteRead = -1;
while((byteRead=(iputstream.read(buffer)))!= -1){
oputstream.write(buffer, 0, byteRead);
}
oputstream.flush();
iputstream.close();
oputstream.close();
//读取文件
StringBuffer strb = new StringBuffer();
FileInputStream fs = new FileInputStream(new File(savePath+"//"+uname));
InputStreamReader isr = new InputStreamReader(fs,"UTF-8");
BufferedReader br = new BufferedReader(isr);
String data = "";
while((data = br.readLine()) != null){
strb.append(data + "\n");
}
br.close();
fs.close();
isr.close();
System.out.println(strb.toString());
return strb.toString();
} catch (Exception e) {
System.out.println("读取失败!");
e.printStackTrace();
}
return null;
}
[java] view
plaincopy
/**
* 远程下载文件并读取返回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();
uc.setDoInput(true);//设置是否要从 URL 连接读取数据,默认为true
uc.connect();
InputStream iputstream = uc.getInputStream();
System.out.println("file size is:"+uc.getContentLength());//打印文件长度
byte[] buffer = new byte[4*1024];
int byteRead = -1;
while((byteRead=(iputstream.read(buffer)))!= -1){
oputstream.write(buffer, 0, byteRead);
}
oputstream.flush();
iputstream.close();
oputstream.close();
//读取文件
StringBuffer strb = new StringBuffer();
FileInputStream fs = new FileInputStream(new File(savePath+"//"+uname));
InputStreamReader isr = new InputStreamReader(fs,"UTF-8");
BufferedReader br = new BufferedReader(isr);
String data = "";
while((data = br.readLine()) != null){
strb.append(data + "\n");
}
br.close();
fs.close();
isr.close();
System.out.println(strb.toString());
return strb.toString();
} catch (Exception e) {
System.out.println("读取失败!");
e.printStackTrace();
}
return null;
}
相关文章推荐
- [Java]远程下载文件并读取实例方法
- [Java]远程下载文件并读取实例方法
- [Java]远程下载文件并读取实例方法
- Java读取并下载网络文件的方法
- java利用SMB读取远程文件的方法
- Java利用SMB上传、下载、读取(内容或文件名)、复制、删除远程文件
- Android apk安装过程及Java、JNI读取安装包内assets资源文件的两种方法(附源码实例)
- java读取properties文件的方法实例分析
- Android apk安装过程及Java、JNI读取安装包内assets资源文件的两种方法(附源码实例)
- [Java]文件下载实例方法
- [Java]读取文件方法大全
- [Java]读取文件方法大全
- Java读取Unicode文件(UTF-8等)时碰到的BOM首字符问题,及处理方法
- 安卓(JAVA)读取Mp3文件中的歌曲信息(及readLine()出现乱码问题的解决方法)
- java基于jxl.jar包生成Excel文件到指定目录和服务器下载两种模式的使用方法
- java 读取文件路径空格、"+"和中文的处理方法
- 关于java读取excle文件的相关方法 ;
- java读取properties文件方法
- Java多线程下载文件实例详解
- 方法:一个简单的读取配置文件.properties的工具类 JAVA