0

Suppose I have a JList, with the following code:

final JList<String> list = new JList<String>(model);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setToolTipText("Choose Version");
list.setBounds(10, 150, 414, 23);

I want to make a feature where the user can select a file from this JList that is hosted on a website, and then download it. For the JList names, I will use the names of the files being hosted. (I want to do this by using JSoup)

So far, I have the following code to display the names:

DefaultListModel<String> model = new DefaultListModel<String>();

        Document doc = null;

        try {
            doc = Jsoup.connect("https://mega.nz/fm/rqwQRKyR").get();
        } catch (IOException e3) {
            // TODO Auto-generated catch block
            e3.printStackTrace();
        }
        for (Element file : doc.select("td.right td a")) {
            model.addElement(file.attr("href"));
        }

In this example code, I get the file names from a website and add the names of the files to the DefaultListModel. Then, I create a new JList with this model.

However, how would I be able to make my program able to download the specific file that was chosen (based on the file name) using JSoup?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Brandon Li
  • 55
  • 8

1 Answers1

1

Well, after reading this Q&A on how to download images using JSoup, I ended up with the code below. By the time you did not specify if you want the images to be downloaded simply in memory or to be saved in a file locally, I implemented the code such that it does both (i.e. it first downloads and then prompts the user to save it if he/she wants).

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Window;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Objects;
import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import org.jsoup.Jsoup;

public class Main {

    //A JDialog which allows the user to save the downloaded image locally...
    private static class ImageDialog extends JDialog {
        private final BufferedImage bimg;

        private ImageDialog(final Window owner,
                            final String title,
                            final JFileChooser sharedChooser,
                            final BufferedImage bimg) {
            super(owner, title, ModalityType.APPLICATION_MODAL);

            this.bimg = Objects.requireNonNull(bimg);

            final JLabel label = new JLabel(new ImageIcon(bimg), JLabel.CENTER);

            final JScrollPane scroll = new JScrollPane(label);
            scroll.setPreferredSize(new Dimension(600, 600));

            final JList<String> options = new JList<>(ImageIO.getWriterFormatNames());
            options.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

            final JButton save = new JButton("Save");
            save.addActionListener(e -> {
                if (JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog(ImageDialog.this, options, "Select image type", JOptionPane.OK_CANCEL_OPTION)) {
                    final String imageFormat = options.getSelectedValue();
                    if (imageFormat == null)
                        JOptionPane.showMessageDialog(ImageDialog.this, "You have to select an image type first!");
                    else if (sharedChooser.showSaveDialog(ImageDialog.this) == JFileChooser.APPROVE_OPTION) {
                        final File imagePath = sharedChooser.getSelectedFile();
                        if (imagePath.exists()) //I would avoid overwriting files.
                            JOptionPane.showMessageDialog(ImageDialog.this, "You cannot overwrite images! Delete them manually first.", "Oups!", JOptionPane.WARNING_MESSAGE);
                        else {
                            try {
                                ImageIO.write(bimg, imageFormat, imagePath);
                                JOptionPane.showMessageDialog(ImageDialog.this, "Saved image successfully under \"" + imagePath + "\".", "Ok", JOptionPane.INFORMATION_MESSAGE);
                            }
                            catch (final IOException iox) {
                                iox.printStackTrace();
                                JOptionPane.showMessageDialog(ImageDialog.this, "Failed to save image! " + iox, "Error!", JOptionPane.ERROR_MESSAGE);
                            }
                        }
                    }
                }
            });

            final JPanel contents = new JPanel(new BorderLayout());
            contents.add(scroll, BorderLayout.CENTER);
            contents.add(save, BorderLayout.PAGE_END);

            super.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            super.getContentPane().add(contents);
            super.pack();
            super.setLocationRelativeTo(owner);
        }
    }

    public static void main(final String[] args) {

        //This is where you fill the model:
        final DefaultListModel<String> model = new DefaultListModel<>();
        model.addElement("https://i.stack.imgur.com/PUMHY.png");
        model.addElement("https://i.stack.imgur.com/PUMHY.png");
        model.addElement("https://i.stack.imgur.com/PUMHY.png");
        model.addElement("https://i.stack.imgur.com/PUMHY.png");

        //List creation and initialization:
        final JList<String> list = new JList<>(model);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        //JScrollPane for list:
        final JScrollPane scroll = new JScrollPane(list);
        scroll.setPreferredSize(new Dimension(400, 400));

        final JFileChooser sharedChooser = new JFileChooser();
        sharedChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        sharedChooser.setMultiSelectionEnabled(false);

        final JProgressBar bar = new JProgressBar();
        bar.setBorderPainted(false);
        bar.setStringPainted(true);
        bar.setString("Downloading, please wait...");
        bar.setIndeterminate(true);

        //Create an extra panel which will have the progress bar centered in it...
        final JPanel centeredBar = new JPanel(new GridBagLayout());
        centeredBar.add(bar);

        final JButton button = new JButton("Download");

        final JPanel contents = new JPanel(new BorderLayout());
        contents.add(scroll, BorderLayout.CENTER);
        contents.add(button, BorderLayout.PAGE_END);

        //Creating the cards to add in the content pane of the jframe:
        final String cardProgressBar = "PROGRESS_BAR",
                     cardList = "LIST";
        final CardLayout cardl = new CardLayout();
        final JPanel cards = new JPanel(cardl);
        cards.add(contents, cardList);
        cards.add(centeredBar, cardProgressBar);
        cardl.show(cards, cardList);

        final JFrame frame = new JFrame("JList for JSoup");

        button.addActionListener(e -> {
            cardl.show(cards, cardProgressBar);
            new Thread(() -> {
                final String imagePath = list.getSelectedValue();
                if (imagePath == null)
                    JOptionPane.showMessageDialog(frame, "No image selected!");
                else {
                    try {
                        //This is the actual downloading you requested:
                        try (final BufferedInputStream bis = Jsoup.connect(imagePath).ignoreContentType(true).execute().bodyStream()) {
                            final BufferedImage bimg = ImageIO.read(bis);
                            new ImageDialog(frame, imagePath, sharedChooser, bimg).setVisible(true);
                        }
                    }
                    catch (final IOException iox) {
                        iox.printStackTrace();
                        JOptionPane.showMessageDialog(frame, "Failed to download \"" + imagePath + "\"! " + iox);
                    }
                    finally {
                        cardl.show(cards, cardList);
                    }
                }
            }).start();
        });

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(cards);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

You have to fill your list model with how you showed in your own code. In my example code above, I just fill it with some random value many times.

You can also replace the call Jsoup.connect(imagePath).ignoreContentType(true).execute().bodyStream() with Jsoup.connect(imagePath).ignoreContentType(true).execute().bodyAsBytes() which will tolerate files up to 2GB (because I guess you cannot allocate byte arrays larger than Integer.MAX_VALUE in Java). Let me know if this is what you were looking for.

gthanop
  • 3,035
  • 2
  • 10
  • 27