2013-01-08 123 views
1

我想从一个java应用程序访问存储在http目录下的文件,例如:http://www.zen134237.zen.co.uk/ 。假定服务器允许目录列表或它在FTP上。有人能指出我正确的方向吗?如何从java应用程序访问http服务器目录?

+2

存在准备使用的http客户端。你有没有尝试使用其中之一?见[这里](http://stackoverflow.com/questions/3089394/java-httpclient-how-to-download-all-the-files-from-a-given-folder?rq=1)。 – mrab

回答

1

你提到你想访问http目录下的文件。你知道这个文件的位置吗?您可以使用Java的net包并打开一个url连接。然后从这个URL连接中提取内容。

从Java教程摘自:

http://docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html

import java.net.*; 
import java.io.*; 

public class URLConnectionReader { 
    public static void main(String[] args) throws Exception { 
     URL oracle = new URL("http://www.oracle.com/"); 
     URLConnection yc = oracle.openConnection(); 
     BufferedReader in = new BufferedReader(new InputStreamReader(
            yc.getInputStream())); 
     String inputLine; 
     while ((inputLine = in.readLine()) != null) 
      System.out.println(inputLine); 
     in.close(); 
    } 
} 
相关问题