Download file from remote serveri want to download a collection of files from webserver
17 Replies - 22170 Views - Last Post: 13 September 2007 - 07:37 PM
#1
Download file from remote server
Posted 12 September 2007 - 08:07 PM
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
Replies To: Download file from remote server
#2
Re: Download file from remote server
Posted 12 September 2007 - 10:29 PM
Also, what you've written so far doesn't mention which files do you want to download.
#3
Re: Download file from remote server
Posted 12 September 2007 - 10:37 PM
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();
}
}
1lacca, on 12 Sep, 2007 - 10:29 PM, said:
Also, what you've written so far doesn't mention which files do you want to download.
*modedit: code tags added, password hidden
#4
Re: Download file from remote server
Posted 12 September 2007 - 11:50 PM
And next time please use code tags to post your code.
#5
Re: Download file from remote server
Posted 12 September 2007 - 11:53 PM
#6
Re: Download file from remote server
Posted 12 September 2007 - 11:59 PM
#7
Re: Download file from remote server
Posted 13 September 2007 - 12:03 AM
#8
Re: Download file from remote server
Posted 13 September 2007 - 12:16 AM
Quote
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?
#9
Re: Download file from remote server
Posted 13 September 2007 - 12:22 AM
I dont find this kind of error.Check it once again
1lacca, on 13 Sep, 2007 - 12:16 AM, said:
Quote
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.
#10
Re: Download file from remote server
Posted 13 September 2007 - 12:34 AM
Anyway, do you get the file list and are the files generated locally, but they are empty?
#11
Re: Download file from remote server
Posted 13 September 2007 - 12:39 AM
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
1lacca, on 13 Sep, 2007 - 12:34 AM, said:
Anyway, do you get the file list and are the files generated locally, but they are empty?
#12
Re: Download file from remote server
Posted 13 September 2007 - 12:45 AM
#13
Re: Download file from remote server
Posted 13 September 2007 - 12:55 AM
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
1lacca, on 13 Sep, 2007 - 12:45 AM, said:
#14
Re: Download file from remote server
Posted 13 September 2007 - 01:09 AM
[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);
}
agpasam, on 13 Sep, 2007 - 12:55 AM, said:
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
1lacca, on 13 Sep, 2007 - 12:45 AM, said:
#15
Re: Download file from remote server
Posted 13 September 2007 - 01:18 AM
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.
|
|

New Topic/Question
Reply




MultiQuote




|