Jelajahi Sumber

fix:调试完成,可以使用maven插件打包为.jar文件

yy 7 bulan lalu
induk
melakukan
1ef6315dea
3 mengubah file dengan 42 tambahan dan 5 penghapusan
  1. 31 0
      pom.xml
  2. 1 1
      readme.md
  3. 10 4
      src/main/java/space/anyi/autoConnectNetwork/Main.java

+ 31 - 0
pom.xml

@@ -48,4 +48,35 @@
         </dependency>
     </dependencies>
 
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-shade-plugin</artifactId>
+                <version>1.4</version>
+                <configuration>
+                    <createDependencyReducedPom>true</createDependencyReducedPom>
+                </configuration>
+                <executions>
+                    <execution>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>shade</goal>
+                        </goals>
+                        <configuration>
+                            <transformers>
+                                <transformer
+                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
+                                    <mainClass>space.anyi.autoConnectNetwork.Main</mainClass>
+                                </transformer>
+                            </transformers>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+
+        </plugins>
+    </build>
+
+
 </project>

+ 1 - 1
readme.md

@@ -2,7 +2,7 @@
 - 为了解决校园网被挤掉的问题,当没有网络连接时,自动打开登陆网页填写账号密码进行登陆
 - 使用selenium技术
 #### 使用方法
-- 运行`.jar`文件,携带四个参数
+- 运行`.jar`文件,携带四个参数`java -jar autoConnectNetwork-1.0-SNAPSHOT.jar "账号" "密码" "C:\Program Files\Mozilla Firefox\geckodriver.exe" "C:\Program Files\Mozilla Firefox\firefox.exe"`
   - 用户名
   - 账号
   - 浏览器驱动路径

+ 10 - 4
src/main/java/space/anyi/autoConnectNetwork/Main.java

@@ -8,6 +8,8 @@ import org.openqa.selenium.firefox.FirefoxOptions;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
 
 /**
  * @ProjectName: autoConnectNetwork
@@ -29,6 +31,8 @@ public class Main {
         String firefoxPath = args[3];
         System.setProperty("webdriver.gecko.driver",gecko);
         while (true){
+            System.out.println("判断网络是否连接");
+            System.out.println(LocalDateTime.now(ZoneId.systemDefault()));
             if (!connected()){
                 // 执行连接操作
                 System.out.println("执行连接操作");
@@ -42,6 +46,7 @@ public class Main {
     private static void connection(String username, String password,String firefoxPath) throws InterruptedException {
         FirefoxDriver firefoxDriver = null;
         try {
+            System.out.println("注册驱动");
             firefoxDriver = fireFoxDriverInit(firefoxPath);
             //打开网页
             firefoxDriver.get("http://10.30.252.71/eportal/index.jsp?nasip=cc5b64e516a1fa61d915e184b913e171");
@@ -55,7 +60,7 @@ public class Main {
             //选择登陆模式
             firefoxDriver.findElement(By.cssSelector("#selectDisname")).click();
             firefoxDriver.findElement(By.cssSelector("#_service_2")).click();
-            Thread.sleep(20*1000);
+            Thread.sleep(1000);
             //模拟点击登录
             firefoxDriver.findElement(By.cssSelector("#loginLink_div")).click();
         } finally {
@@ -63,6 +68,7 @@ public class Main {
             if (firefoxDriver != null) {
                 firefoxDriver.quit();
             }
+            System.out.println("释放资源");
         }
     }
 
@@ -72,7 +78,7 @@ public class Main {
 
         //设置参数
         //开启无头模式
-        //options.addArguments("--headless");
+        options.addArguments("--headless");
         //设置窗口大小
         options.addArguments("--window-size=1920,1080");
         //设置允许所有远程连接
@@ -123,7 +129,7 @@ public class Main {
             return false;
 
         BufferedReader bufferedReader = new BufferedReader(
-                new InputStreamReader(process.getInputStream(), "UTF-8"));// windows下编码默认是GBK,Linux是UTF-8
+                new InputStreamReader(process.getInputStream(), "GBK"));// windows下编码默认是GBK,Linux是UTF-8
 
         String line = null;
 
@@ -133,7 +139,7 @@ public class Main {
 
             if (line.startsWith("bytes from",3))
                 return true;
-            if (line.startsWith("from"))
+            if (line.startsWith("来自"))
                 return true;
         }