commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Anil Philip <goodnewsfor...@yahoo.com>
Subject RE: [commons-net ftp] why is commons ftp so slow compared with http?
Date Fri, 11 Aug 2006 14:09:54 GMT
Thanks for replying.
Here is the program I was trying to use. You can input
your login and password in main below, if you wish to
run it.
thanks,
Anil
===========================
import javax.swing.*;
import javax.swing.plaf.basic.BasicArrowButton;
 
import org.apache.commons.net.ProtocolCommandEvent;
import org.apache.commons.net.ProtocolCommandListener;
import org.apache.commons.net.ftp.FTPClient;
import
org.apache.commons.net.ftp.FTPConnectionClosedException;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.io.PrintWriter;
public class RemoteFilesListDialog extends JDialog
implements ActionListener {
	private static RemoteFilesListDialog dialog;
 
	private static String value = "";
 
	private JList list;
 
	DefaultListModel model = new DefaultListModel();
 
	String server, username, password;
 
	static FTPClient ftp;
	
	JTextField fileName = new JTextField(15);
 
	/**
	 * Set up and show the dialog. The first Component
argument determines which
	 * frame the dialog depends on; it should be a
component in the dialog's
	 * controlling frame. The second Component argument
should be null if you
	 * want the dialog to come up with its left corner in
the center of the
	 * screen; otherwise, it should be the component on
top of which the dialog
	 * should appear.
	 * @param function TODO
	 * @throws IOException 
	 */
	public static String showDialog(Component frameComp,
			Component locationComp, String labelText, String
title, String server, String user, String password,
String function) throws IOException {
		Frame frame =
JOptionPane.getFrameForComponent(frameComp);
		if(JOptionPane.showConfirmDialog(frameComp, 
				"Press OK to Browse for it at " + server + "\nIt
may take up to 15 seconds to connect to the server.",
"Selecting", JOptionPane.OK_CANCEL_OPTION) ==
JOptionPane.YES_OPTION) {
				
RemoteFilesListDialog.connect(server,user,password);
		} else 
			return null;
		if(dialog == null) {
			dialog = new RemoteFilesListDialog(frame,
locationComp, labelText, title, function);
		}
		RemoteFilesListDialog.value = null;
		dialog.repopulateList();
		dialog.setVisible(true);
		return value;
	}
 
	private void repopulateList() {
		String[] files;
		try {
			model.clear();
			files = ftp.listNames();
			for (int i = 0; i < files.length; i++) {
				if(files[i].endsWith(".html"))
					model.addElement(files[i]);
			}
		} catch (IOException e) {
		JOptionPane.showMessageDialog(null, e);
			e.printStackTrace();
		}
	}
 
	private RemoteFilesListDialog(Frame frame, Component
locationComp, String labelText,
			String title, String function) {
		super(frame, title, true);
 
		// Create and initialize the buttons.
		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(this);
 
		//
		final JButton setButton = new JButton("Select");
		setButton.setActionCommand("Select");
		setButton.addActionListener(this);
		getRootPane().setDefaultButton(setButton);
 
		// main part of the dialog
		list = new JList(model) {
			// Subclass JList to workaround bug 4832765, which
can cause the
			// scroll pane to not let the user easily scroll up
to the beginning
			// of the list. An alternative would be to set the
unitIncrement
			// of the JScrollBar to a fixed value. You wouldn't
get the nice
			// aligned scrolling, but it should work.
			public int getScrollableUnitIncrement(Rectangle
visibleRect,
					int orientation, int direction) {
				int row;
				if (orientation == SwingConstants.VERTICAL &&
direction < 0
						&& (row = getFirstVisibleIndex()) != -1) {
					Rectangle r = getCellBounds(row, row);
					if ((r.y == visibleRect.y) && (row != 0)) {
						Point loc = r.getLocation();
						loc.y--;
						int prevIndex = locationToIndex(loc);
						Rectangle prevR = getCellBounds(prevIndex,
prevIndex);
 
						if (prevR == null || prevR.y >= r.y) {
							return 0;
						}
						return prevR.height;
					}
				}
				return
super.getScrollableUnitIncrement(visibleRect,
						orientation, direction);
			}
		};
 
	
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
		list.setVisibleRowCount(-1);
		list.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					setButton.doClick(); // emulate button click
				}
			}
		});
		JScrollPane listScroller = new JScrollPane(list);
		listScroller.setPreferredSize(new Dimension(250,
80));
		listScroller.setAlignmentX(LEFT_ALIGNMENT);
 
		// Create a container so that we can add a title
around
		// the scroll pane. Can't add a title directly to
the
		// scroll pane because its background would be
white.
		// Lay out the label and scroll pane from top to
bottom.
		JPanel listPane = new JPanel();
		listPane.setLayout(new BoxLayout(listPane,
BoxLayout.PAGE_AXIS));
		JLabel label = new JLabel(labelText);
		label.setLabelFor(list);
		listPane.add(label);
		listPane.add(Box.createRigidArea(new Dimension(0,
5)));
		listPane.add(listScroller);
	
listPane.setBorder(BorderFactory.createEmptyBorder(10,
10, 10, 10));
 
		if(function.equals("Save"))
			listPane.add(fileName);
		
		// Lay out the buttons from left to right.
		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new BoxLayout(buttonPane,
BoxLayout.LINE_AXIS));
	
buttonPane.setBorder(BorderFactory.createEmptyBorder(0,
10, 10, 10));
		buttonPane.add(Box.createHorizontalGlue());
		buttonPane.add(cancelButton);
		buttonPane.add(Box.createRigidArea(new Dimension(10,
0)));
		buttonPane.add(setButton);
 
		// Put everything together, using the content pane's
BorderLayout.
		Container contentPane = getContentPane();
		contentPane.add(listPane, BorderLayout.CENTER);
		contentPane.add(buttonPane, BorderLayout.PAGE_END);
 
		pack();
		setLocationRelativeTo(locationComp);
	}
 
	// Handle clicks on the Select and Cancel buttons.
	public void actionPerformed(ActionEvent e) {
		if ("Select".equals(e.getActionCommand())) {
			String selected = (String)
(list.getSelectedValue());
			if(selected == null) {
				selected = fileName.getText();
				if(!selected.endsWith(".html"))
					selected = selected + ".html";
			}
			RemoteFilesListDialog.value = selected;
		}
		RemoteFilesListDialog.dialog.setVisible(false);
		try {
			if (ftp.isConnected())
				ftp.disconnect();
		} catch (IOException e1) {
			JOptionPane.showMessageDialog(null, e1);
			e1.printStackTrace();
		}
	}
 
	public static void connect(String server, String
username, String password) throws IOException {
		ftp = new FTPClient();
		ftp.addProtocolCommandListener(new
PrintingCommandListener(
				new PrintWriter(System.out)));
 
		try {
			int reply;
			ftp.connect(server);
			System.out.println("Connected to " + server + ".");
 
			// After connection attempt, you should check the
reply code to
			// verify
			// success.
			reply = ftp.getReplyCode();
 
			if (!FTPReply.isPositiveCompletion(reply)) {
				ftp.disconnect();
				System.err.println("FTP server refused
connection.");
				JOptionPane.showMessageDialog(null,"FTP server
refused connection.");
			}
		} catch (IOException e) {
			if (ftp.isConnected()) {
				try {
					ftp.disconnect();
				} catch (IOException e1) {
					throw e1;
				}
			}
			System.err.println("Could not connect to server.");
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e);
		}
		try {
			if (!ftp.login(username, password)) {
				ftp.logout();
			}
 
			System.out.println("Remote system is " +
ftp.getSystemName());
			System.out.println("My directory is " +
ftp.printWorkingDirectory());
 
			// Use passive mode as default because most of us
are
			// behind firewalls these days.
			// ftp.enterLocalPassiveMode();
		} catch (FTPConnectionClosedException e) {
			System.err.println("Server closed connection.");
			e.printStackTrace();
			throw e;
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		}
 
	} // end main
 
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws
IOException {
		
		String s =
RemoteFilesListDialog.showDialog(null,null,"label","title","myserver.com",
"login", "password", "Save");
	System.out.println("selected file:" + s);
	}
 
}
 
class PrintingCommandListener implements
ProtocolCommandListener {
	private PrintWriter __writer;
 
	public PrintingCommandListener(PrintWriter writer) {
		__writer = writer;
	}
 
	public void protocolCommandSent(ProtocolCommandEvent
event) {
		__writer.print(event.getMessage());
		__writer.flush();
	}
 
	public void
protocolReplyReceived(ProtocolCommandEvent event) {
		__writer.print(event.getMessage());
		__writer.flush();
	}
}


===========================

--- Julius Davies <juliusdavies@cucbc.com> wrote:

> Anil Philip,
> 
> Personally, I find FTPClient a lot snappier and more
> reliable if I go into passive mode:
> 
> FTPClient ftpClient = new FTPClient();
> ftpClient.setDefaultTimeout( 60 * 1000 );
> ftpClient.setSoTimeout( 60 * 1000 );
> ftpClient.setDataTimeout( 60 * 1000 );
> 
> ftpClient.connect( "juliusdavies.ca" );
> ftpClient.login( "julius", "password" );
> 
> // Enter PASSIVE mode!
> ftpClient.enterLocalPassiveMode();
> 
> 
> 
> yours,
> 
> Julius Davies
> 
> http://juliusdavies.ca/
> 
> 
> -----Original Message-----
> From:	Daniel F. Savarese [mailto:dfs@savarese.org]
> Sent:	Thu 8/10/2006 7:48 PM
> To:	commons-user@jakarta.apache.org
> Cc:	
> Subject:	Re: [commons-net ftp] why is commons ftp so
> slow compared with http? 
> 
> 
> In message
>
<20060809035054.15472.qmail@web52701.mail.yahoo.com>,
> Anil Philip wr
> ites:
> >My program is logging into my server and doing an
> ls
> >in an almost empty directory. It takes about 10
> >seconds.
> >Contrast this with: I create an .htaccess file in
> the
> >same dir so that the dir can be accessed in
> Internet
> >Explorer and lists the folder contents. (Options
> >+Indexes). It takes about 2.5 seconds.
> >why is commons-net so painfully slow compared with
> >http for a similar operation? (Is it just the
> >authentication?)
> 
> Presumably you're talking about FTP.  FTPClient is
> far from perfect
> and its implementation is very dated and
> inefficient.  However, one
> should always keep in mind that the basic code has
> been used by 
> innumerable programmers for almost 10 years now, so
> when you run
> into an issue like this, it's probably not
> commons-net, but rather
> how you're using it.
> 
> First, establishing a new FTP connection every time
> you list a
> directory will always be slower than HTTP.  First
> you have to log in,
> then you have to establish a data connection for the
> file listing.
> The fact that it takes 10 seconds for you has to do
> with your
> particular code and runtime environment.  Using a
> modified version
> of the ftp.java example (so that it will print a
> listing in addition
> to performing the file transfer), I can list 
>
ftp://distro.ibiblio.org/pub/linux/distributions/fedora/linux/core/5/i386/os/
> and 
> download
>
ftp://distro.ibiblio.org/pub/linux/distributions/fedora/linux/core/5/i386/os/RPM
> -GP-KEY
> in less than a second.  And that includes the JVM
> start up time (I
> just use /usr/bin/time to time it).
> 
> Without a precisely specified test case that other
> people can run to
> try to reproduce the behavior you are encountering,
> there's no telling
> what you are experiencing.
> 
> daniel
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> commons-user-help@jakarta.apache.org
> 
> 
> 
> 
> 
> >
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
commons-user-help@jakarta.apache.org


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message