IDEA多執行緒檔案下載外掛開發的步驟詳解
上週使用Java開發了大檔案多執行緒下載工具類,自己平時的檔案下載也在使用這個工具,下載速度確實提升不少,但是每次下載都要去開啟專案執行程式碼,覺得實在不是很方便;考慮到每天我們都會使用到IDEA開發工具,所以就決定把這個下載工具做成IDEA的外掛,文章末尾附上外掛下載地址。
Java實現大檔案多執行緒下載
Gitee地址: https://gitee.com/silently9527/fast-download
IDEA多執行緒檔案下載外掛
Github地址: https://github.com/silently9527/FastDownloadIdeaPlugin
Gitee地址: https://gitee.com/silently9527/FastDownloadIdeaPlugin
不要忘記star喲
IDEA外掛介紹
IntelliJ IDEA是目前最好用的JAVA開發IDE,它本身的功能已經非常強大了,但是可能我們會遇到一些定製的需求,比如說:自定義程式碼生成器;這時候就需要我們自己動手來寫一個外掛,如果只是想要開發簡單的功能其實只要掌握了Java Swing,那麼開發IDEA的外掛是很容易的,如果想學習更多的原理和設計理念可以看 IntelliJ Platform SDK 的官方文件。
IDEA外掛開發步驟
1. 建立Gradle的外掛工程
建立完成專案之後,我們可以看一下 resource/META-INF/plugin.xml
<idea-plugin> <id>cn.silently9527.fast-download-idea-plugin</id> <!-- 外掛的ID --> <name>FastDownloadPlugin</name> <!-- 外掛的名字,會在外掛中心展示 --> <vendor email="[email protected]" url="https://silently9527">Silently9527</vendor> <!--外掛說明--> <description><![CDATA[ 多執行緒檔案下載器 ]]></description> <!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html on how to target different products --> <!-- uncomment to enable plugin in all products <depends>com.intellij.modules.lang</depends> --> <extensions defaultExtensionNs="com.intellij"> <!-- Add your extensions here --> </extensions> <actions> <!-- Add your actions here --> </actions> </idea-plugin>
2. 建立一個Action
在IDEA的外掛開發中,基本都會使用到Action,Action其實就是事件的處理器,就好比JS中的onClick方法。在IDEA中建立一個Action十分簡單,通過圖形化介面就可以完成
建立完成後就可以看到Action類
public class FastDownloadAction extends AnAction { @Override public void actionPerformed(AnActionEvent e) { } }
在 plugin.xml
中可以看到生成的Action資訊
<action id="fast.download" class="cn.silently9527.FastDownloadAction" text="FastDownload" description="檔案多執行緒下載"> <add-to-group group-id="ToolsMenu" anchor="last"/> <keyboard-shortcut keymap="$default" first-keystroke="shift D"/> </action>
3. 建立輸入下載資訊的彈窗
IDEA外掛的SDK已經對彈窗進行的封裝,只需要繼承 DialogWrapper
即可,介面上的繪製工作都在 createCenterPanel
方法中,元件的佈局與JavaSwing類似
@Nullable @Override protected JComponent createCenterPanel() { Box verticalBox = Box.createVerticalBox(); verticalBox.add(createUrlBox()); verticalBox.add(Box.createVerticalStrut(10)); verticalBox.add(createFileDirJPanel()); verticalBox.add(Box.createVerticalStrut(10)); verticalBox.add(createThreadNumJPanel()); return verticalBox; }
我們需要對輸入的下載地址和存放的路徑的引數進行校驗,判斷輸入是否正確,可以實現方法 doValidate
,校驗通過返回null,校驗不通過返回 ValidationInfo
物件
@Nullable @Override protected ValidationInfo doValidate() { if (StringUtils.isBlank(downloadUrlField.getText())) { return new ValidationInfo("檔案下載地址必填"); } if (StringUtils.isBlank(fileDirField.getText())) { return new ValidationInfo("檔案儲存目錄必填"); } if (StringUtils.isBlank(threadNumField.getText())) { return new ValidationInfo("下載執行緒數必填"); } return null; }
最終介面完成後的效果
4. 在FastDownloadAction中獲取彈窗輸入的下載資訊
DownloadDialog downloadDialog = new DownloadDialog(); if (downloadDialog.showAndGet()) { // 使用者點選OK之後進入到這裡 }
當用戶點選了OK,輸入資訊檢驗通過後我們就可以開始下載檔案了,由於之前做的下載元件是同步呼叫,為了不阻塞介面操作,需要使用執行緒非同步下載
CompletableFuture.runAsync(() -> { try { Downloader downloader = new MultiThreadFileDownloader(threadNum,downloadProgressPrinter); downloader.download(downloadURL,downloadDir); } catch (IOException e) { throw new RuntimeException(e); } })
在下載的過程中,需要給使用者反饋,讓使用者知道當前下載的進度是多少,以及當前下載的速度是多少
//使用SDK開啟一個後臺任務執行緒 ProgressManager.getInstance().run(new Task.Backgroundable(project,"File Downloading") { private long tmpAlreadyDownloadLength; //當前已下載位元組數 private long speed; //每秒下載速度 public void run(@NotNull ProgressIndicator progressIndicator) { // start your process while (true) { long alreadyDownloadLength = downloadProgressPrinter.getAlreadyDownloadLength(); long contentLength = downloadProgressPrinter.getContentLength(); if (alreadyDownloadLength != 0 && alreadyDownloadLength >= contentLength) { // 下載已完成,進度條顯示100% progressIndicator.setFraction(1.0); progressIndicator.setText("finished"); break; } setProgressIndicator(progressIndicator,contentLength,alreadyDownloadLength); sleep(); } } private void setProgressIndicator(ProgressIndicator progressIndicator,long contentLength,long alreadyDownloadLength) { if (alreadyDownloadLength == 0 || contentLength == 0) { return; } speed = alreadyDownloadLength - tmpAlreadyDownloadLength; tmpAlreadyDownloadLength = alreadyDownloadLength; double value = (double) alreadyDownloadLength / (double) contentLength; double fraction = Double.parseDouble(String.format("%.2f",value)); progressIndicator.setFraction(fraction); String text = "already download " + fraction * 100 + "%,speed: " + (speed / 1000) + "KB"; progressIndicator.setText(text); //進度條顯示已下載百分比,下載速度 } });
測試多執行緒下載檔案
測試下載820M的idea ,地址: https://download.jetbrains.8686c.com/idea/ideaIU-2020.3.dmg
外掛安裝
下載外掛之後,選擇本地安裝
總結
- IDEA外掛介紹
- IDEA外掛開發的基本步驟
- 實現了多執行緒檔案下載外掛
目前測試過程中發現檔案下載速度計算不太準確,個別執行緒的下載速度未能統計在內,後期繼續優化。
外掛下載連結:
https://pan.baidu.com/s/1MzYwk9tmuwA7DusN0r7n9Q 提取碼: fqr9
到此這篇關於IDEA多執行緒檔案下載外掛開發的文章就介紹到這了,更多相關IDEA多執行緒檔案下載外掛內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!