⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 batchdownload.java

📁 A java GUI interface program used to download a batch of files in a specified URL, or a kind of simi
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            if (includeSub)            {                String localFile = strUrl.substring(strUrl.lastIndexOf('/') + 1);                String urlPath = strUrl.substring(0, strUrl.lastIndexOf('/') + 1);                if (localFile.equals(""))                    localFile = "index.html";                Vector<String> fileList = getFileList(path + localFile);                // download the files in the list                for (int i = 0; i < fileList.size(); i++)                {                    thisFile = fileList.elementAt(i);                    if (thisFile.startsWith("Each Months"))                        continue;                    else if (thisFile.indexOf("://") == -1)                    {                        thisFile = urlPath + thisFile;                    }                    try                    {                        url = new URL(thisFile);                        count += getInBinaryMode(url, "(" + (i + 1) + "/" + fileList.size() + ")");                    }                    catch (MalformedURLException ex)                    {                    }                }            }        }        catch (MalformedURLException ex)        {            LabelStatus.setText("URL error.");        }        return count;    }    String getYYMMPath(String path, String year)    {        if (path.charAt(path.length() - 1) != '\\') path += "\\";        String yyyymm;        int position = path.substring(0, path.length() - 1).lastIndexOf("\\");        yyyymm = path.substring(position + 1, path.length() - 1);        return path.replaceFirst(yyyymm, year);    }    int getInBinaryMode(URL url, String process)    {        int count = 0;        String urlPath = url.getPath();        int start = urlPath.lastIndexOf('/');        String filename = urlPath.substring(start + 1);        if (filename.equals(""))            filename = "index.html";        try        {            // read url file            InputStream is;            int i = 0;            do            {                is = url.openStream();                i++;                Thread.sleep(1000);            } while (is.available() == 0 && i < 10);            if (is.available() == 0)                return count;            File file = new File(path + filename);            if (!file.createNewFile() && !override)            {                System.out.println("File \"" + filename + "\" already exist.");                return count;            } else            {                File.createTempFile("tmp", "");            }            // write file to disk            OutputStream os = new FileOutputStream(file);            LabelStatus.setText("Downloading file " + process + " " + filename + " ...");            int length;            while ((length = (int) is.read(context)) > 0)            {                os.write(context, 0, length);            }            os.close();            is.close();            count++;        }        catch (IOException e)        {            System.out.println("BatchDownload:getFileList::Did not find file <" + filename + ">, " + e.getMessage());        } catch (InterruptedException e)        {        }        return count;    }    void CheckBoxRememberDir_actionPerformed(ActionEvent e)    {        if (((JCheckBox) e.getSource()).getModel().isSelected())        {            path = getOldPath();            TextFieldSaveTo.setText(path);        }    }    String getOldPath()    {        String oldPath = "";        // read the path from configuration file        try        {            File config = new File("Batchdown.ini");            FileReader in = new FileReader(config);            char c;            int count = 0;            while ((c = (char) in.read()) != '>')            {                oldPath += c;                if (count++ == 128)                {                    oldPath = "";                    break;                }            }            in.close();            if (oldPath.equals(""))                oldPath = System.getProperty("OLD_PATH");        } catch (IOException ex)        {        }        return oldPath;    }    Vector<String> getFileList(String filename)    {        Vector<String> list = new Vector<String>();        BufferedReader input = null;        String line;        int maxLength = 1024 * 1024; // max 1M per time        int pos, start, end, lastEnd = 0;        StringBuffer lineBuf;        boolean readOver;        final String sign1 = "href";        final char sign2 = '=';        char c;        try        {            input = new BufferedReader(new FileReader(filename));            System.out.println("BatchDownload:getFileList::Waiting file <" + filename + "> ready...");            while (!input.ready())            {                try                {                    System.out.print(".");                    Thread.sleep(1000);                }                catch (InterruptedException e)                {                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.                }            }            System.out.println("Done");            System.out.println("BatchDownload:getFileList::Start to read file <" + filename + ">");            readOver = false;            lineBuf = new StringBuffer("");            while (!readOver)            {                if ((line = input.readLine()) == null)                    readOver = true;                else                {                    lineBuf.append(line);                    if (lineBuf.length() < maxLength)                        continue;                }                // get every link after "href="                pos = 0;                while (pos < lineBuf.length())                {                    if ((pos = lineBuf.indexOf(sign1, pos)) == -1)                        break;                    else                    {                        pos += sign1.length();                        while (pos < lineBuf.length())                        {                            if (isVoidChar(lineBuf.charAt(pos)))                                continue;                            else if (lineBuf.charAt(pos) == sign2)                                break;                            pos++;                        }                        assert pos == lineBuf.length();                    }                    // get the next position after "="                    while (isVoidChar(lineBuf.charAt(++pos)) && pos < lineBuf.length()) ;                    c = lineBuf.charAt(pos);                    if (c == '\'' || c == '\"')                    {                        pos++;                    }                    start = pos;                    end = 0;                    while (pos < lineBuf.length())                    {                        c = lineBuf.charAt(pos);                        if (c == '\'' || c == '\"' || c == '>' || c == '<')                        {                            end = pos;                            break;                        }                        pos++;                    }                    if (end != 0)                    {                        if (end > start)                        {                            line = lineBuf.substring(start, end);                            if (line.startsWith("./") || line.startsWith(".\\"))                                line = line.substring(2);                            list.add(line);                        }                        lastEnd = end;                    } else                    {                        lineBuf = new StringBuffer(lineBuf.substring(lastEnd));                    }                }            }        }        catch (IOException e)        {            System.out.println("BatchDownload:getFileList::Reading file <" + filename + "> error.");        }        finally        {            try            {                if (input != null) input.close();            } catch (IOException e)            {            }            System.out.println("BatchDownload:getFileList::Finished to read file <" + filename + ">");        }        return list;    }    void ButtonChooser_actionPerformed(ActionEvent e)    {        JFileChooser dirChooser = new JFileChooser();        dirChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);        path = getOldPath();        if (!path.equals(""))        {            dirChooser.setCurrentDirectory(new File(path));        }        if (JFileChooser.APPROVE_OPTION == dirChooser.showOpenDialog(this))        {            path = dirChooser.getSelectedFile().getPath();            TextFieldSaveTo.setText(path);        }    }    // if the character the space or cr    public boolean isVoidChar(char c)    {        return c == ' ' || c == '\t' || c == '\n' || c == '\r';    }}class BatchDownload_ButtonCancel_actionAdapter implements java.awt.event.ActionListener{    BatchDownload adaptee;    BatchDownload_ButtonCancel_actionAdapter(BatchDownload adaptee)    {        this.adaptee = adaptee;    }    public void actionPerformed(ActionEvent e)    {        adaptee.ButtonCancel_actionPerformed(e);    }}class BatchDownload_ButtonOk_actionAdapter implements java.awt.event.ActionListener{    BatchDownload adaptee;    BatchDownload_ButtonOk_actionAdapter(BatchDownload adaptee)    {        this.adaptee = adaptee;    }    public void actionPerformed(ActionEvent e)    {        adaptee.ButtonOk_actionPerformed(e);    }}class BatchDownload_CheckBoxRememberDir_actionAdapter implements java.awt.event.ActionListener{    BatchDownload adaptee;    BatchDownload_CheckBoxRememberDir_actionAdapter(BatchDownload adaptee)    {        this.adaptee = adaptee;    }    public void actionPerformed(ActionEvent e)    {        adaptee.CheckBoxRememberDir_actionPerformed(e);    }}class BatchDownload_ButtonChooser_actionAdapter implements java.awt.event.ActionListener{    BatchDownload adaptee;    BatchDownload_ButtonChooser_actionAdapter(BatchDownload adaptee)    {        this.adaptee = adaptee;    }    public void actionPerformed(ActionEvent e)    {        adaptee.ButtonChooser_actionPerformed(e);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -