📄 fileuploadapplet.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src-components/org/opencms/applet/upload/FileUploadApplet.java,v $
* Date : $Date: 2007-08-13 16:30:16 $
* Version: $Revision: 1.27 $
*
* This library is part of OpenCms -
* the Open Source Content Management System
*
* Copyright (c) 2002 - 2007 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.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
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 javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileFilter;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
/**
* File Upload Applet, displays a file selector box to upload multiple resources into OpenCms.<p>
*
* @author Michael Emmerich
*
* @version $Revision: 1.27 $
*
* @since 6.0.0
*/
public class FileUploadApplet extends JApplet implements Runnable {
/** The JSESSIONID cookie header name. */
public static final String C_JSESSIONID = "JSESSIONID";
/** The value for the resource upload applet action. */
// Warning: This constant has to be kept in sync with the same named constant in
// org.opencms.explorer.CmsNewResourceUpload
public static final String DIALOG_CHECK_OVERWRITE = "checkoverwrite";
/** Serial version UID required for safe serialization. */
private static final long serialVersionUID = -3710093915699772778L;
/** Output string for action messages. */
private String m_action = "";
private String m_actionOutputCount = "Counting resources ....";
private String m_actionOutputCreate = "Creating Zip-File...";
private String m_actionOutputError = "Error";
/** Defintion of output strings.*/
private String m_actionOutputSelect = "Seleting files for upload....";
private String m_actionOutputUpload = "Upload Zip-File";
private String m_actionOverwriteCheck = "Checking file existance on server...";
/** Indicates if the applet certificate has been accepted. */
private boolean m_certificateAccepted;
private String m_certificateErrorMessage = "The required Applet certificate has not been accepted!";
private String m_certificateErrorTitle = "Error initializing the OpenCms Upload Applet";
/** The initial folder path for the file chooser. */
private String m_clientFolder = null;
/** Color defintions. */
private HashMap m_colors = new HashMap();
private String m_errorLine1 = "An error has occurred on the server:";
/** The URL to return to after an error. */
private String m_errorUrl = "";
/** File extensions, used to find the correct icons for the selectbox. */
private String m_fileExtensions = "";
/** The type of gallery to upload to. */
private String m_fileFilterSelection = "";
/** The file selector. */
private UploadAppletFileChooser m_fileSelector;
private Image m_floater;
/** Image position for the floater during upload. */
private int m_floaterPos = 50;
/** Definition variables for graphics output. */
private Font m_font;
/** Maximum file upload size. */
private long m_maxsize = -1;
/** Output string for loggin messages. */
private String m_message = "";
private String m_messageNoPreview = "no preview available";
private String m_messageOutputAdding = "Adding ";
private String m_messageOutputErrorSize = "Zip file too big:";
private String m_messageOutputErrorZip = "Error creating Zip-File, see Java Console.";
private String m_messageOutputUpload = "Please wait, uploading data...";
private FontMetrics m_metrics;
private Graphics m_offgraphics;
private Image m_offscreen;
/** The URL of the OpenCms instance. */
private String m_opencms = "";
/** Output mode selector. */
private int m_outputMode;
private ModalDialog m_overwriteDialog;
private String m_overwriteDialogCancel = "Cancel";
private String m_overwriteDialogIntro = "The files listed below already exist on the server. \nAll checked files will be overwritten.";
private String m_overwriteDialogLocale = "en";
private String m_overwriteDialogOk = "Ok";
private String m_overwriteDialogTitle = "Select the files to overwrite on the server";
/** List of potential overwrites. */
private List m_overwrites;
/** The Target Frame to return to after uploading the files. */
private String m_redirectTargetFrame = "";
/** The URL to return to after uploading the files. */
private String m_redirectUrl = "";
/** Number of resources to upload. */
private int m_resources;
/** Applet thread. */
private Thread m_runner;
/** Definition of the images during upload. */
private Image m_source;
/** Counter for creating the progress bar. */
private int m_step;
private Image m_target;
/** The URL to send the uploaded files to. */
private String m_targetUrl = "";
/** The name of the folder to upload to. */
private String m_uploadFolder = "";
/**
* Adds a single file to the zip output.<p>
*
* @param zipStream the zip output stream
* @param file the file to add to the stream
* @throws Exception if something goes wrong
*/
private void addFileToZip(ZipOutputStream zipStream, File file) throws Exception {
// add to zipfile
String name = file.getAbsolutePath().replace('\\', '/');
name = name.substring(m_fileSelector.getCurrentDirectory().getAbsolutePath().length());
m_message = m_messageOutputAdding + " " + name + "..";
m_step++;
repaint();
ZipEntry entry = new ZipEntry(name);
zipStream.putNextEntry(entry);
writeFileBytes(file, zipStream);
zipStream.closeEntry();
}
private void addFolderToZip(ZipOutputStream zipStream, File file) throws Exception {
File[] children = file.listFiles();
File child;
for (int i = 0; i < children.length; i++) {
child = children[i];
if (child.isDirectory()) {
addFolderToZip(zipStream, child);
} else {
addFileToZip(zipStream, child);
}
}
}
/**
* Returns the merge of both file arrays with no check for duplications. <p>
*
* @param files first array of files
*
* @param overwriteFiles 2nd array of files
*
* @return the union of both file arrays
*/
private File[] addOverwrites(File[] files, File[] overwriteFiles) {
List result = new ArrayList(files.length + overwriteFiles.length);
// faster for loop;)
for (int i = files.length - 1; i >= 0; i--) {
result.add(files[i]);
}
for (int i = overwriteFiles.length - 1; i >= 0; i--) {
result.add(overwriteFiles[i]);
}
return (File[])result.toArray(new File[result.size()]);
}
/**
* Checks if the given client files exist on the server and internally stores duplications.<p>
*
* Comparison is made by cutting the current directory of the file chooser from the path of the given files.
* The server files (VFS files) to compare to are found by the current session of the user which finds the correct site and
* the knowledge about the current directory. File translation rules are taken into account on the server. <p>
*
* @param files the local files to check if they exist in the VFS
*
* @return one of {@link ModalDialog#ERROR_OPTION} , {@link ModalDialog#CANCEL_OPTION}, {@link ModalDialog#APPROVE_OPTION}.
*/
int checkServerOverwrites(File[] files) {
m_action = m_actionOverwriteCheck;
repaint();
int rtv = ModalDialog.ERROR_OPTION;
// collect files
List fileNames = new ArrayList();
for (int i = 0; i < files.length; i++) {
getRelativeFilePaths(files[i], fileNames);
}
StringBuffer uploadFiles = new StringBuffer();
Iterator it = fileNames.iterator();
// Http post header is limited, therefore only a ceratain amount of files may be checked
// for server overwrites. Solution is: multiple requests.
int count = 0;
List duplications;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -