FTP Client

Hello all.

I am trying to use a FTP client to send and retrieve files from a remote FTP server.

I have a basic GUI and some code; when I try to put a file onto the server, I get this error:

Error: sun.net.ftp.FtpProtocolException: STOR

As far as I can see, the file is not protected (ie read only etc) and I have tried it on two different FTP servers each time with the same results.

Here is the code I am using:

protectedvoid putFile(){

if (localFileName.length() == 0){

setMessage("Please enter file name");

}

byte[] buffer =newbyte[BUFFER_SIZE];

try{

File f =new File(localFileName);

int size = (int) f.length();

setMessage("File " + localFileName +": " + size +" bytes");

setProgressMaximum(size);

FileInputStream in =new FileInputStream(localFileName);

OutputStream out = ftpClient.put(remoteFileName);

int counter = 0;

while (true){

int bytes = in.read(buffer);

if (bytes < 0)

break;

out.write(buffer, 0, bytes);

counter += bytes;

setProgressValue(counter);

int proc = (int) Math

.round(m_progress.getPercentComplete() * 100);

setProgressString(proc +" %");

}

out.close();

in.close();

}catch (Exception ex){

setMessage("Error: " + ex.toString());

}

}

If anyone can tell me what I am doing wrong, I woruld be very greatfull.

Thanks Ota

[2559 byte] By [Otacustesa] at [2007-11-27 6:22:27]
# 1
check this out http://www.nsftools.com/tips/RawFTP.htmBefore transfering the file to remote machine you have to adopt the way how you will send it. The server will connect you or you will conenct with the server to send that file.After this send the file to the server.
jawadhashmia at 2007-7-12 17:39:27 > top of Java-index,Java Essentials,Java Programming...
# 2

Hi,

something very similar worked for me, so my question is whether you're sure that the FTP connection was opened correctly. I'm executing the following statements before transfer:

ftpClient.openServer(host);

ftpClient.login(user, passwd);

ftpClient.binary();

If you still have that problem, could you give more info on how you connect to the server and how your file names look like?

martin@worka at 2007-7-12 17:39:27 > top of Java-index,Java Essentials,Java Programming...
# 3

To check that the connection with the server has established send any command and then wait for the response from the server.

can you post the code for ftpClient if it is your custom class.

You can send and receive commands after connecting with the FTP server on port 21. But for sending and receiving any files you have to open another connection with the server to send or receive the file or the server will connect with you to send/receive the file.

You have to send the PASV/PORT before STOR command.

Go through the link which I sent you earlier.

jawadhashmia at 2007-7-12 17:39:27 > top of Java-index,Java Essentials,Java Programming...
# 4

Hi guys,

Thanks for your replies.

I believe that I am logging in correctly as I get a login ok response. Looking at the sun.net.ftp.FtpClient code, it uses port 21 which is why I didn't do anything with it.

For your reference, here is the complete code:import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Cursor;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.event.WindowListener;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.StringTokenizer;

import javax.swing.JButton;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JPasswordField;

import javax.swing.JProgressBar;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.SwingUtilities;

import javax.swing.border.BevelBorder;

import javax.swing.border.EmptyBorder;

import java.awt.Dimension;

import sun.net.TelnetInputStream;

import sun.net.ftp.FtpClient;

public class BasicGUI extends JFrame {

public static int BUFFER_SIZE = 10240;

protected JTextField userNameTextField = new JTextField("admin");

protected JPasswordField passwordTextField = new JPasswordField("******");

protected JTextField urlTextField = new JTextField("ftp://192.168.1.15/test/");

protected JTextField fileTextField = new JTextField(10);

protected JTextArea monitorTextArea = new JTextArea(10, 30);

protected JProgressBar m_progress = new JProgressBar();

protected JButton putButton = new JButton("Send File");

protected JButton getButton;

protected JButton fileButton = new JButton("Choose File");

protected JButton closeButton = new JButton("Quit & Disconnect");

protected JFileChooser fileChooser = new JFileChooser();

protected FtpClient ftpClient;

protected String localFileName;

protected String remoteFileName;

public BasicGUI() {

super("FTP Client");

JPanel p = new JPanel();

p.setBorder(new EmptyBorder(5, 5, 5, 5));

p.add(new JLabel("UserName:"));

p.add(userNameTextField);

p.add(new JLabel("Password:"));

p.add(passwordTextField);

p.add(new JLabel("FTP Server:"));

p.add(urlTextField);

p.add(new JLabel("FileName:"));

p.add(fileTextField);

m_progress.show(false);

monitorTextArea.setEditable(false);

JScrollPane ps = new JScrollPane(monitorTextArea);

p.add(ps);

m_progress.setStringPainted(true);

m_progress.setBorder(new BevelBorder(BevelBorder.LOWERED, Color.white,

Color.gray));

m_progress.setMinimum(0);

JPanel p1 = new JPanel(new BorderLayout());

p1.add(m_progress, BorderLayout.CENTER);

p.add(p1);

ActionListener lst = new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (connect()) {

Thread uploader = new Thread() {

public void run() {

putFile();

disconnect();

}

};

uploader.start();

}

}

};

putButton.addActionListener(lst);

//putButton.setMnemonic('p');

p.add(putButton);

getButton = new JButton("Retrieve File");

lst = new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (connect()) {

Thread downloader = new Thread() {

public void run() {

getFile();

disconnect();

}

};

downloader.start();

}

}

};

getButton.addActionListener(lst);

//getButton.setMnemonic('g');

p.add(getButton);

lst = new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (fileChooser.showSaveDialog(BasicGUI.this) != JFileChooser.APPROVE_OPTION)

return;

File f = fileChooser.getSelectedFile();

fileTextField.setText(f.getPath());

}

};

fileButton.addActionListener(lst);

//fileButton.setMnemonic('f');

p.add(fileButton);

lst = new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (ftpClient != null)

disconnect();

else

System.exit(0);

}

};

closeButton.addActionListener(lst);

closeButton.setDefaultCapable(true);

//closeButton.setMnemonic('g');

p.add(closeButton);

getContentPane().add(p, BorderLayout.CENTER);

fileChooser.setCurrentDirectory(new File("."));

fileChooser

.setApproveButtonToolTipText("Select file for upload/download");

WindowListener wndCloser = new WindowAdapter() {

public void windowClosing(WindowEvent e) {

disconnect();

System.exit(0);

}

};

addWindowListener(wndCloser);

setSize(720, 240);

setVisible(true);

}

public void setButtonStates(boolean state) {

putButton.setEnabled(state);

getButton.setEnabled(state);

fileButton.setEnabled(state);

}

protected boolean connect() {

monitorTextArea.setText("");

setButtonStates(false);

closeButton.setText("Cancel");

setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

String user = userNameTextField.getText();

if (user.length() == 0) {

setMessage("Please enter user name");

setButtonStates(true);

return false;

}

String password = new String(passwordTextField.getPassword());

String sUrl = urlTextField.getText();

if (sUrl.length() == 0) {

setMessage("Please enter URL");

setButtonStates(true);

return false;

}

localFileName = fileTextField.getText();

// Parse URL

int index = sUrl.indexOf("//");

if (index >= 0)

sUrl = sUrl.substring(index + 2);

index = sUrl.indexOf("/");

String host = sUrl.substring(0, index);

sUrl = sUrl.substring(index + 1);

String sDir = "";

index = sUrl.lastIndexOf("/");

if (index >= 0) {

sDir = sUrl.substring(0, index);

sUrl = sUrl.substring(index + 1);

}

remoteFileName = sUrl;

try {

setMessage("Connecting to host " + host);

ftpClient = new FtpClient(host);

ftpClient.login(user, password);

setMessage("User " + user + " login OK");

setMessage(ftpClient.welcomeMsg);

ftpClient.cd(sDir);

setMessage("Directory: " + sDir);

ftpClient.binary();

return true;

} catch (Exception ex) {

setMessage("Error: " + ex.toString());

setButtonStates(true);

return false;

}

}

protected void disconnect() {

if (ftpClient != null) {

try {

ftpClient.closeServer();

} catch (IOException ex) {

}

ftpClient = null;

}

Runnable runner = new Runnable() {

public void run() {

m_progress.setValue(0);

putButton.setEnabled(true);

getButton.setEnabled(true);

fileButton.setEnabled(true);

closeButton.setText("Quit & Disconnect");

BasicGUI.this.setCursor(Cursor

.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

}

};

SwingUtilities.invokeLater(runner);

}

protected void getFile() {

if (localFileName.length() == 0) {

localFileName = remoteFileName;

SwingUtilities.invokeLater(new Runnable() {

public void run() {

fileTextField.setText(localFileName);

}

});

}

byte[] buffer = new byte[BUFFER_SIZE];

try {

int size = getFileSize(ftpClient, remoteFileName);

if (size > 0) {

setMessage("File " + remoteFileName + ": " + size + " bytes");

setProgressMaximum(size);

} else

setMessage("File " + remoteFileName + ": size unknown");

FileOutputStream out = new FileOutputStream(localFileName);

InputStream in = ftpClient.get(remoteFileName);

int counter = 0;

while (true) {

int bytes = in.read(buffer);

if (bytes < 0)

break;

out.write(buffer, 0, bytes);

counter += bytes;

if (size > 0) {

setProgressValue(counter);

int proc = (int) Math

.round(m_progress.getPercentComplete() * 100);

setProgressString(proc + " %");

} else {

int kb = counter / 1024;

setProgressString(kb + " KB");

}

}

out.close();

in.close();

} catch (Exception ex) {

setMessage("Error: " + ex.toString());

}

}

protected void putFile() {

if (localFileName.length() == 0) {

setMessage("Please enter file name");

}

byte[] buffer = new byte[BUFFER_SIZE];

try {

File f = new File(localFileName);

int size = (int) f.length();

setMessage("File " + localFileName + ": " + size + " bytes");

setProgressMaximum(size);

FileInputStream in = new FileInputStream(localFileName);

OutputStream out = ftpClient.put(remoteFileName);

int counter = 0;

while (true) {

int bytes = in.read(buffer);

if (bytes < 0)

break;

out.write(buffer, 0, bytes);

counter += bytes;

setProgressValue(counter);

int proc = (int) Math

.round(m_progress.getPercentComplete() * 100);

setProgressString(proc + " %");

}

out.close();

in.close();

} catch (Exception ex) {

setMessage("Error: " + ex.toString());

}

}

protected void setMessage(final String str) {

if (str != null) {

Runnable runner = new Runnable() {

public void run() {

monitorTextArea.append(str + '\n');

monitorTextArea.repaint();

}

};

SwingUtilities.invokeLater(runner);

}

}

protected void setProgressValue(final int value) {

Runnable runner = new Runnable() {

public void run() {

m_progress.setValue(value);

}

};

SwingUtilities.invokeLater(runner);

}

protected void setProgressMaximum(final int value) {

Runnable runner = new Runnable() {

public void run() {

m_progress.setMaximum(value);

}

};

SwingUtilities.invokeLater(runner);

}

protected void setProgressString(final String string) {

Runnable runner = new Runnable() {

public void run() {

m_progress.setString(string);

}

};

SwingUtilities.invokeLater(runner);

}

public static int getFileSize(FtpClient client, String fileName)

throws IOException {

TelnetInputStream lst = client.list();

String str = "";

fileName = fileName.toLowerCase();

while (true) {

int c = lst.read();

char ch = (char) c;

if (c < 0 || ch == '\n') {

str = str.toLowerCase();

if (str.indexOf(fileName) >= 0) {

StringTokenizer tk = new StringTokenizer(str);

int index = 0;

while (tk.hasMoreTokens()) {

String token = tk.nextToken();

if (index == 4)

try {

return Integer.parseInt(token);

} catch (NumberFormatException ex) {

return -1;

}

index++;

}

}

str = "";

}

if (c <= 0)

break;

str += ch;

}

return -1;

}

public static void main(String argv[]) {

new BasicGUI();

}

}

I am trying to put files on a local NAS with FTP capabilites. I have checked and it is using port 21. I have also tried to upload files to my webspace via FTP with no luck.

Any help is appreciated.

Thanks

Ota

Otacustesa at 2007-7-12 17:39:27 > top of Java-index,Java Essentials,Java Programming...
# 5
> sun.net.ftp.FtpClientdont use classes in the sun package. http://java.sun.com/products/jdk/faq/faq-sun-packages.html
mlka at 2007-7-12 17:39:28 > top of Java-index,Java Essentials,Java Programming...
# 6
Thanks mlk...I was afraid someone would say that!!... a re-think may be in order. doh!!ota
Otacustesa at 2007-7-12 17:39:28 > top of Java-index,Java Essentials,Java Programming...
# 7
edtFTPj is an open source FTP client.[url= http://www.enterprisedt.com/products/edtftpj/download.html]Download[/url]~Tim
SomeoneElsea at 2007-7-12 17:39:28 > top of Java-index,Java Essentials,Java Programming...
# 8
You have to check the documenation of the FTPClient.I think before receiving or sending any file you have to ask the FTPClient to go in the send/receive mode.
jawadhashmia at 2007-7-12 17:39:28 > top of Java-index,Java Essentials,Java Programming...