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

📄 fileuploadapplet.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src-components/org/opencms/applet/upload/FileUploadApplet.java,v $
 * Date   : $Date: 2006/03/27 14:52:27 $
 * Version: $Revision: 1.19 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.applet.upload;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.swing.JApplet;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.MultipartPostMethod;

/**
 * File Upload Applet, displays a file selector box to upload multiple resources into OpenCms.<p>
 * 
 * @author Michael Emmerich 
 * 
 * @version $Revision: 1.19 $ 
 * 
 * @since 6.0.0 
 */
public class FileUploadApplet extends JApplet implements Runnable {

    /** Serial version UID required for safe serialization. */
    private static final long serialVersionUID = -3710093915699772778L;

    /** Applet thread. */
    Thread m_runner;

    /** The URL of the OpenCms instance. */
    private String m_opencms = "";

    /** The URL to send the uploaded files to. */
    private String m_targetUrl = "";

    /** The URL to return to after uploading the files. */
    private String m_redirectUrl = "";
    
    /** The Target Frame to return to after uploading the files. */
    private String m_redirectTargetFrame = "";

    /** The URL to return to after an error. */
    private String m_errorUrl = "";

    /** The name of the folder to upload to. */
    private String m_uploadFolder = "";

    /** Maximum file upload size. */
    private long m_maxsize = -1;

    /** Number of resources to upload. */
    private int m_resources;

    /** File extensions, used to find the correct icons for the selectbox. */
    private String m_fileExtensions = "";

    /** Color defintions. */
    private HashMap m_colors = new HashMap();

    /** Output string for action messages. */
    private String m_action = "";

    /** Output string for loggin messages. */
    private String m_message = "";

    /** Output mode selector. */
    private int m_outputMode;

    /** Counter for creating the progress bar. */
    private int m_step;

    /** Definition of the images during upload. */
    private Image m_source;
    private Image m_target;
    private Image m_floater;

    /** Image position for the floater during upload. */
    private int m_floaterPos = 50;

    /** Defintion of output strings.*/
    private String m_actionOutputSelect = "Seleting files for upload....";
    private String m_actionOutputCount = "Counting resources ....";
    private String m_actionOutputCreate = "Creating Zip-File...";
    private String m_actionOutputUpload = "Upload Zip-File";
    private String m_actionOutputError = "Error";
    private String m_messageNoPreview = "no preview available";
    private String m_errorLine1 = "An error has occurred on the server:";
    private String m_messageOutputUpload = "Please wait, uploading data...";
    private String m_messageOutputAdding = "Adding ";
    private String m_messageOutputErrorSize = "Zip file too big:";
    private String m_messageOutputErrorZip = "Error creating Zip-File, see Java Console.";

    /** Definition variables for graphics output. */
    private Font m_font;
    private FontMetrics m_metrics;
    private Image m_offscreen;
    private Graphics m_offgraphics;

    /** The file selector. */
    private JFileChooser m_fileSelector;

    /**
     * @see java.applet.Applet#init()
     */
    public void init() {

        m_opencms = getParameter("opencms");
        m_targetUrl = getParameter("target");
        m_redirectUrl = getParameter("redirect");
        m_redirectTargetFrame = getParameter("targetframe");
        if (m_redirectTargetFrame == null || m_redirectTargetFrame.equals("")) {
            m_redirectTargetFrame = "explorer_files";
        }
        m_errorUrl = getParameter("error");
        m_uploadFolder = getParameter("filelist");
        String tmpSize = getParameter("maxsize");
        if (tmpSize != null && tmpSize.length() > 0) {
            m_maxsize = Long.parseLong(tmpSize);
        }
        m_fileExtensions = getParameter("fileExtensions");
        m_colors = extractColors(getParameter("colors"));

        // setup the applet output
        m_font = new java.awt.Font(null, Font.BOLD, 12);
        m_metrics = getFontMetrics(m_font);
        m_source = getImage(getCodeBase(), "org/opencms/applet/upload/applet_source.png");
        m_target = getImage(getCodeBase(), "org/opencms/applet/upload/applet_target.png");
        m_floater = getImage(getCodeBase(), "org/opencms/applet/upload/floater.gif");

        // get the output massages in the correct language
        if (getParameter("actionOutputSelect") != null) {
            m_actionOutputSelect = getParameter("actionOutputSelect");
        }
        if (getParameter("actionOutputCount") != null) {
            m_actionOutputCount = getParameter("actionOutputCount");
        }
        if (getParameter("actionOutputCreate") != null) {
            m_actionOutputCreate = getParameter("actionOutputCreate");
        }
        if (getParameter("actionOutputUpload") != null) {
            m_actionOutputUpload = getParameter("actionOutputUpload");
        }
        if (getParameter("actionOutputError") != null) {
            m_actionOutputError = getParameter("actionOutputError");
        }
        if (getParameter("messageOutputUpload") != null) {
            m_messageOutputUpload = getParameter("messageOutputUpload");
        }
        if (getParameter("messageOutputAdding") != null) {
            m_messageOutputAdding = getParameter("messageOutputAdding");
        }
        if (getParameter("messageOutputErrorZip") != null) {
            m_messageOutputErrorZip = getParameter("messageOutputErrorZip");
        }
        if (getParameter("messageOutputErrorSize") != null) {
            m_messageOutputErrorSize = getParameter("messageOutputErrorSize");
        }
        if (getParameter("messageNoPreview") != null) {
            m_messageNoPreview = getParameter("messageNoPreview");
        }
        if (getParameter("errorLine1") != null) {
            m_errorLine1 = getParameter("errorLine1");
        }
    }

    /**
     * @see java.applet.Applet#destroy()
     */
    public void destroy() {

        // NOOP
    }

    /**
     * @see java.applet.Applet#start()
     */
    public void start() {

        m_runner = new Thread(this);
        m_runner.start();
    }

    /**
     * @see java.applet.Applet#stop()
     */
    public void stop() {

        m_runner = null;
    }

    /**
     * @see java.lang.Runnable#run()
     */
    public void run() {

        try {
            boolean ok = true;
            while (ok) {
                ok = true;
                
                //System.out.println("Version 1.62");
                                
                m_message = "";
                m_resources = 0;
                m_step = 0;
                // create a new file chooser

                if (m_fileSelector == null) {
                    m_fileSelector = new JFileChooser();
                }

                // file selector can read files and folders
                m_fileSelector.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

                m_fileSelector.setDialogTitle(m_actionOutputSelect);

                // add two custom file filters (office and images) and the default filters
                m_fileSelector.addChoosableFileFilter(new ImageFilter());
                m_fileSelector.addChoosableFileFilter(new OfficeFilter());
                m_fileSelector.setAcceptAllFileFilterUsed(true);
                // enable multi-selection of files
                m_fileSelector.setMultiSelectionEnabled(true);
                // add custom icons for file types.
                m_fileSelector.setFileView(new ImageFileView(m_opencms, m_fileExtensions));
                // add the image preview pane.
                m_fileSelector.setAccessory(new ImagePreview(m_fileSelector, m_messageNoPreview));

                m_action = m_actionOutputSelect;
                repaint();

                // show the file selector dialog
                int returnVal = m_fileSelector.showDialog(this, "OK");

                // process the results.
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    // count all resources
                    m_outputMode = 1;
                    m_action = m_actionOutputCount;
                    repaint();
                    m_resources = countResources(m_fileSelector.getSelectedFiles());
                    // create the zipfile  
                    m_outputMode = 2;
                    File targetFile = createZipFile(m_fileSelector.getSelectedFiles());
                    // check the size of the zip files
                    if (targetFile == null || m_maxsize > 0 && targetFile.length() > m_maxsize) {
                        // show some details in the applet itself
                        m_outputMode = 4;
                        if (targetFile == null) {
                            m_message = m_messageOutputErrorZip;
                        } else {
                            m_message = m_messageOutputErrorSize + " " + targetFile.length() + " > " + m_maxsize;
                        }
                        m_action = m_actionOutputError;
                        repaint();
                        // show an error-alertbog
                        JOptionPane.showMessageDialog(this, m_message, m_action, JOptionPane.ERROR_MESSAGE);
                    } else {
                        m_outputMode = 3;
                        m_message = m_messageOutputUpload + " (" + targetFile.length() / 1024 + " kb)";
                        repaint();
                        // upload the zipfile
                        FileUploadThread uploadThreat = new FileUploadThread();

                        uploadThreat.init(this);
                        uploadThreat.start();

                        uploadZipFile(targetFile);
                        ok = false;
                    }

                } else {
                    //the cancel button was used, so go back to the workplace
                    ok = false;
                    getAppletContext().showDocument(new URL(m_redirectUrl), m_redirectTargetFrame);
                }
            }
        } catch (Exception e) {
            System.err.println(e);
        }
    }

    /**
     * Counts all resources to add to the zip file.<p>
     * 
     * @param files the files to be packed into the zipfile
     * @return number of resources
     */
    private int countResources(File[] files) {

        int count = 0;
        // look through all selected resources
        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()) {
                // its a file, count it
                count++;
            } else {
                // its a folder, count all resources in it and add the number
                count += countSubresources(files[i]);
            }
        }
        return count;
    }

    /**
     * Counts all resources in a folder.<p>

⌨️ 快捷键说明

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