Olá amigos, segue uma classe que fiz para upload de arquivos...
package com.theosnet.upload;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class FtpUpload {
private FTPClient ftp;
public FtpUpload(){
super();
ftp = new FTPClient();
}
public void connect(String host, String user, String pass) throws SocketException, IOException{
ftp.connect( host );
ftp.login( user, pass );
}
public void disconnect(){
try{
ftp.logout();
ftp.disconnect();
}catch (IOException ex) {
ex.printStackTrace();
}
}
public String[] listFiles(String diretorio){
String[] arqList = {};
try{
ftp.changeWorkingDirectory (diretorio);
arqList = ftp.listNames();
}catch(IOException ex){
ex.printStackTrace();
}
return arqList;
}
public boolean uploadFile(File fl, String diretorio){
boolean retorno = false;
try{
if(fl.length() == 0){
throw new Exception("Arquivo não carregado.");
}
FileInputStream in = new FileInputStream(fl);
ftp.changeWorkingDirectory(diretorio);
retorno = (ftp.storeFile(fl.getName(), in));
}catch(Exception ex){
ex.printStackTrace();
}finally{
try{
disconnect();
}catch(Exception e){
e.printStackTrace();
}
}
return retorno;
}
}
Uso ela assim...
FtpUpload ftp = new FtpUpload();
try{
ftp.connect("ftp.com", "user", "senha");
txtLog.append("Conexão OK!");
this.getContentPane().repaint();
}catch( Exception e){
e.printStackTrace();
txtLog.append(e.getMessage());
this.getContentPane().repaint();
}
txtLog.append("Iniciando envio...");
this.getContentPane().repaint();
if (! edtFile.getText().equals("")){
File arquivo = new File(edtFile.getText());
if (ftp.uploadFile(arquivo,diretorio)) {
txtLog.append("Envio efetuado!");
this.getContentPane().repaint();
}else{
txtLog.append("Arquivo não foi transferido.");
this.getContentPane().repaint();
}
}else{
txtLog.append("É necessário escolher um arquivo.");
this.getContentPane().repaint();
}
como vocês podem ver, já fiz de tudo para poder ver algum LOG de o que está acontecendo na hora de upar o arquivo...
mas ...o problema é, a execuão parece que para na hora de conectar ao FTP... já fiz teste com a classe pelo JUnit...funciona 100%...
porém quando tento fazer upload pela applet não da erro...mas nem sobe o arquivo tbm...alguém pode me dar um help ?
desde já obrigado por toda ajuda!