日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

        歡迎訪問 生活随笔!

        生活随笔

        當前位置: 首頁 > 运维知识 > linux >内容正文

        linux

        java执行linux解压RAR命令程序阻塞/卡死

        發布時間:2023/12/20 linux 28 豆豆
        生活随笔 收集整理的這篇文章主要介紹了 java执行linux解压RAR命令程序阻塞/卡死 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

        1、問題描述

        公司項目有上傳2g壓縮包(zip、rar)功能,其中zip壓縮包功能直接使用linux命令解壓沒有問題,在RAR解壓時請求出現阻塞卡死的情況(直至情斷請求超時,請求才會失敗),查看服務器日志解壓命令如下:

        /usr/local/bin/unrar X -o+ /data/temp/upload/xxx/2021/06/01/xxx_1622544204569.rar /data/temp/upload/xxx/2021/06/01

        可以看出解壓命令是沒有錯誤的,于是去查看解壓命令是否執行成功,找到解壓目錄,文件已經解壓成功,如下圖:

        那么問題來了,為什么解壓成功后,程序卡死在解壓的代碼(Process proc = Runtime.getRuntime().exec(cmd))??解壓的方法如下:

        /*** 采用命令行方式解壓文件** @param zipFile 壓縮文件* @param destDir 解壓結果路徑* @return*/public static boolean realExtract(String zipFile, String destDir) {// 解決路徑中存在/..格式的路徑問題destDir = new File(destDir).getAbsoluteFile().getAbsolutePath();while (destDir.contains("..")) {String[] sepList = destDir.split("\\\\");destDir = "";for (int i = 0; i < sepList.length; i++) {if (!"..".equals(sepList[i]) && i < sepList.length - 1 && "..".equals(sepList[i + 1])) {i++;} else {destDir += sepList[i] + File.separator;}}}boolean bool = false;if (!FileUtil.exist(zipFile)) {return false;}if (!FileUtil.exist(destDir)) {FileUtil.mkdir(destDir);}//判斷系統類型 開始調用命令行解壓,參數-o+是表示覆蓋的意思String cmdPath = "";String osName = System.getProperty("os.name");if (osName.contains("Window")) {cmdPath = "C:\\Program Files\\WinRAR\\WinRAR.exe";} else {cmdPath = "/usr/local/bin/unrar";}String cmd = cmdPath + " X -o+ " + zipFile + " " + destDir;System.out.println(cmd);try {Process proc = Runtime.getRuntime().exec(cmd);if (proc.waitFor() != 0) {if (proc.exitValue() == 0) {bool = false;}} else {bool = true;}} catch (Exception e) {log.error("解壓失敗:" + e.getMessage(), e);e.printStackTrace();}System.out.println("解壓" + (bool ? "成功" : "失敗"));return bool;}

        問題分析

        1、排除服務器權限問題(zip文件可解壓成功,程序正常運行) 2、排除RAR解壓命令問題(壓縮包已經正常解壓) 3、那現在就只剩下代碼問題了由于系統是接手的項目,工具類在別的代碼中也有引用,就直接拿過來使用,理所當然的認為代碼沒問題,于是開始找自己的問題(服務器目錄權限、服務器上RAR版本等都沒找到),準備在服務器上生成程序的dump日志,在查看服務器進程時發現了新大陸,如下圖:![在這里插入圖片描述](https://img-blog.csdnimg.cn/2021060213295942.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d1aGFuY2hlbm1pbg==,size_16,color_FFFFFF,t_70#pic_center)

        這個是解壓RAR文件的進程,那問題可以確定了,也就是RAR解壓沒有關閉進程,那我看下JAVA執行linux命令方式:

        Process proc = Runtime.getRuntime().exec(cmd)

        百度下Process在線API:

        重點我們看下面的圖:

        看到這里馬上去對比代碼:


        這個缺少了殺死子進程的代碼,那我們把代碼修改下,下面是ZIP和RAR解壓的工具類:

        import cn.hutool.core.io.FileUtil; import lombok.extern.slf4j.Slf4j; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile;import java.io.*; import java.util.Enumeration;@Slf4j public class ZipAndRarTools {/*** 解壓Zip文件** @param zipFileName 需要解壓縮的文件位置* @param descFileName 將文件解壓到某個路徑* @param isDel 是否刪除壓縮包* @throws IOException*/public static void unZip(String zipFileName, String descFileName, boolean isDel) throws IOException {System.out.println("文件解壓開始...");String descFileNames = descFileName;if (!descFileNames.endsWith(File.separator)) {descFileNames = descFileNames + File.separator;}ZipFile zipFile = null;try {zipFile = new ZipFile(zipFileName);ZipEntry entry = null;String entryName = null;String descFileDir = null;byte[] buf = new byte[4096];int readByte = 0;@SuppressWarnings("rawtypes")Enumeration enums = zipFile.getEntries();while (enums.hasMoreElements()) {entry = (ZipEntry) enums.nextElement();entryName = entry.getName();descFileDir = descFileNames + entryName;if (entry.isDirectory()) {new File(descFileDir).mkdir();continue;} else {new File(descFileDir).getParentFile().mkdir();}File file = new File(descFileDir);OutputStream os = new FileOutputStream(file);InputStream is = zipFile.getInputStream(entry);while ((readByte = is.read(buf)) != -1) {os.write(buf, 0, readByte);}os.close();is.close();}System.out.println("文件解壓成功!");} catch (Exception e) {System.out.println("文件解壓失敗!");e.printStackTrace();} finally {zipFile.close();}//刪除壓縮包if (isDel) {FileUtil.del(zipFileName);}}/*** 采用命令行方式解壓文件** @param zipFile 壓縮文件* @param destDir 解壓結果路徑* @return*/public static boolean realExtract(String zipFile, String destDir) {// 解決路徑中存在/..格式的路徑問題destDir = new File(destDir).getAbsoluteFile().getAbsolutePath();while (destDir.contains("..")) {String[] sepList = destDir.split("\\\\");destDir = "";for (int i = 0; i < sepList.length; i++) {if (!"..".equals(sepList[i]) && i < sepList.length - 1 && "..".equals(sepList[i + 1])) {i++;} else {destDir += sepList[i] + File.separator;}}}boolean bool = false;if (!FileUtil.exist(zipFile)) {return false;}if (!FileUtil.exist(destDir)) {FileUtil.mkdir(destDir);}//判斷系統類型 開始調用命令行解壓,參數-o+是表示覆蓋的意思String cmdPath = "";String osName = System.getProperty("os.name");if (osName.contains("Window")) {cmdPath = "C:\\Program Files\\WinRAR\\WinRAR.exe";} else {cmdPath = "/usr/local/bin/unrar";}String cmd = cmdPath + " X -o+ " + zipFile + " " + destDir;System.out.println(cmd);try {File file = new File(zipFile);String s;Process proc = Runtime.getRuntime().exec(cmd);BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));while((s = bufferedReader.readLine()) != null) {// 輸出解壓日志log.info(file.getName() + "-->" + s);}if (proc.waitFor() != 0) {if (proc.exitValue() == 0) {bool = false;}} else {bool = true;}// 關閉進程proc.destroy();} catch (Exception e) {log.error("解壓失敗:" + e.getMessage(), e);e.printStackTrace();}System.out.println("解壓" + (bool ? "成功" : "失敗"));return bool;}}

        問題解決收工,記錄問題希望能幫到大家,如有錯誤請指出,謝謝

        總結

        以上是生活随笔為你收集整理的java执行linux解压RAR命令程序阻塞/卡死的全部內容,希望文章能夠幫你解決所遇到的問題。

        如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。