您好,登錄后才能下訂單哦!
這篇文章給大家介紹使用IDEA怎么在多線程中開發(fā)一個(gè)文件下載插件,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
IDEA插件介紹
IntelliJ IDEA是目前最好用的JAVA開發(fā)IDE,它本身的功能已經(jīng)非常強(qiáng)大了,但是可能我們會(huì)遇到一些定制的需求,比如說:自定義代碼生成器;這時(shí)候就需要我們自己動(dòng)手來寫一個(gè)插件,如果只是想要開發(fā)簡單的功能其實(shí)只要掌握了Java Swing,那么開發(fā)IDEA的插件是很容易的,如果想學(xué)習(xí)更多的原理和設(shè)計(jì)理念可以看 IntelliJ Platform SDK 的官方文檔。
創(chuàng)建完成項(xiàng)目之后,我們可以看一下 resource/META-INF/plugin.xml
<idea-plugin> <id>cn.silently9527.fast-download-idea-plugin</id> <!-- 插件的ID --> <name>FastDownloadPlugin</name> <!-- 插件的名字,會(huì)在插件中心展示 --> <vendor email="380303318@qq.com" 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>
在IDEA的插件開發(fā)中,基本都會(huì)使用到Action,Action其實(shí)就是事件的處理器,就好比JS中的onClick方法。在IDEA中創(chuàng)建一個(gè)Action十分簡單,通過圖形化界面就可以完成
創(chuàng)建完成后就可以看到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>
IDEA插件的SDK已經(jīng)對(duì)彈窗進(jìn)行的封裝,只需要繼承 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; }
我們需要對(duì)輸入的下載地址和存放的路徑的參數(shù)進(jìn)行校驗(yàn),判斷輸入是否正確,可以實(shí)現(xiàn)方法 doValidate
,校驗(yàn)通過返回null,校驗(yàn)不通過返回 ValidationInfo
對(duì)象
@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("下載線程數(shù)必填"); } return null; }
最終界面完成后的效果
DownloadDialog downloadDialog = new DownloadDialog(); if (downloadDialog.showAndGet()) { // 用戶點(diǎn)擊OK之后進(jìn)入到這里 }
當(dāng)用戶點(diǎn)擊了OK,輸入信息檢驗(yàn)通過后我們就可以開始下載文件了,由于之前做的下載組件是同步調(diào)用,為了不阻塞界面操作,需要使用線程異步下載
CompletableFuture.runAsync(() -> { try { Downloader downloader = new MultiThreadFileDownloader(threadNum, downloadProgressPrinter); downloader.download(downloadURL, downloadDir); } catch (IOException e) { throw new RuntimeException(e); } })
在下載的過程中,需要給用戶反饋,讓用戶知道當(dāng)前下載的進(jìn)度是多少,以及當(dāng)前下載的速度是多少
//使用SDK開啟一個(gè)后臺(tái)任務(wù)線程 ProgressManager.getInstance().run(new Task.Backgroundable(project, "File Downloading") { private long tmpAlreadyDownloadLength; //當(dāng)前已下載字節(jié)數(shù) 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) { // 下載已完成,進(jìn)度條顯示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); //進(jìn)度條顯示已下載百分比,下載速度 } });
測試下載820M的idea ,地址: https://download.jetbrains.8686c.com/idea/ideaIU-2020.3.dmg
下載插件之后,選擇本地安裝
關(guān)于使用IDEA怎么在多線程中開發(fā)一個(gè)文件下載插件就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。