远程服务器文件目录获取以及所属文件读取
2017-10-28 14:28
253 查看
package com.jiuqi;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.util.NodeList;
public class IndexOF {
public static void main(String[] args) throws Exception {
String[] urls = DownAndReadFile("http://221.226.108.218:9090//file//min//");
if(urls != null){
for(String url : urls){
System.out.println(url);
String str = getFileContent("http://221.226.108.218:9090//file//min//ST20171018100000.txt");
System.out.println(str);
}
}
}
/**
* 远程下载文件并读取返回p
* @param filePath 文件网络地址 如 http://www.baidu.com/1.txt
* @return String
* @throws IOException
*/
public static String[] DownAndReadFile(String filePath) throws IOException{
HttpURLConnection uc = null;
InputStream iputstream = null;
BufferedReader in = null;
StringBuilder sb = new StringBuilder();
String[] fileArr = null;
try {
URL url = new URL(filePath);
uc = (HttpURLConnection) url.openConnection();
uc.setDoInput(true);//设置是否要从 URL 连接读取数据,默认为true
uc.connect();
iputstream = uc.getInputStream();
in = new BufferedReader(new InputStreamReader(iputstream, "utf-8"));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null) {
buffer.append(line);
}
String nginxStr = buffer.toString();
Parser parser = new Parser(nginxStr);
NodeFilter filter = new HasAttributeFilter("a");
NodeList nodes = parser.extractAllNodesThatMatch(filter);
for (int i = 1; i < nodes.size(); i++) {
Node node = nodes.elementAt(i);
sb.append(node.toPlainTextString() + " ");
}
fileArr = sb.toString().split(" ");
for(int j = 0; j < fileArr.length;j++){
fileArr[j] = filePath + fileArr[j];
}
} catch (Exception e) {
e.printStackTrace();
} finally{
if(in != null){
in.close();
in = null;
}
if(iputstream != null){
iputstream.close();
iputstream = null;
}
}
return fileArr;
}
public static String getFileContent(String filePath) throws IOException{
String str = "";
HttpURLConnection uc = null;
BufferedReader in = null;
ae50
InputStream iputstream = null;
try {
URL url = new URL(filePath);
uc = (HttpURLConnection) url.openConnection();
uc.setDoInput(true);//设置是否要从 URL 连接读取数据,默认为true
uc.connect();
iputstream = uc.getInputStream();
in = new BufferedReader(new InputStreamReader(iputstream, "gbk"));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null) {
buffer.append(line);
}
str = buffer.toString();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(in != null){
in.close();
in = null;
}
if(iputstream != null){
iputstream.close();
iputstream = null;
}
}
return str;
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.util.NodeList;
public class IndexOF {
public static void main(String[] args) throws Exception {
String[] urls = DownAndReadFile("http://221.226.108.218:9090//file//min//");
if(urls != null){
for(String url : urls){
System.out.println(url);
String str = getFileContent("http://221.226.108.218:9090//file//min//ST20171018100000.txt");
System.out.println(str);
}
}
}
/**
* 远程下载文件并读取返回p
* @param filePath 文件网络地址 如 http://www.baidu.com/1.txt
* @return String
* @throws IOException
*/
public static String[] DownAndReadFile(String filePath) throws IOException{
HttpURLConnection uc = null;
InputStream iputstream = null;
BufferedReader in = null;
StringBuilder sb = new StringBuilder();
String[] fileArr = null;
try {
URL url = new URL(filePath);
uc = (HttpURLConnection) url.openConnection();
uc.setDoInput(true);//设置是否要从 URL 连接读取数据,默认为true
uc.connect();
iputstream = uc.getInputStream();
in = new BufferedReader(new InputStreamReader(iputstream, "utf-8"));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null) {
buffer.append(line);
}
String nginxStr = buffer.toString();
Parser parser = new Parser(nginxStr);
NodeFilter filter = new HasAttributeFilter("a");
NodeList nodes = parser.extractAllNodesThatMatch(filter);
for (int i = 1; i < nodes.size(); i++) {
Node node = nodes.elementAt(i);
sb.append(node.toPlainTextString() + " ");
}
fileArr = sb.toString().split(" ");
for(int j = 0; j < fileArr.length;j++){
fileArr[j] = filePath + fileArr[j];
}
} catch (Exception e) {
e.printStackTrace();
} finally{
if(in != null){
in.close();
in = null;
}
if(iputstream != null){
iputstream.close();
iputstream = null;
}
}
return fileArr;
}
public static String getFileContent(String filePath) throws IOException{
String str = "";
HttpURLConnection uc = null;
BufferedReader in = null;
ae50
InputStream iputstream = null;
try {
URL url = new URL(filePath);
uc = (HttpURLConnection) url.openConnection();
uc.setDoInput(true);//设置是否要从 URL 连接读取数据,默认为true
uc.connect();
iputstream = uc.getInputStream();
in = new BufferedReader(new InputStreamReader(iputstream, "gbk"));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null) {
buffer.append(line);
}
str = buffer.toString();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(in != null){
in.close();
in = null;
}
if(iputstream != null){
iputstream.close();
iputstream = null;
}
}
return str;
}
}
相关文章推荐
- centos6.4下定时通过ftp获取数据库服务器上的文件并且删除远程目录里面的文件
- Xcode9学习笔记74 - 读取和解析Plist属性列表文件(获取远程服务器信息并显示返回数据)
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)
- php实现远程网络文件下载到服务器指定目录(方法二)
- Java-读取某个目录下所有文件、文件夹和3种从文件路径中获取文件名的方法
- ubuntu各文件目录作用以及启动流程 分类: ubuntu学习 服务器等硬件学习 2014-12-01 16:06 142人阅读 评论(0) 收藏
- 使用HttpWebRequest以及HttpWebResponse读取Http远程文件[转]
- Android连接服务器,从服务器获取数据,以及从服务器下载文件(单,多线程)
- A网站引用B服务器虚拟目录文件导致:网站部署到IIS7上出现HTTP 错误 500.19(由于权限不足而无法读取配置文件)的问题
- 通过java读取远程nfs服务器文件
- 通过vbs获取远程host文件并保存到指定目录
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)
- 黑马程序员——list()返回该目录下文件,文件夹,listFiles()返回文件文,件夹对象,递归调用获取所有文件,文件夹,以及文件名过滤
- Java项目中读取properties文件,以及六种获取路径的方法
- 遍历某个文件夹(目录)下的所有文件 以及获取文件夹大小
- java实现远程连接服务器,监控某个目录下的日志文件
- [转]Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)
- php实现远程网络文件下载到服务器指定目录(方法一)
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)
- 在Action中获取项目的服务器路径and读取配置文件common.properties