J'ai une applet qui peut lire l'adresse mac du client qui se connecte à ton application.
Je voudrai que tu me donnes la suite parce que j'ai du mal à le faire fonctionner.
import java.net.InetAddress;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.applet.Applet;
/**
* Classe permettant de récupérer l'adresse MAC d'un ordinateur.
*/
public class appletMac extends Applet{
/**
* Méthode Main de l'application
*
* @param args
*/
public static void main (String args []){
appletMac macAppl = new appletMac();
macAppl.init();
}
public void init() {
restart();
}
public void restart() {
try
{
System.out.println("Information Réseau Local");
System.out.println(" Système d'exploitation : " +System.getProperty("os.name"));
System.out.println(" IP/Localhost: " +InetAddress.getLocalHost().getHostName());
System.out.println(" IP/Localhost: " +InetAddress.getLocalHost().getHostAddress());
System.out.println(" IP/Localhost: " +InetAddress.getLocalHost().getLocalHost());
System.out.println(" Adresse MAC : " + getMacAddress());
}
catch (Throwable t)
{
t.printStackTrace();
}
}
/**
* Méthode invoquant le traitement à effectuer pour récupérer l'adresse MAC
* de l'ordinateur sur lequel on se trouve suivant le système
* d'exploitation.
*
* @return L'adresse MAC
*
* @throws IOException
*/
public static String getMacAddress() throws IOException {
String os = System.getProperty("os.name");
try{
if (os.startsWith("Windows")){
return windowsParseMacAddress(windowsRunIpConfigCommand());
}else{
throw new IOException("Système d'exploitation non supporté : " + os);
}
}catch (ParseException ex){
ex.printStackTrace();
throw new IOException(ex.getMessage());
}
}
// *************** Commande de récupération des informations réseau ***************//
/**
* Méthode récupérant les informations réseau sous Windows
* @return toutes les informations réseau
* @throws IOException
*/
private static String windowsRunIpConfigCommand() throws IOException {
Process p = Runtime.getRuntime().exec("ipconfig /all");
InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
StringBuffer buffer = new StringBuffer();
for (; ; ){
int c = stdoutStream.read();
if (c == -1)
break;
buffer.append( (char) c);
}
String outputText = buffer.toString();
stdoutStream.close();
return outputText;
}
private static String windowsParseMacAddress(String ipConfigResponse) throws ParseException
{
String localHost = null;
try
{
localHost = InetAddress.getLocalHost().getHostAddress();
}catch (java.net.UnknownHostException ex){
ex.printStackTrace();
throw new ParseException(ex.getMessage(), 0);
}
StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");
String lastMacAddress = null;
while (tokenizer.hasMoreTokens()){
String line = tokenizer.nextToken().trim();
// see if line contains IP address
if (line.endsWith(localHost) && lastMacAddress != null){
return lastMacAddress;
}
// see if line contains MAC address
int macAddressPosition = line.indexOf(":");
if (macAddressPosition <= 0)
continue;
String macAddressCandidate = line.substring(macAddressPosition + 1).trim();
if (windowsIsMacAddress(macAddressCandidate)){
lastMacAddress = macAddressCandidate;
continue;
}
}
ParseException ex = new ParseException("cannot read MAC address from [" +ipConfigResponse + "]", 0);
ex.printStackTrace();
throw ex;
}
private static boolean windowsIsMacAddress(String macAddressCandidate)
{
Pattern macPattern = Pattern
.compile("[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}");
Matcher m = macPattern.matcher(macAddressCandidate);
return m.matches();
}
}
Si oui comment l'utiliser ?
Merci