problème avec mon code pour accès au serveur ftp
Bonjour, j'ai un problème avec mon code, pourriez-vous le tester s'il vous plaît ?
Merci déjà;
voici mon 1er fichier (c'est un jFrame)
Code:
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 127 128 129 130 131 132 133 134 135 136 137 138 139
|
/*
* FtpWrapperTest.java
*
* Created on 29 février 2008, 09:29
*/
package testftp;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.lang.*;
import testftp.SunFtpWrapper;
/**
*
* @author GRESLON Jérémy
*/
public class FtpWrapperTest extends javax.swing.JFrame {
/** Creates new form FtpWrapperTest */
public FtpWrapperTest() {
initComponents();
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanelPhoto = new javax.swing.JPanel();
jButtonBadge = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanelPhoto.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
org.jdesktop.layout.GroupLayout jPanelPhotoLayout = new org.jdesktop.layout.GroupLayout(jPanelPhoto);
jPanelPhoto.setLayout(jPanelPhotoLayout);
jPanelPhotoLayout.setHorizontalGroup(
jPanelPhotoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 210, Short.MAX_VALUE)
);
jPanelPhotoLayout.setVerticalGroup(
jPanelPhotoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 216, Short.MAX_VALUE)
);
jButtonBadge.setText("Passage du badge");
jButtonBadge.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonBadgeActionPerformed(evt);
}
});
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(83, 83, 83)
.add(jPanelPhoto, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.add(133, 133, 133)
.add(jButtonBadge)))
.addContainerGap(103, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(34, 34, 34)
.add(jPanelPhoto, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(jButtonBadge)
.addContainerGap(12, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButtonBadgeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
SunFtpWrapper ftp = new SunFtpWrapper();
String serverName = "127.0.0.1";
ftp.openServer(serverName);
if (ftp.serverIsOpen()) {
System.out.println("Connecte à " + serverName);
try {
ftp.login("GARDIEN", "admin");
System.out.println("Message de bienvenue:\n" + ftp.welcomeMsg);
System.out.println("Répertoire courrant: " + ftp.pwd());
System.out.println("Listage du répértoire:\n" + ftp.listRaw());
System.out.println("Téléchargement de bmw.jpg");
ftp.ascii();
ftp.downloadFile("25.jpg", "C:\\Documents and Settings\\Jérémy GRESLON\\Mes documents\\Photos_employes\\25.jpg");
} catch (Exception ftpe) {
ftpe.printStackTrace();
} finally {
ImageIcon icone = new ImageIcon("25.jpg");
JLabel image = new JLabel(icone);
image.setSize(jPanelPhoto.getWidth(), jPanelPhoto.getHeight());
jPanelPhoto.add(image);
jPanelPhoto.repaint();
ftp.closeServer();
}
} else {
System.out.println("Impossible de se connecter à" + serverName);
}
System.out.println("Fin");
} catch(Exception e) {
e.printStackTrace();
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FtpWrapperTest().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButtonBadge;
private javax.swing.JPanel jPanelPhoto;
// End of variables declaration
} |
et mon 2ème fichier (.java)
Code:
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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
/* <!-- in case someone opens this in a browser... --> <pre> */
package testftp;
import java.util.Vector;
import java.io.*;
import sun.net.ftp.FtpClient;
/**
* This is a basic wrapper around the sun.net.ftp.FtpClient
* class, which is an undocumented class which is included
* with Sun Java that allows you to make FTP connections
* and file transfers.
* <p>
* Program version 1.0. Author Julian Robichaux, http://www.nsftools.com
*
* @author Julian Robichaux ( http://www.nsftools.com )
* @version 1.0
*/
public class SunFtpWrapper extends FtpClient {
/*
* Methods you might use from the base FtpClient class
* // set the transfer type to ascii
* public void ascii()
*
* // set the transfer type to binary
* public void binary()
*
* // change to the specified directory
* public void cd(String remoteDirectory)
*
* // close the connection to the server
* public void closeServer()
*
* // download the specified file from the FTP server
* public TelnetInputStream get(String filename)
*
* // return the last response from the server as a single String
* public String getResponseString()
*
* // return the last response from the server as a Vector of Strings
* public Vector getResponseStrings()
*
* // list the contents of the current directory (could be in any number
* // of formats, depending on the remote server)
* public TelnetInputStream list()
*
* // login to the FTP server, using the specified username and password
* public void login(String username, String password)
*
* // open a connection to the specified FTP server, using port 21
* public void openServer(String host)
*
* // open a connection to the specified FTP server, using the specified port
* public void openServer(String host, int port))
*
* // upload a file to the FTP server (the uploaded file will have the specified
* // file name
* public TelnetOutputStream put(String filename)
*/
/** Get the present working directory */
public String pwd() throws IOException {
issueCommand("PWD");
if (isValidResponse()) {
String response = getResponseString().substring(4).trim();
if (response.startsWith("\""))
response = response.substring(1);
if (response.endsWith("\""))
response = response.substring(0, response.length() - 1);
return response;
} else {
return "";
}
}
/** Go up one directory */
public boolean cdup() throws IOException {
issueCommand("CDUP");
return isValidResponse();
}
/** Create a new directory */
public boolean mkdir (String newDir) throws IOException {
issueCommand("MKDIR " + newDir);
return isValidResponse();
}
/** Delete a remote file */
public boolean deleteFile (String fileName) throws IOException {
issueCommand("DELETE " + fileName);
return isValidResponse();
}
/** Get the results of the LIST command as a Vector of Strings.
* Because there's no standard format for the results of a LIST
* command, it's hard to tell what resulting data will look like.
* Just be aware that different servers have different ways of
* returning your LIST data. */
public Vector listRaw() throws IOException {
String fileName;
Vector ftpList = new Vector();
BufferedReader reader = new BufferedReader(new InputStreamReader(list()));
while ((fileName = reader.readLine()) != null) {
ftpList.add(fileName);
}
return ftpList;
}
/** Get the response code from the last command that was sent */
public int getResponseCode() throws NumberFormatException {
return Integer.parseInt(getResponseString().substring(0, 3));
}
/** Return true if the last response code was in the 200 range,
* false otherwise */
public boolean isValidResponse() {
try {
int respCode = getResponseCode();
return (respCode >= 200 && respCode < 300);
} catch (Exception e) {
return false;
}
}
/** Send a raw FTP command to the server. You can get the response
* by calling getResponseString (which returns the entire response as a
* single String) or getResponseStrings (which returns the response
* as a Vector). */
public int issueRawCommand (String command) throws IOException {
return issueCommand(command);
}
/** Download a file from the server, and save it to the specified local file */
public boolean downloadFile (String serverFile, String localFile) throws IOException {
int i = 0;
byte[] bytesIn = new byte[1024];
BufferedInputStream in = new BufferedInputStream(get(serverFile));
FileOutputStream out = new FileOutputStream(localFile);
while ((i = in.read(bytesIn)) >= 0) {
out.write(bytesIn, 0, i);
}
out.close();
return true;
}
/** Upload a file to the server */
public boolean uploadFile (String localFile, String serverFile) throws IOException {
int i = 0;
byte[] bytesIn = new byte[1024];
FileInputStream in = new FileInputStream(localFile);
BufferedOutputStream out = new BufferedOutputStream(put(serverFile));
while ((i = in.read(bytesIn)) >= 0) {
out.write(bytesIn, 0, i);
}
in.close();
out.close();
return true;
}
} |