Download file from remote server

i want to download a collection of files from webserver

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 26701 Views - Last Post: 13 September 2007 - 07:37 PM Rate Topic: -----

#1 agpasam  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 12-September 07

Download file from remote server

Posted 12 September 2007 - 08:07 PM

Hi

I want to download a list of files from server using Java

example.

server name: http://google.com
uname:aa
pwd:aa
directory name:ss/

when i submit this details it will automatically download file to svaed to local drive


if anyone help me plz
Is This A Good Question/Topic? 0
  • +

Replies To: Download file from remote server

#2 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Download file from remote server

Posted 12 September 2007 - 10:29 PM

You should specify what exactly is causing you a problem. We are not giving complete solutions. If you've written something so far, post it, so we can tell you how to complete it.
Also, what you've written so far doesn't mention which files do you want to download.
Was This Post Helpful? 0
  • +
  • -

#3 agpasam  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 12-September 07

Re: Download file from remote server

Posted 12 September 2007 - 10:37 PM

i have written code like this
i want to download files from remote server

This code working fine
but no data in downloaded file.
please let me know what is the problem


import java.util.*;
public class frmFileDownload {
	public static void main (String[] args) {
		try {
			clsFileDownload ftp = new clsFileDownload ();
			String serverName = "http://govindapasam.741.com";
			if (ftp.connectAndLogin(serverName, "*********", "********")) {
				System.out.println("Connected to " + serverName);
				try {
					System.out.println("Welcome message:\n" + ftp.getReplyString());

					ftp.setPassiveMode(true);

					Vector v =(Vector)ftp.listFileNames();

					ListIterator iter = v.listIterator();
					while (iter.hasNext())
					{

					//ftp.ascii();
					String fileName=iter.next().toString();

					String filePath="C:/Pasam/Java/FtpUpload_Thu/ServerFiles/" + fileName;
					System.out.println(fileName);
					ftp.downloadFile(fileName,filePath);
					}

				} catch (Exception ftpe) {
					ftpe.printStackTrace();
				} finally {
					ftp.logout();
					ftp.disconnect();
				}
			} else {
				System.out.println("Unable to connect to" + serverName);
			}
			System.out.println("Finished");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
}



class file


import org.apache.commons.net.ftp.*;
import java.util.Vector;
import java.io.*;
import java.net.UnknownHostException;



public class clsFileDownload extends FTPClient {

	/** A convenience method for connecting and logging in */
	public boolean connectAndLogin (String host, String userName, String password)
			throws  IOException, UnknownHostException, FTPConnectionClosedException {
		boolean success = false;
		connect(host);
		int reply = getReplyCode();
		if (FTPReply.isPositiveCompletion(reply))
			success = login(userName, password);
		if (!success)
			disconnect();
		return success;
	}

	/** Turn passive transfer mode on or off. If Passive mode is active, a
	  * PASV command to be issued and interpreted before data transfers;
	  * otherwise, a PORT command will be used for data transfers. If you're
	  * unsure which one to use, you probably want Passive mode to be on. */
	public void setPassiveMode(boolean setPassive) {
		if (setPassive)
			enterLocalPassiveMode();
		else
			enterLocalActiveMode();
	}

	/** Use ASCII mode for file transfers */
	public boolean ascii () throws IOException {
		return setFileType(FTP.ASCII_FILE_TYPE);
	}

	/** Use Binary mode for file transfers */
	public boolean binary () throws IOException {
		return setFileType(FTP.BINARY_FILE_TYPE);
	}


	public boolean downloadFile (String serverFile, String localFile)
	throws IOException, FTPConnectionClosedException {
FileOutputStream out = new FileOutputStream(localFile);
boolean result = retrieveFile(serverFile, out);
out.close();
return result;
}




	/** Get the list of files in the current directory as a Vector of Strings
	  * (excludes subdirectories) */
	public Vector<String> listFileNames ()
			throws IOException, FTPConnectionClosedException {
		FTPFile[] files = listFiles("/_webimages/FtpSample/");
		Vector<String> v = new Vector<String>();
		for (int i = 0; i < files.length; i++) {
			if (!files[i].isDirectory())
				v.addElement(files[i].getName());
		}
		return v;
	}

	/** Get the list of files in the current directory as a single Strings,
	  * delimited by \n (char '10') (excludes subdirectories) */
	public String listFileNamesString ()
			throws IOException, FTPConnectionClosedException {
		return vectorToString(listFileNames(), "\n");
	}



	/** Convert a Vector to a delimited String */
	private String vectorToString (Vector v, String delim) {
		StringBuffer sb = new StringBuffer();
		String s = "";
		for (int i = 0; i < v.size(); i++) {
			sb.append(s).append((String)v.elementAt(i));
			s = delim;
		}
		return sb.toString();
	}

}






View Post1lacca, on 12 Sep, 2007 - 10:29 PM, said:

You should specify what exactly is causing you a problem? We are not giving complete solutions. If you've written something so far, post it, so we can tell you how to complete it.
Also, what you've written so far doesn't mention which files do you want to download.


*modedit: code tags added, password hidden
Was This Post Helpful? 0
  • +
  • -

#4 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Download file from remote server

Posted 12 September 2007 - 11:50 PM

Try String filePath="C:\\Pasam\\Java\\FtpUpload_Thu\\ServerFiles\\" + fileName; instead of String filePath="C:/Pasam/Java/FtpUpload_Thu/ServerFiles/" + fileName; .



And next time please use code tags to post your code.
Was This Post Helpful? 0
  • +
  • -

#5 agpasam  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 12-September 07

Re: Download file from remote server

Posted 12 September 2007 - 11:53 PM

hi
same problem is coming again



View Post1lacca, on 12 Sep, 2007 - 11:50 PM, said:

Try String filePath="C:\\Pasam\\Java\\FtpUpload_Thu\\ServerFiles\\" + fileName; instead of String filePath="C:/Pasam/Java/FtpUpload_Thu/ServerFiles/" + fileName; .



And next time please use code tags to post your code.

Was This Post Helpful? 0
  • +
  • -

#6 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Download file from remote server

Posted 12 September 2007 - 11:59 PM

In the downloadFile method try to flush the FileOutputStream before closing.
Was This Post Helpful? 0
  • +
  • -

#7 agpasam  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 12-September 07

Re: Download file from remote server

Posted 13 September 2007 - 12:03 AM

hi

Same problem coming again




View Post1lacca, on 12 Sep, 2007 - 11:59 PM, said:

In the downloadFile method try to flush the FileOutputStream before closing.

Was This Post Helpful? 0
  • +
  • -

#8 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Download file from remote server

Posted 13 September 2007 - 12:16 AM

I ran your code, and I've received an exception:

Quote

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:254)
at java.io.BufferedInputStream.read(BufferedInputStream.java:313)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read(BufferedInputStream.java:235)
at org.apache.commons.net.telnet.TelnetInputStream.__read(TelnetInputStream.java:114)
at org.apache.commons.net.telnet.TelnetInputStream.run(TelnetInputStream.java:535)
at java.lang.Thread.run(Thread.java:595)


Do you have this, too?
Was This Post Helpful? 0
  • +
  • -

#9 agpasam  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 12-September 07

Re: Download file from remote server

Posted 13 September 2007 - 12:22 AM

Hi
I dont find this kind of error.Check it once again




View Post1lacca, on 13 Sep, 2007 - 12:16 AM, said:

I ran your code, and I've received an exception:

Quote

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:254)
at java.io.BufferedInputStream.read(BufferedInputStream.java:313)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read(BufferedInputStream.java:235)
at org.apache.commons.net.telnet.TelnetInputStream.__read(TelnetInputStream.java:114)
at org.apache.commons.net.telnet.TelnetInputStream.run(TelnetInputStream.java:535)
at java.lang.Thread.run(Thread.java:595)


Do you have this, too?



i downloaded from this site check it

http://www.nsftools....ips/JavaFtp.htm

Option 4: Use the Jakarta Net classes

Probably the most attractive option for FTP functionality in Java is the Jakarta Commons Net library, which includes a full-featured FTP client class. The library is pretty well tested, and is also open-source and free to use (as long as you abide by the terms of the Apache license). This library is actually the old ORO NetComponents.jar package, which was donated to Apache several years ago.

Here are some examples of using the Jakarta Commons FTP class:

a wrapper around the Jakarta FTP class: JakartaFtpWrapper.java
a program that uses the wrapper: JakartaWrapperTest.java

Not only is Jakarta FTP much more feature-rich than the other options above, but it also has its own internal support for parsing file/directory listings on multiple server types. To do this it uses the Jakarta ORO library (also open-source and covered by the Apache license) for regular expression parsing, although this is all done transparent to you.

For a good, general purpose FTP client implementation in Java, Jakarta Commons is a good bet.
Was This Post Helpful? 0
  • +
  • -

#10 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Download file from remote server

Posted 13 September 2007 - 12:34 AM

For me the command FTPFile[] files = listFiles("/_webimages/FtpSample/"); seems to throw this stuff all the time (even if called without parameters ).
Anyway, do you get the file list and are the files generated locally, but they are empty?
Was This Post Helpful? 0
  • +
  • -

#11 agpasam  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 12-September 07

Re: Download file from remote server

Posted 13 September 2007 - 12:39 AM

Hi

YES
I got the list of files.
what can i do to get tha file with data
if am using without passing parameter also get same problem



View Post1lacca, on 13 Sep, 2007 - 12:34 AM, said:

For me the command FTPFile[] files = listFiles("/_webimages/FtpSample/"); seems to throw this stuff all the time (even if called without parameters ).
Anyway, do you get the file list and are the files generated locally, but they are empty?

Was This Post Helpful? 0
  • +
  • -

#12 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Download file from remote server

Posted 13 September 2007 - 12:45 AM

I think the problem is that you don't download the file from the /_webimages/FtpSample/ directory. Try to change there first, or put it before the name of the files.
Was This Post Helpful? 0
  • +
  • -

#13 agpasam  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 12-September 07

Re: Download file from remote server

Posted 13 September 2007 - 12:55 AM

Hi

when i list files This method is working fine i am using this ("/_webimages/FtpSample/")

But

[code]
public Vector<String> listFileNames ()
throws IOException, FTPConnectionClosedException {
FTPFile[] files = listFiles("/_webimages/FtpSample/");
Vector<String> v = new Vector<String>();
for (int i = 0; i < files.length; i++) {
if (!files[i].isDirectory())
v.addElement(files[i].getName());
}
return v;
}


But
i calling this method

public boolean downloadFile (String serverFile, String localFile)
throws IOException, FTPConnectionClosedException {
FileOutputStream out = new FileOutputStream(localFile);
boolean result = retrieveFile(serverFile, out);
out.flush()
out.close();
return result;
}


it will return false

i think Here is the problem






View Post1lacca, on 13 Sep, 2007 - 12:45 AM, said:

I think the problem is that you don't download the file from the /_webimages/FtpSample/ directory. Try to change there first, or put it before the name of the files.

Was This Post Helpful? 0
  • +
  • -

#14 agpasam  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 12-September 07

Re: Download file from remote server

Posted 13 September 2007 - 01:09 AM

Here also i changed like this again same problem coming

[code]

while (iter.hasNext())
{

//ftp.ascii();
String fileName=iter.next().toString();
String fileName1="http://govindapasam.741.com/_webimages/FtpSample/"+fileName;
String filePath="C:\\Pasam\\Java\\FtpUpload_Thu\\ServerFiles\\" + fileName;
System.out.println(fileName1);
ftp.setPassiveMode(true);
ftp.downloadFile(fileName1,filePath);
}











View Postagpasam, on 13 Sep, 2007 - 12:55 AM, said:

Hi

when i list files This method is working fine i am using this ("/_webimages/FtpSample/")

But

[code]
public Vector<String> listFileNames ()
throws IOException, FTPConnectionClosedException {
FTPFile[] files = listFiles("/_webimages/FtpSample/");
Vector<String> v = new Vector<String>();
for (int i = 0; i < files.length; i++) {
if (!files[i].isDirectory())
v.addElement(files[i].getName());
}
return v;
}


But
i calling this method

public boolean downloadFile (String serverFile, String localFile)
throws IOException, FTPConnectionClosedException {
FileOutputStream out = new FileOutputStream(localFile);
boolean result = retrieveFile(serverFile, out);
out.flush()
out.close();
return result;
}


it will return false

i think Here is the problem






View Post1lacca, on 13 Sep, 2007 - 12:45 AM, said:

I think the problem is that you don't download the file from the /_webimages/FtpSample/ directory. Try to change there first, or put it before the name of the files.

Was This Post Helpful? 0
  • +
  • -

#15 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Download file from remote server

Posted 13 September 2007 - 01:18 AM

String fileName1="http://govindapasam.741.com/_webimages/FtpSample/"+fileName;
is probably not good. Try simply:
String fileName1="/_webimages/FtpSample/"+fileName;

This was my last shot for a while, because I have to leave, but I guess someone else might help.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2