摘要
上周应用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@Overrideprotected 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@Overrideprotected 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