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
| import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class BrowserControl
{
// Fonction qui affiche une URL sur Windows ou UNIX
public static void displayURL(String url)
{
boolean windows = isWindowsPlatform();
String cmd = null;
try
{
if (windows)
{
// cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
Process p = Runtime.getRuntime().exec(cmd);
}
else
{
// Under Unix, Netscape has to be running for the "-remote"
// command to work. So, we try sending the command and
// check for an exit value. If the exit command is 0,
// it worked, otherwise we need to start the browser.
// cmd = 'netscape -remote openURL(http://www.javaworld.com)'
cmd = UNIX_PATH + " " + UNIX_FLAG + "(" + url + ")";
Process p = Runtime.getRuntime().exec(cmd);
try
{
// wait for exit code -- if it's 0, command worked,
// otherwise we need to start the browser up.
int exitCode = p.waitFor();
if (exitCode != 0)
{
// Command failed, start up the browser
// cmd = 'netscape http://www.javaworld.com'
cmd = UNIX_PATH + " " + url;
p = Runtime.getRuntime().exec(cmd);
}
}
catch(InterruptedException x)
{
System.err.println("Error bringing up browser, cmd='" +
cmd + "'");
System.err.println("Caught: " + x);
}
}
}
catch(IOException x)
{
// couldn't exec browser
System.err.println("Could not invoke browser, command=" + cmd);
System.err.println("Caught: " + x);
}
}
// Attributs
public String pathFolder = "C:\\files\\"; // Stocke le chemin d'accès au dossier du fichier
public static String urlName=""; // Stocke le nom de l'URL en cours d'affichage
// Méthode qui détermine si l'application est lançé sur un OS un windows ou pas
public static boolean isWindowsPlatform()
{
String os = System.getProperty("os.name");
if ( os != null && os.startsWith(WIN_ID))
return true;
else
return false;
}
// Fonction permettant de lire un fichier ligne par ligne et d'afficher les URL contenant http ou https
public static void openURLForAllLine(File fileForFileName){
// On crée le
// Lire un fichier ligne par ligne
try{
// Création du flux bufférisé sur un FileReader, immédiatement suivi par un
// try/finally, ce qui permet de ne fermer le flux QUE s'il le reader
// est correctement instancié (évite les NullPointerException)
BufferedReader buff = new BufferedReader(new FileReader(fileForFileName));
try {
String line;
// Lecture du fichier ligne par ligne. Cette boucle se termine
// quand la méthode retourne la valeur null.
while ((line = buff.readLine()) != null) {
// Condition si http ou https présent comme occurence dans la ligne en cours
if (line.contains("http") && line.contains("https")) {
// on stocke la ligne dans la variable urlName
urlName=line;
// On affiche l'URL dans le navigateur
displayURL(urlName);
}
}
} finally {
// dans tous les cas, on ferme nos flux
buff.close();
}
} catch (IOException ioe) {
// erreur de fermeture des flux
System.out.println("Erreur --" + ioe.toString());
}
}
/**
* Main principale :
* Lancement du programme
* On récupère le dossier que l'utilisateur aura spécifié
* On récupère le nom du fichier que l'utilisateur aura spécifié
* On appelle la fonction qui ouvre les URL
*/
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
// Demander le dossier du fichier (se termine par un \)
System.out.println("Entrer le nom du dossier où se situe le fichier avec un \\ en fin");
String pathFolder = sc.nextLine();
// Demander le nom du fichier avec son extension
System.out.println("Entrer le nom du fichier avec son extension");
String fileName = sc.nextLine();
File pathFile= new File(pathFolder+fileName); // Stocke le chemin d'accès au fichier
openURLForAllLine(pathFile);
}
// Used to identify the windows platform.
private static final String WIN_ID = "Windows";
// The default system browser under windows.
private static final String WIN_PATH = "rundll32";
// The flag to display a url.
private static final String WIN_FLAG = "url.dll,FileProtocolHandler";
// The default browser under unix.
private static final String UNIX_PATH = "netscape";
// The flag to display a url.
private static final String UNIX_FLAG = "-remote openURL";
} |
Partager