Java:你能解析服务器上的文件吗?

时间:2014-07-23 04:13:29

标签: java client-server download

我一直都明白,为了阅读服务器上的文件,你必须先下载它,即:

  URL url = new URL(myUrl);
              connection = (HttpURLConnection) url.openConnection();
              connection.connect();


              input = connection.getInputStream();
              output = new FileOutputStream(TEMP_FILE_PATH); 

             byte data[] = new byte[4096];
              long total = 0;
              int count;
              while ((count = input.read(data)) != -1) {
                  // allow canceling with back button
                  if (isCancelled()) {
                      input.close();
                      return null;
                  }
                   output.write(data, 0, count);
              }

我的假设不正确吗?你可以在不下载文件的情况下阅读/解析文件吗?

2 个答案:

答案 0 :(得分:0)

  

是否可以解析服务器上的文件?

是的,如果您的解析器在服务器上运行。如果您的解析器没有直接访问该文件而运行,那么您必须以某种方式获取该文件以进行解析。

答案 1 :(得分:0)

  

是否可以解析服务器上的文件?

绝对你可以。

根据您的代码段,您似乎想要从远程服务器或http请求中读取和解析文件内容。

我有一个应用程序,用户可以从远程文件服务器预览文件。

如果您可以使用" myUrl"来访问文件直接地,您还可以在java中读取和解析文件。

请尝试使用下面的代码段。

您可能需要包含 org.apache.http.client.HttpClient 库。

HTTP GET示例

String myUrl = "http://enter.the.url.you.want";
OutputStream output = new FileOutputStream("TEMP_FILE_PATH");

// Define Header information if you want to have.
Map<String, String> headers = new HashMap<String, String>();
headers.put("range", "bytes=0-51199");

// org.apache.http.client.HttpClient
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(myUrl);

if(headers != null){
    Iterator<String> iter = headers.keySet().iterator();
    while(iter.hasNext()){
        String key = (String)iter.next();
        get.addHeader(key, headers.get(key));
    }
}

HttpResponse res = client.execute(get);
InputStream input = res.getEntity().getContent();

int count = 0;
byte[] data = new byte[4096];

StringBuilder sb = new StringBuilder();

while ((count = input.read(data)) != -1) {
    sb.append(new String(data, 0, count, "UTF-8"));
    // Here is the place you can read a file.
    output.write(data);
}

input.close();
output.close();

HTTP POST示例

String myUrl = "http://enter.the.url.you.want";
OutputStream output = new FileOutputStream("TEMP_FILE_PATH");

// Define parameters if you want to have.
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("key", "some-value"));

// Define Header information if you want to have.
Map<String, String> headers = new HashMap<String, String>();
headers.put("range", "bytes=0-51199");

// org.apache.http.client.HttpClient
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(myUrl);
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

if(headers != null){
    Iterator<String> iter = headers.keySet().iterator();
    while(iter.hasNext()){
        String key = (String)iter.next();
        post.addHeader(key, headers.get(key));
    }
}

HttpResponse res = client.execute(post);
InputStream input = res.getEntity().getContent();

int count = 0;
byte[] data = new byte[4096];

StringBuilder sb = new StringBuilder();

while ((count = input.read(data)) != -1) {
    sb.append(new String(data, 0, count, "UTF-8"));
    // Here is the place you can read a file.
    output.write(data);
}

input.close();
output.close();