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

📄 main.java

📁 用java applet實現的檔案上傳功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * $Id: Main.java 116 2006-09-14 11:24:14Z sdrycroft $ *//*  Copyright (C) 2005 Simon David Rycroft         This program is free software; you can redistribute it and/or        modify it under the terms of the GNU General Public License        as published by the Free Software Foundation; either version 2        of the License, or (at your option) any later version.         This program is distributed in the hope that it will be useful,        but WITHOUT ANY WARRANTY; without even the implied warranty of        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        GNU General Public License for more details.         You should have received a copy of the GNU General Public License        along with this program; if not, write to the Free Software        Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. */import java.awt.BorderLayout;import java.awt.Color;import java.awt.Container;import java.awt.dnd.DropTargetListener;import java.awt.dnd.DropTargetDropEvent;import java.awt.dnd.DropTargetDragEvent;import java.awt.dnd.DropTargetEvent;import java.awt.dnd.DropTarget;import java.awt.dnd.DnDConstants;import java.awt.datatransfer.*;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.GridLayout;import java.io.File;import java.io.PrintStream;import java.io.BufferedReader;import java.net.URL;import java.net.URI;import java.util.Arrays;import java.util.Iterator;import java.util.List;import java.util.Vector;import javax.swing.BorderFactory;import javax.swing.JApplet;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JProgressBar;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.table.TableColumn;import javax.swing.UIManager;import javax.swing.SwingConstants;import netscape.javascript.JSObject;import java.net.UnknownHostException;import java.net.MalformedURLException;import javax.swing.UnsupportedLookAndFeelException;public class Main extends JApplet implements MouseListener, DropTargetListener {        JTable table;    JButton add, remove, upload, help;    TableData tabledata;    TableColumn sizeColumn;    File [] files;    JLabel progCompletion;    JProgressBar progBar;    int sentBytes,totalBytes,buttonClicked;    Color backgroundColour, columnHeadColourBack, columnHeadColourFore;    PostletLabels pLabels;        // Boolean set to false when a javascript method is executed    boolean javascript;        // Parameters    URL endPageURL, helpPageURL, destinationURL;    boolean warnMessage, autoUpload;    String language,endpage,helppage;    int maxThreads;    String [] fileExtensions;        // URI list flavor (Hack for linux)    DataFlavor uriListFlavor;        public void init() {        // First thing, output the version, for debugging purposes.        System.out.println("POSTLET VERSION: 0.10");        String date = "$Date: 2006-09-14 12:24:14 +0100 (Thu, 14 Sep 2006) $";        System.out.println(date.substring(7,date.length()-1));        	// URI list flavor:	try {		uriListFlavor = new DataFlavor("text/uri-list;class=java.lang.String");	}	catch (ClassNotFoundException cnfe){		errorMessage(System.out, "No class found for DataFlavor");	}	        // Set the javascript to false, and start listening for clicks        javascript = false;        JavascriptListener jsListen = new JavascriptListener(this);        jsListen.start();        buttonClicked = 0; // Default of add click.                getParameters();        pLabels = new PostletLabels(language, getCodeBase());        layoutGui();            }        private void layoutGui(){                // Set the look of the applet        try {            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());        } catch (UnsupportedLookAndFeelException exc){;} catch (IllegalAccessException exc){;} catch (ClassNotFoundException exc){;} catch (InstantiationException exc){;}                // Get the main pane to add content to.        Container pane = getContentPane();                // Attempt to add drop listener to the whole applet        try {            DropTarget dt = new DropTarget();            dt.addDropTargetListener(this);            pane.setDropTarget(dt);        } catch (java.util.TooManyListenersException tmle){            errorMessage(System.out, "Too many listeners to drop!");        }                // Table for the adding of Filenames and sizes to.        tabledata = new TableData(pLabels.getLabel(0),pLabels.getLabel(1)+" -KB ");        table = new JTable(tabledata);        table.setColumnSelectionAllowed(false);        //table.setDragEnabled(false); // This method is not available to Java 3!        sizeColumn = table.getColumn(pLabels.getLabel(1)+" -KB ");        sizeColumn.setMaxWidth(100);        table.getColumn(pLabels.getLabel(1)+" -KB ").setMinWidth(100);        if (columnHeadColourBack != null && backgroundColour != null){            errorMessage(System.out, "setting the tables colours");            table.getTableHeader().setBackground(columnHeadColourBack);            table.getTableHeader().setForeground(columnHeadColourFore);            table.setBackground(backgroundColour);        }        JScrollPane scrollPane = new JScrollPane(table);        scrollPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));                // Add the scroll pane/table to the main pane        pane.add(scrollPane, BorderLayout.CENTER);                JPanel rightPanel = new JPanel(new GridLayout(4,1,10,10));        rightPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));                add = new JButton(pLabels.getLabel(6));        add.addMouseListener(this);        rightPanel.add(add);                remove = new JButton(pLabels.getLabel(7));        remove.addMouseListener(this);        remove.setEnabled(false);        rightPanel.add(remove);                upload = new JButton(pLabels.getLabel(8));        upload.addMouseListener(this);        upload.setEnabled(false);        rightPanel.add(upload);                help = new JButton(pLabels.getLabel(9));        help.addMouseListener(this);        rightPanel.add(help);        pane.add(rightPanel,"East");                JPanel progPanel = new JPanel(new GridLayout(2, 1));        progPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));                progCompletion = new JLabel(pLabels.getLabel(10),SwingConstants.CENTER);        progPanel.add(progCompletion);                progBar = new JProgressBar();        progPanel.add(progBar);        progPanel.setBorder(BorderFactory.createEmptyBorder(5,25,5,25));                if (backgroundColour != null){            pane.setBackground(backgroundColour);            rightPanel.setBackground(backgroundColour);            scrollPane.setBackground(backgroundColour);            progPanel.setBackground(backgroundColour);        }        // Always set the table background colour as White.        // May change this if required, only would require alot of Params!        scrollPane.getViewport().setBackground(Color.white);                pane.add(progPanel,"South");                // If the destination has not been set/isn't a proper URL        // Then deactivate the buttons.        if (destinationURL == null)            add.setEnabled(false);    }        public void errorMessage(PrintStream out, String message){        out.println("***"+message+"***");    }        // Helper method for getting the parameters from the webpage.    private void getParameters(){                /*  LANGUAGE */        try {            language = getParameter("language");            if (language == "" || language == null)                language = "EN";        } catch (NullPointerException nullLang){            // Default language being set            language = "EN";            errorMessage(System.out,"language is null");        }                /*  DESTINATION  */        try {            destinationURL = new URL(getParameter("destination"));	    // Following line is for testing, and to hard code the applet to postlet.com	    //destinationURL = new URL("http://www.postlet.com/example/javaUpload.php");        } catch(java.net.MalformedURLException malurlex){            // Do something here for badly formed destination, which is ESENTIAL.            errorMessage(System.out, "Badly formed destination:###"+getParameter("destination")+"###");            JOptionPane.showMessageDialog(null, pLabels.getLabel(3),pLabels.getLabel(5), JOptionPane.ERROR_MESSAGE);        } catch(java.lang.NullPointerException npe){            // Do something here for the missing destination, which is ESENTIAL.            errorMessage(System.out,"destination is null");            JOptionPane.showMessageDialog(null, pLabels.getLabel(4), pLabels.getLabel(5), JOptionPane.ERROR_MESSAGE);        }                /*  BACKGROUND  */        try {            Integer bgci = new Integer(getParameter("backgroundcolour"));            backgroundColour = new Color(bgci.intValue());        } catch(NumberFormatException numfe){            errorMessage(System.out, "background colour is not a number:###"+getParameter("backgroundcolour")+"###");        } catch (NullPointerException nullred){            errorMessage(System.out, "background colour is null");        }                /*  TABLEHEADERFOREGROUND  */        try {            Integer thfi = new Integer(getParameter("tableheadercolour"));            columnHeadColourFore = new Color(thfi.intValue());        } catch(NumberFormatException numfe){            errorMessage(System.out, "table header colour is not a number:###"+getParameter("tableheadcolour")+"###");        } catch (NullPointerException nullred){            errorMessage(System.out, "table header colour is null");        }                /*  TABLEHEADERBACKGROUND  */        try {            Integer thbi = new Integer(getParameter("tableheaderbackgroundcolour"));            columnHeadColourBack = new Color(thbi.intValue());        } catch(NumberFormatException numfe){            errorMessage(System.out, "table header back colour is not a number:###"+getParameter("tableheaderbackgroundcolour")+"###");        } catch (NullPointerException nullred){            errorMessage(System.out, "table header back colour is null");        }                /*  FILEEXTENSIONS  */        try {            fileExtensions = getParameter("fileextensions").split(",");        } catch(NullPointerException nullfileexts){            errorMessage(System.out, "file extensions is null");        }                /*  WARNINGMESSAGE  */        try {            if (getParameter("warnmessage").toLowerCase() == "true")                warnMessage = true;            else                warnMessage = false;        } catch(NullPointerException nullwarnmessage){            errorMessage(System.out, "warnmessage is null");            warnMessage = false;        }                /*  AUTOUPLOAD  */        try {            if (getParameter("autoupload").toLowerCase() == "true")                autoUpload = true;            else                autoUpload = false;        } catch(NullPointerException nullwarnmessage){            errorMessage(System.out, "autoUpload is null");            autoUpload = false;        }                /*  MAXTHREADS  */        try {            Integer maxts = new Integer(getParameter("maxthreads"));            maxThreads = maxts.intValue();        } catch (NullPointerException nullmaxthreads){            errorMessage(System.out, "maxthreads is null");        } catch (NumberFormatException nummaxthreads){            errorMessage(System.out, "maxthread is not a number");}                /*  ENDPAGE  */        try {            endPageURL = new URL(getParameter("endpage"));        } catch(java.net.MalformedURLException malurlex){            errorMessage(System.out, "endpage is badly formed:###"+getParameter("endpage")+"###");        } catch(java.lang.NullPointerException npe){            errorMessage(System.out, "endpage is null");        }                /*  HELPPAGE  */        try {            helpPageURL = new URL(getParameter("helppage"));        } catch(java.net.MalformedURLException malurlex){

⌨️ 快捷键说明

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