0

Java アプリケーションで FTP を使用してファイルをアップロードしたいのですが、うまくいきません。

問題は、メイン クラスで FTPUploader クラスを実行すると完全に機能することですが、ここにあるようにすると機能しません。皆さん、私を助けてくれませんか?

私のコードは次のとおりです。

メインスレッド:

package restrictedareamanager;

    import java.io.File;
    import java.io.IOException;

    public class RestrictedAreaManager {

    File path = new File ("C:\\Área Restrita");
    File lista[] = path.listFiles();

    public void processa () throws IOException {

        String titulo, subtitulo, nomeArq, aux;

        for (File s : this.lista) {
        aux = s.getName();

            //String work
            titulo = aux.substring (0, aux.indexOf ("-"));
            aux = aux.substring (aux.indexOf ("-")+1);
            subtitulo = aux.substring (0, aux.indexOf ("-"));
            aux = aux.substring (aux.indexOf ("-")+1);
            nomeArq = aux.substring (0);

            //Create new file to be copied
            final File dest = new File (path + "\\" + nomeArq);

           //Copy File
           FileCopier copiador = new FileCopier();
           copiador.copiaArquivo(s, dest);

            //Send file via FTP
            FTPUploader ftp = new FTPUploader("**********", "********", "*********", titulo, subtitulo, dest);
            ftp.execute();  

        }

    }

    public static void main(String[] args) throws IOException {

        RestrictedAreaManager ram = new RestrictedAreaManager();
        ram.processa();

    }
}

FTPUploader クラス:

package restrictedareamanager;

import java.awt.HeadlessException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class FTPUploader extends SwingWorker <Object,Object> {

    private String servidor;
    private String usuario;
    private String senha;
    private String titulo;
    private String subtitulo;
    private File dest;

public FTPUploader (String servidor, String usuario, String senha, String titulo, String subtitulo, File dest) {

    this.servidor = servidor;
    this.usuario = usuario;
    this.senha = senha;
    this.titulo = titulo;
    this.subtitulo = subtitulo;
    this.dest = dest;

}   

@Override
protected Object doInBackground() throws Exception {

FTPClient ftp = new FTPClient ();

            try {  
             ftp.connect("servidor");  

             //verifica se conectou com sucesso!  
             if( FTPReply.isPositiveCompletion( ftp.getReplyCode() ) ) {  
                 ftp.login ("usuario", "senha");  
             } else {  
                 //erro ao se conectar  
                 ftp.disconnect();  
                 JOptionPane.showMessageDialog(null, "Ocorreu um erro ao se conectar com o servidor FTP", "Erro", JOptionPane.ERROR_MESSAGE);    
                 System.exit(1);  
             }  

                 ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
                 ftp.changeWorkingDirectory("/download");
                 ftp.changeWorkingDirectory(this.titulo);
                 ftp.changeWorkingDirectory (this.subtitulo);

                 ftp.storeFile (dest.getName(), new FileInputStream (dest.getPath().toString()));
                 System.out.println ("Done");


             ftp.logout();
             ftp.disconnect();  

             } catch( IOException | HeadlessException e ) {  
                 JOptionPane.showMessageDialog(null, "Ocorreu um erro ao enviar o arquivo.", "Erro", JOptionPane.ERROR_MESSAGE);  
                 System.exit(1);  
             }

   return null;
}

}
4

1 に答える 1

0

JSch ライブラリーにチェックインすることをお勧めします。これは Java での FTP および sFTP 接続のすべての醜さを処理します。

http://www.jcraft.com/jsch/

サンプルの実装は次のとおりです (ライブ プロダクション コードから)。

package com.somecompany.productfeed;

import net.snakedoc.jutils.ConfigException;

import org.apache.log4j.Logger;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

public class SFTP {

    // setup logger
    private static final Logger LOG = Logger.getLogger(SFTP.class.getName());

    private JSch jsch;
    private ChannelSftp sftpChannel;
    private Session session;

    private String username;
    private String password;
    private String host;
    private int port;

    // singleton
    private volatile static SFTP instance = null;

    /**
     * Private constructor used in Singleton pattern.
     *     All variables populated via properties file upon 
     *     object instantiation. Since we only need FTP to one server,
     *     object variables are immutable after instantiation.
     * 
     * @throws ConfigException
     * @throws NumberFormatException
     */
    private SFTP() throws ConfigException, NumberFormatException {

        this.jsch = new JSch();
        this.username = Config.getInstance().getConfig("SFTP_USER");
        this.password = Config.getInstance().getConfig("SFTP_PASS");
        this.host = Config.getInstance().getConfig("SFTP_HOST");
        this.port = Integer.parseInt(Config.getInstance().getConfig("SFTP_PORT"));

    }

    /**
     * Create or Return SFTP Object using Singleton pattern.
     * 
     * @return Singleton of SFTP Object.
     * @throws NumberFormatException
     * @throws ConfigException
     */
    public static SFTP getInstance() throws NumberFormatException, ConfigException {

        if (SFTP.instance == null) {

            synchronized (SFTP.class) {

                if (SFTP.instance == null) {

                    SFTP.instance = new SFTP();

                }

            }

        }

        return SFTP.instance;

    }

    /**
     * If connection is not already open/connected, open connection.
     * 
     * @throws JSchException
     */
    public void openConnection() throws JSchException {

        LOG.info("Opening SFTP Connection");
        if (null == getSession() || ! getSession().isConnected()) {

            setSession(jsch.getSession(this.username, this.host, this.port));
            getSession().setConfig("StrictHostKeyChecking", "no");
            getSession().setPassword(this.password);
            getSession().connect();

            Channel channel = getSession().openChannel("sftp");
            channel.connect();
            setSftpChannel((ChannelSftp) channel);

        } else {

            LOG.info("SFTP Connection already open");

        }

        LOG.debug("Success");

    }

    /**
     * Closes connection.
     */
    public void closeConnection() {

        LOG.info("Closing SFTP connection");
        getSftpChannel().exit();
        getSession().disconnect();
        LOG.debug("SFTP Connection closed");

    }

    /**
     * Checks if SFTP Connection is open.
     * 
     * @return TRUE if connection is open, FALSE if connection is closed
     */
    public boolean isOpen() {

        if (getSession().isConnected()) {

            return true;

        } else {

            return false;

        }

    }

    /**
     * Gets SFTP Channel
     * 
     * @return SFTP Channel (<code>ChannelSftp</code>)
     */
    private ChannelSftp getSftpChannel() {

        return this.sftpChannel;

    }

    /**
     * Returns current <code>Session</code>
     * 
     * @return <code>Session</code>
     */
    private Session getSession() {

        return this.session;

    } 

    /**
     * Sets SFTP Channel (<code>ChannelSftp</code>)
     * 
     * @param sftpChannel Channel to set this object's <code>ChannelSftp</code>
     */
    private void setSftpChannel(ChannelSftp sftpChannel) {

        this.sftpChannel = sftpChannel;

    }

    /**
     * Sets current <code>Session</code>
     * 
     * @param session Sets this object's <code>Session</code>
     */
    private void setSession(Session session) {

        this.session = session;

    }

    /**
     * Pushes local file to remote location
     * 
     * @param local String representation of filepath + filename (ex: /some_local_directory/somefile.txt)
     * @param remote String representation of filepath + filename (ex: /some_remote_directory/somefile.txt)
     * @throws SftpException
     */
    public void push(String local, String remote) throws SftpException {

        LOG.info("Sending file: " + local + " to remote: " + remote);
        getSftpChannel().put(local, remote);
        LOG.debug("Success");

    }

    /**
     * Gets remote file to local location
     * 
     * @param remote String representation of filepath + filename (ex: /some_remote_directory/somefile.txt)
     * @param local String representation of filepath + filename (ex: /some_local_directory/somefile.txt)
     * @throws SftpException
     */
    public void get(String remote, String local) throws SftpException {

        LOG.info("Retrieving file: " + remote + " saving to: " + local);
        getSftpChannel().get(remote, local);
        LOG.debug("Success");

    }

}
于 2013-09-19T18:41:14.230 に答える