关于idea:IDEA插件多线程文件下载插件开发

1次阅读

共计 4597 个字符,预计需要花费 12 分钟才能阅读完成。

摘要

上周应用 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="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>

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.86…

插件装置

下载插件之后,抉择本地装置

总结

  • IDEA 插件介绍
  • IDEA 插件开发的根本步骤
  • 实现了多线程文件下载插件

目前测试过程中发现文件下载速度计算不太精确,个别线程的下载速度未能统计在内,前期持续优化。

插件下载链接: https://pan.baidu.com/s/1cmzKgmu8JwUa-liWmNl5jw 提取码: 3f4t

写到最初 点关注,不迷路

文中或者会存在或多或少的有余、谬误之处,有倡议或者意见也十分欢送大家在评论交换。

最初,创作不易,请不要白嫖 ,心愿敌人们能够 点赞评论关注 三连,因为这些就是我分享的全副能源起源????

微信公众号:贝塔学 JAVA

原文地址:https://silently9527.cn/archives/93

正文完
 0