package main;
import java.awt.Desktop;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.ActionEvent;
public class Download {
private JFrame frame;
private JTextField txtText;
private JTextField txtFolder;
private File textFile;
private File folder;
private StringBuilder links;
private JButton btnStart;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Throwable e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Download window = new Download();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Download() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 440, 162);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblLinkText = new JLabel("File text");
lblLinkText.setBounds(29, 34, 66, 14);
frame.getContentPane().add(lblLinkText);
JLabel lblFolderCha = new JLabel("Folder ch\u1EE9a");
lblFolderCha.setBounds(29, 59, 66, 14);
frame.getContentPane().add(lblFolderCha);
txtText = new JTextField();
txtText.setBounds(105, 32, 203, 20);
frame.getContentPane().add(txtText);
txtText.setColumns(10);
txtFolder = new JTextField();
txtFolder.setColumns(10);
txtFolder.setBounds(105, 56, 203, 20);
frame.getContentPane().add(txtFolder);
JButton btnText = new JButton("Ch\u1ECDn");
btnText.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser fC = new JFileChooser();
fC.setFileSelectionMode(JFileChooser.FILES_ONLY);
int option = fC.showOpenDialog(frame);
if (option == JFileChooser.APPROVE_OPTION) {
textFile = fC.getSelectedFile();
txtText.setText(textFile.getAbsolutePath());
}
}
});
btnText.setBounds(318, 30, 89, 23);
frame.getContentPane().add(btnText);
JButton btnFolder = new JButton("Ch\u1ECDn");
btnFolder.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fC = new JFileChooser();
fC.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int option = fC.showSaveDialog(frame);
if (option == JFileChooser.APPROVE_OPTION) {
folder = fC.getSelectedFile();
txtFolder.setText(folder.getAbsolutePath());
}
}
});
btnFolder.setBounds(318, 55, 89, 23);
frame.getContentPane().add(btnFolder);
btnStart = new JButton("B\u1EAFt \u0111\u1EA7u");
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try (BufferedReader inp = new BufferedReader(new FileReader(textFile))) {
String line;
links = new StringBuilder("");
while ((line = inp.readLine()) != null) {
links.append((line + "\n"));
}
new Thread(new Runnable() {
@Override
public void run() {
try {
download();
} catch (InterruptedException e) {
frame.dispose();
e.printStackTrace();
}
}
}).start();
btnStart.setEnabled(true);
} catch (IOException e1) {
System.out.println(e1);
frame.dispose();
}
}
});
btnStart.setBounds(161, 87, 89, 23);
frame.getContentPane().add(btnStart);
}
private void download() throws InterruptedException {
btnStart.setEnabled(false);
String fdPath = folder.getAbsolutePath();
String links = this.links.toString();
class DownThread extends Thread {
String linkStr;
public DownThread(String linkStr) {
this.linkStr = linkStr;
}
@Override
public void run() {
String[] links = this.linkStr.split("\n");
for (String link : links) {
try {
String[] parts = link.split("/");
download(link, fdPath + "/" + parts[parts.length - 1]);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
List<DownThread> dThreads = new ArrayList<>();
int end = 0;
int start = 0;
int lines = 0;
while (end != -1) {
end = links.indexOf("\n", end+1);
lines++;
if (lines == 8300 && dThreads.size()<=3) {
lines = 0;
if (end != -1) {
dThreads.add(new DownThread(links.substring(start, end)));
start = end + 1;
}
}
}
if (lines >0) {
dThreads.add(new DownThread(links.substring(start)));
}
try {
Desktop.getDesktop().open(folder);
} catch (IOException e) {
e.printStackTrace();
}
for (DownThread d : dThreads) {
d.start();
}
for (DownThread d : dThreads) {
d.join();
}
btnStart.setEnabled(true);
}
private void download(String url, String fileName) throws MalformedURLException, IOException {
URL website = new URL(url);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(fileName);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
}
}Add a code snippet to your website: www.paste.org