Abstract
Last week, I used Java to develop a large file multithreaded download tool class. I am also using this tool for my usual file download. The download speed has really improved a lot, but every time I download, I have to open the project and run the code, which is not very convenient. Considering that we use IDEA development tool every day, we decided to make this download tool into a plug-in for IDEA. The download address of the plug-in is attached at the end of the article.
❝
Java multithreaded download of large files
- Gitee address: gitee.com/silently952…
IDEA multithreaded file download plug-in
- Github address: github.com/silently952…
- Gitee address: gitee.com/silently952…
Don’t forget Star
❞
Introduction to IDEA Plug-in
IntelliJ IDEA is currently the best JAVA development IDE, which is already very powerful, but we may encounter some custom requirements, such as: custom code generator; At this time, we need to write a plug-in by ourselves. If we just want to develop simple functions, as long as we master Java Swing, it is easy to develop plug-ins for IDEA. If you want to learn more principles and design concepts, you can see the official documents of the IntelliJ Platform SDK.
IDEA plug-in development steps
1. Create a Gradle plugin project
After creating the project, we can look at resource/ meta-INF /plugin.xml
<idea-plugin>
<id>cn.silently9527.fast-download-idea-plugin</id> <! -- Plugin ID -->
<name>FastDownloadPlugin</name> <! -- The name of the plugin is displayed in the plugin center -->
<vendor email="[email protected]" url="https://silently9527">Silently9527</vendor>
<! -- Plugin description -->
<description><! [CDATA[
Multithreaded file downloader
]]></description>
<! -- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
on how to target different products -->
<! -- uncomment toenable 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>
Copy the code
2. Create an Action
In the plug-in development of IDEA, Action is basically used. Action is the handler of the event, just like the onClick method in JS. Creating an Action in IDEA is simple and can be done using a graphical interface
Once created, you can see the Action class
public class FastDownloadAction extends AnAction {
@Override
public void actionPerformed(AnActionEvent e) {
}
}
Copy the code
You can see the generated Action information in plugin.xml
<action id="fast.download" class="cn.silently9527.FastDownloadAction" text="FastDownload" description="Multithreaded file download">
<add-to-group group-id="ToolsMenu" anchor="last"/>
<keyboard-shortcut keymap="$default" first-keystroke="shift D"/>
</action>
Copy the code
3. Create a popover to enter download information
The SDK of IDEA plug-in has already packaged popover and only needs to inherit DialogWrapper. The drawing work on the interface is in createCenterPanel method, and the layout of components is similar to that of 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;
}
Copy the code
We need to verify the input parameters of the download address and store path to determine whether the input is correct. We can implement the method doValidate. Null is returned after verification, and ValidationInfo object is returned after verification
@Nullable
@Override
protected ValidationInfo doValidate() {
if (StringUtils.isBlank(downloadUrlField.getText())) {
return new ValidationInfo("File download address required");
}
if (StringUtils.isBlank(fileDirField.getText())) {
return new ValidationInfo("File save directory required");
}
if (StringUtils.isBlank(threadNumField.getText())) {
return new ValidationInfo("Number of download threads required");
}
return null;
}
Copy the code
The final interface after the completion of the effect
4. Obtain the download information that pops up in FastDownloadAction
DownloadDialog downloadDialog = new DownloadDialog();
if (downloadDialog.showAndGet()) {
// The user clicks OK to enter here
}
Copy the code
When the user clicks OK and the input information is verified, we can start to download the file. Since the download component is called synchronously before, in order not to block the interface operation, we need to use thread asynchronous download
CompletableFuture.runAsync(() -> {
try {
Downloader downloader = new MultiThreadFileDownloader(threadNum, downloadProgressPrinter);
downloader.download(downloadURL, downloadDir);
} catch (IOException e) {
throw new RuntimeException(e);
}
})
Copy the code
During the download process, feedback needs to be given to the user to let the user know how far the download is currently progressing and how fast it is currently downloading
// Start a background task thread using SDK
ProgressManager.getInstance().run(new Task.Backgroundable(project, "File Downloading") {
private long tmpAlreadyDownloadLength; // Number of bytes downloaded
private long speed; // Download speed per second
public void run(@NotNull ProgressIndicator progressIndicator) {
// start your process
while (true) {
long alreadyDownloadLength = downloadProgressPrinter.getAlreadyDownloadLength();
long contentLength = downloadProgressPrinter.getContentLength();
if(alreadyDownloadLength ! = 0 && alreadyDownloadLength >= contentLength) {
// Download complete, progress bar showing 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); // The progress bar shows the percentage of downloads and download speed
}
});
Copy the code
Test multithreaded download files
Test download 820 m’s idea, address: https://download.jetbrains.8686c.com/idea/ideaIU-2020.3.dmg
Plug-in installation
After downloading the plug-in, choose local installation
conclusion
- Introduction to IDEA Plug-in
- Basic steps of IDEA plug-in development
- Multithreaded file download plug-in
❝
During the current test, it was found that the calculation of file download speed was not accurate, and the download speed of individual threads was not counted, so the optimization was continued in the later stage.
The plug-in download links: pan.baidu.com/s/1cmzKgmu8… Extraction code: 3F4T
❞
To the last point of attention, do not get lost
❝
There may be more or less deficiencies and mistakes in the article, suggestions or opinions are welcome to comment and exchange.
Finally, “creation is not easy, please don’t white whoring”, I hope friends can “like comment attention” three even, because these are all the power source I share 🙏
❞
❝
Wechat official account: Beta learning JAVA
❞