Olá pessoal, eu estou com um problemao q não consigo resolver “nem q a vaca tussa”, eu tenho já uma conexão ftp para envio de arquivos, eu tenho duas dúvidas q eu não consigo resolver. Eu quero criar pastas via FTP e ainda quero que quando eu selecionar os arquivos a serem enviados eu selecione vários e o jTextFild leia todos e não só o primeiro como está acontecendo, eu quero q ele importe todos os arquivos q eu selecionar, via FTP.
Eu já consigo enviar arquivos normalmente mas eu preciso disso que citei acima.
Ai vai o código
Desde já agradeço pessoal.
package ftp;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.apache.commons.net.ftp.FTPClient;
/**
*
* @author muryllo
*/
public final class EnvioFTP extends javax.swing.JFrame {
/** Creates new form EnvioFTP */
public EnvioFTP() {
initComponents();
bloquearCampos();
initFrame();
JOptionPane.showMessageDialog(null, "Verifique em 'Arquivos do Servidor FTP' se não há arquivo existente do qual deseja enviar para o servidor!");
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jButton1Importar = new javax.swing.JButton();
jButton1Buscar = new javax.swing.JButton();
jTextField1Arquivo = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Envio ao FTP");
jPanel1.setBackground(new java.awt.Color(255, 255, 255));
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
jLabel1.setText("Arquivo:");
jButton1Importar.setFont(new java.awt.Font("Tahoma", 1, 11));
jButton1Importar.setText("Importar");
jButton1Importar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ImportarActionPerformed(evt);
}
});
jButton1Buscar.setText("Buscar");
jButton1Buscar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1BuscarActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(4, 4, 4)
.addComponent(jTextField1Arquivo, javax.swing.GroupLayout.PREFERRED_SIZE, 347, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1Buscar))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(148, 148, 148)
.addComponent(jButton1Importar)))
.addContainerGap(22, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(29, 29, 29)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jButton1Buscar)
.addComponent(jTextField1Arquivo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
.addComponent(jButton1Importar)
.addGap(87, 87, 87))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
public void bloquearCampos(){
jTextField1Arquivo.setEnabled(true);
}
private void jButton1BuscarActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser abrir = new JFileChooser();
abrir.setMultiSelectionEnabled(true);
int retorno = abrir.showOpenDialog(null);
String caminho = null;
if (retorno == 0) {
caminho = abrir.getSelectedFile().getAbsolutePath();
jTextField1Arquivo.setText(caminho);
this.caminhoFTP = jTextField1Arquivo.getText();
}
}
private void jButton1ImportarActionPerformed(java.awt.event.ActionEvent evt) {
FTPClient ftp = new FTPClient();
try {
ftp.connect("ftp.com.br");
} catch (SocketException ex) {
Logger.getLogger(EnvioFTP.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(EnvioFTP.class.getName()).log(Level.SEVERE, null, ex);
}
try {
ftp.login("usuário", "senha");
} catch (IOException ex) {
Logger.getLogger(EnvioFTP.class.getName()).log(Level.SEVERE, null, ex);
}
try {
FileInputStream arqEnviar = new FileInputStream(jTextField1Arquivo.getText());
try {
String caminho = jTextField1Arquivo.getText();
System.out.println("Caminho original: " + caminho);
int indiceBarra = caminho.lastIndexOf("\\") + 1;
int tamanhoString = caminho.length();
String arq = caminho.substring(indiceBarra, tamanhoString);
System.out.println("Nome do arquivo: " + arq);
if (ftp.storeFile (arq, arqEnviar)){
JOptionPane.showMessageDialog(null, "Arquivo armazanado com sucesso!");
frame.dispose();
frame = new MyJFrame("Pastas e Arquivos Servidor FTP");
frame.setVisible(true);
} else
JOptionPane.showMessageDialog(null,"Erro ao armazenar o arquivo.");
} catch (IOException ex) {
Logger.getLogger(EnvioFTP.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(EnvioFTP.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, "Voce nao selecionou nenhum arquivo ou o arquivo está incorreto.");
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(EnvioFTP.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(EnvioFTP.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(EnvioFTP.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(EnvioFTP.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new EnvioFTP().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1Buscar;
private javax.swing.JButton jButton1Importar;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTextField1Arquivo;
// End of variables declaration
private String caminhoFTP;
private MyJFrame frame = new MyJFrame("Pastas e Arquivos Servidor FTP");
private void initFrame() {
frame.setVisible(true);
}
}
Eu tenho ainda a outra classe q gera a lista do servidor que e a seguinte
package ftp;
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.event.*;
import java.awt.*;
import java.io.IOException;
import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
class SimpleTree extends JPanel {
JTree tree;
DefaultMutableTreeNode root, node1;
public SimpleTree(){
FTPClient ftp = new FTPClient();
try {
ftp.connect("ftp.com.br");
} catch (SocketException ex) {
Logger.getLogger(EnvioFTP.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(EnvioFTP.class.getName()).log(Level.SEVERE, null, ex);
}
try {
ftp.login("usuário", "senha");
} catch (IOException ex) {
Logger.getLogger(EnvioFTP.class.getName()).log(Level.SEVERE, null, ex);
}
try {
FTPFile[] files = ftp.listFiles();
root = new DefaultMutableTreeNode("Pastas e Arquivos do Servidor", true);
for( int i=0; i<files.length; i++ ) {
System.out.println( files[i].getName());
node1 = new DefaultMutableTreeNode(files[i].getName(), true);
root.add(node1);
setLayout(new BorderLayout());
}
} catch (IOException ex) {
Logger.getLogger(SimpleTree.class.getName()).log(Level.SEVERE, null, ex);
}
UIManager.put("Tree.collapsedIcon", new ImageIcon("treePlus.gif"));
tree = new JTree(root);
add(new JScrollPane((JTree)tree), "Center");
}
public Dimension getPreferredSize(){
return new Dimension(200, 120);
} public static void main(String s[]) {
MyJFrame frame = new MyJFrame("Pastas e Arquivos Servidor FTP");
}
} class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent e) {
Window win = e.getWindow();
win.setVisible(false);
System.exit(0);
}
} class MyJFrame extends JFrame {
SimpleTree panel;
MyJFrame(String s){
super(s);
setForeground(Color.black);
setBackground(Color.lightGray);
panel = new SimpleTree();
getContentPane().add(panel,"Center");
setSize(300,300);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowCloser());
}
}
Tudo isso é netbeans.