bonjours,

je cherche depuis longtemps comment envoyer un fichier a partir d'un programme à un serveur ftp

voici le code

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package envoie;
 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JWindow;
import javax.swing.SwingConstants;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.io.CopyStreamListener;
 
@SuppressWarnings("unused")
public class Frame extends JFrame {
	String titre;
	String texte;
	public static final long serialVersionUID = 1L;
	public Frame () {
 
		this.setSize(1200, 700);
		this.setVisible(true);
		this.setResizable(false);
	    this.setLayout(null);
	    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
	    JTextArea text = new JTextArea();
		text.setBounds(20, 40, getWidth(), getHeight());		
		text.setSize(200,30);		
		text.setVisible(true);
		this.add(text);
 
		JLabel Titre = new JLabel("<html><body><u>Titre :</u></body></html>");
		Titre.setBounds(20, 15, getWidth(), getHeight());
		Titre.setVerticalAlignment(SwingConstants.TOP);
	    Titre.setVisible(true);	
		this.add(Titre);
 
		JLabel Text = new JLabel("<html><body><u>Texte :</u></body></html>");
		Text.setBounds(500, 15, getWidth(), getHeight());
		Text.setVerticalAlignment(SwingConstants.TOP);
		Text.setVisible(true);	
		this.add(Text); 
 
	 	JTextArea text1 = new JTextArea();	
	 	text1.setLineWrap(true);
		text1.setBounds(500, 40, 600, 300);					
		text1.setVisible(true);
		this.add(text1);
 
 
		JButton button = new JButton("Envoyer !");
		button.setBounds(35,320, 300, 300);	
		button.setVisible(true);	
		this.add(button);
		button.addActionListener(new ActionListener() {			
			public void actionPerformed(ActionEvent event) {
				titre = text1.getText();
				texte = text.getText();
				System.out.println("le titre est "+ titre);
				System.out.println("le texte est " + texte);
 
				File f = new File("D:\\" + texte + ".txt");
				try {
					boolean CreateNewFile = f.createNewFile();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					PrintWriter writer = new PrintWriter(f);
					writer.println(titre);
					writer.close();
 
 
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}	
 
				FTPClient dd = new FTPClient();
				try {
					dd.connect("192.168.1.31",21);
					int reponse = dd.getReplyCode();
 
					  if (!FTPReply.isPositiveCompletion(reponse)) 
						  System.out.println("sa a echouer");
 
					  boolean etat = dd.login("julien", "julien");
 
					  if (!etat)
						  System.out.println("sa na pas marcher");
					  else 
						  System.out.println("tout a bien marche");
 
 
			} catch (IOException e) {
				System.out.println("ya eu un bug dans la matrice !");
			}
 
 
 
 
 
			}
 
 
 
 
		});
	}
 
}
je réussi a me connecter a mon serveur mais pas a envoyer des fichier.

j'utilise la librairie d'apache.


merci.