Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Fetch files over web server (http://www.go4expert.com/forums/fetch-files-web-server-t16038/)

Juuno 3Feb2009 09:15

Fetch files over web server
Hi, I am just a newbie and I need to write some programs.

I have one directory and under that directory, I have 35 xml files in the Apache Web Server. The path for the directory is like //localhost:8081/dirxml/

And path for the xml files are like..
.... and so on....

I would like to fetch them from my java program and then produce an xml file which is a combination of those xml files.

So, how can I fetch those xml files from web server simultaneously? Is it ok if I use java.net.URL class. But what I found so far is it needs to give the exact URL path like //localhost:8081/dirxml/mycontacts.xml?? But I can't do it coz I don't want to give those xml file names and fetches. What I want to do is to fetch the files under that directory without knowing the file name. It is like to fetch all the files under that directory which ends with .xml

How can I do this? And is there any reference for it.

Thanks in advance.

devunion 3Feb2009 16:49

Re: Fetch files over web server
You can use next code to retrieve list of files:


        try {
            URL url = new URL(YOUR_URL_HERE);
            InputStream inputStream = url.openStream();
            System.out.println("inputStream.available() = " + inputStream.available());

            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

            String line = null;

            while ((line = br.readLine()) != null) {
                System.out.println("line = " + line);
        } catch (MalformedURLException e) {
        } catch (IOException e) {

graveyard220 19Feb2009 07:06

Re: Fetch files over web server
there is no simple way to do that because this is not a real path for a file system so this will be some huge code because you want to parse an html resulted from specified url and get the resulted inline files,
but fortunately apache did that effort and make a package called ivr this have more utilities to do such that things,
you can download the binary from this location:

graveyard220 19Feb2009 07:06

Re: Fetch files over web server
ht tp:/ /a nt. apa che. org /i v y/d o w n lo ad. cgi

and then use it as simple as:

graveyard220 19Feb2009 07:12

Re: Fetch files over web server
sorry for that cutting

package networkanddatabase;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;

import org.apache.ivy.util.url.ApacheURLLister;

public class FetchFilesFromHttpURL {
        public static void main(String[] args) {
                URL url;
                try {
                        url = new URL(youurl);
                        File destFolder = new File("c:\\test");
                        ApacheURLLister lister = new ApacheURLLister();
                        // this list of URLs objects
                        List files = lister.listAll(url);
                        System.out.println("list file is complete.."+files);
                        for (Iterator iter = files.iterator(); iter.hasNext();) {
                                URL fileUrl = (URL) iter.next();
                                httpFileDownload(fileUrl, destFolder);
                        System.out.println("download is complete..");
                } catch (Exception e) {


        public static void httpFileDownload(URL url, File destFolder) throws Exception {
                File destination = new File(destFolder, url.getFile());
                BufferedInputStream bis = null;
                BufferedOutputStream bos = null;
                try {
                        URLConnection urlc = url.openConnection();

                        bis = new BufferedInputStream(urlc.getInputStream());
                        bos = new BufferedOutputStream(new FileOutputStream(destination.getPath()));

                        int i;
                        while ((i = bis.read()) != -1) {
                } finally {
                        if (bis != null)
                                try {
                                } catch (IOException ioe) {
                        if (bos != null)
                                try {
                                } catch (IOException ioe) {

devunion 19Feb2009 10:24

Re: Fetch files over web server
Good solution, thanks!

All times are GMT +5.5. The time now is 04:21.