📄 fileuploadapplet.java
字号:
// request to server
HttpClient client = new HttpClient();
this.m_overwrites = new ArrayList();
try {
while (it.hasNext()) {
count++;
uploadFiles.append(((String)it.next())).append('\n');
if (((count % 40) == 0) || (!it.hasNext())) {
// files to upload:
PostMethod post = new PostMethod(m_targetUrl);
Header postHeader = new Header("uploadFiles", URLEncoder.encode(uploadFiles.toString(), "utf-8"));
post.addRequestHeader(postHeader);
// upload folder in vfs:
Header header2 = new Header("uploadFolder", URLEncoder.encode(getParameter("filelist"), "utf-8"));
post.addRequestHeader(header2);
// the action constant
post.setParameter("action", DIALOG_CHECK_OVERWRITE);
// add jsessionid query string
String sessionId = getParameter("sessionId");
String query = ";" + C_JSESSIONID.toLowerCase() + "=" + sessionId;
post.setQueryString(query);
post.addRequestHeader(C_JSESSIONID, sessionId);
HttpConnectionParams connectionParams = client.getHttpConnectionManager().getParams();
connectionParams.setConnectionTimeout(5000);
// add the session cookie
client.getState();
client.getHostConfiguration().getHost();
HttpState initialState = new HttpState();
URI uri = new URI(m_targetUrl, false);
Cookie sessionCookie = new Cookie(uri.getHost(), C_JSESSIONID, sessionId, "/", null, false);
initialState.addCookie(sessionCookie);
client.setState(initialState);
int status = client.executeMethod(post);
if (status == HttpStatus.SC_OK) {
String response = post.getResponseBodyAsString();
duplications = parseDuplicateFiles(URLDecoder.decode(response, "utf-8"));
this.m_overwrites.addAll(duplications);
} else {
// continue without overwrite check
String error = m_errorLine1 + "\n" + post.getStatusLine();
System.err.println(error);
}
count = 0;
uploadFiles = new StringBuffer();
}
}
if (m_overwrites.size() > 0) {
rtv = showDuplicationsDialog(m_overwrites);
} else {
rtv = ModalDialog.APPROVE_OPTION;
}
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace(System.err);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(System.err);
}
return rtv;
}
/**
* 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>
*
* @param folder the folder to count
* @return number of resources
*/
private int countSubresources(File folder) {
int count = 0;
if (folder.isFile()) {
// check if is really a folder
count = 1;
} else {
// recurest to count
count = countResources(folder.listFiles());
}
return count;
}
/**
* Creates a ZipFile from all files to upload.<p>
*
* @param files the files to be packed into the zipfile
* @return reference to the zipfile
*/
private File createZipFile(File[] files) {
File targetFile = null;
if (files.length > 0) {
m_action = m_actionOutputCreate;
try {
// create a new zipStream
String zipFileName = ".opencms_upload.zip";
String userHome = System.getProperty("user.home");
// create file in user home directory where write permissions should exist
if (userHome != null) {
if (!userHome.endsWith(File.separator)) {
userHome = userHome + File.separator;
}
zipFileName = userHome + zipFileName;
}
ZipOutputStream zipStream = new ZipOutputStream(new FileOutputStream(zipFileName));
// loop through all files
for (int i = 0; i < files.length; i++) {
// if its a file, add it to the zipfile
if (files[i].isFile()) {
addFileToZip(zipStream, files[i]);
} else {
addFolderToZip(zipStream, files[i]);
}
repaint();
// free mem
files[i] = null;
}
zipStream.close();
// get the zipfile
targetFile = new File(zipFileName);
} catch (Exception e) {
System.err.println("Error creating zipfile " + getStackTraceAsString(e));
}
}
return targetFile;
}
/**
* Returns the stack trace (including the message) of an exception as a String.<p>
*
* If the exception is a CmsException,
* also writes the root cause to the String.<p>
*
* @param e the exception to get the stack trace from
* @return the stack trace of an exception as a String
*/
private static String getStackTraceAsString(Throwable e) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
return stringWriter.toString();
}
/**
* @see java.applet.Applet#destroy()
*/
public void destroy() {
// NOOP
}
/**
* Displays an error message in case the applet could not be initialized.<p>
*/
public void displayError() {
m_outputMode = 5;
m_action = m_certificateErrorTitle;
m_message = m_certificateErrorMessage;
JOptionPane.showMessageDialog(this, m_message, m_action, JOptionPane.ERROR_MESSAGE);
try {
// redirect back to the server
getAppletContext().showDocument(new URL(m_redirectUrl), m_redirectTargetFrame);
} catch (MalformedURLException e) {
// this should never happen
e.printStackTrace();
}
stop();
}
/**
* Extracts the colors from the parameter String.<p>
*
* @param colors list of color names and values
* @return HashMap with color names and values
*/
private HashMap extractColors(String colors) {
HashMap colorStorage = new HashMap();
if (colors != null) {
StringTokenizer tok = new StringTokenizer(colors, ",");
// loop through the tokens
// all tokens have the format "extension=type"
while (tok.hasMoreElements()) {
String token = tok.nextToken();
// now extract the file extension and the type
String colorName = token.substring(0, token.indexOf("="));
String colorValue = token.substring(token.indexOf("=") + 1);
colorStorage.put(colorName, colorValue);
}
}
return colorStorage;
}
/**
* Gets a color for drawing the output.<p>
*
* @param colorName the name of the color
* @return color
*/
private Color getColor(String colorName) {
Color col = Color.black;
try {
col = Color.decode((String)m_colors.get(colorName));
} catch (Exception e) {
System.err.println("Error reading " + colorName + ":" + e);
}
return col;
}
/**
* Puts all given files (and all files in subtree of given potential "folder" file) into the given
* list (as {@link File} instances) with respect to the current file filter in the file chooser.
* <p>
*
*
* @param file the file or folder to collect all subfiles of with respect to the current file chooser.
*
* @param fileNames all given files (and all files in subtree of given potential "folder" file) into the given
* list (as {@link File} instances) with respect to the current file filter in the file chooser
*/
private void getFilesInTree(final File file, final List fileNames) {
if (file.isDirectory()) {
File[] children = file.listFiles();
for (int i = 0; i < children.length; i++) {
getFilesInTree(children[i], fileNames);
}
} else {
FileFilter filter = m_fileSelector.getFileFilter();
if (filter.accept(file)) {
fileNames.add(file);
}
}
}
/**
* Puts the path of the given file in relation to the current root directory of the intenal
* file chooser with support for folder-recursion. <p>
*
*
* @param file the file to put into relation to the root directory of the internal file chooser or a folder
* of files to do this.
*
* @param fileNames will contain the result.
*/
private void getRelativeFilePaths(final File file, final List fileNames) {
if (file.isDirectory()) {
File[] children = file.listFiles();
for (int i = 0; i < children.length; i++) {
getRelativeFilePaths(children[i], fileNames);
}
} else {
FileFilter filter = m_fileSelector.getFileFilter();
if (filter.accept(file)) {
String rootDir = m_fileSelector.getCurrentDirectory().getAbsolutePath();
int rootDirLength = rootDir.length();
String filePath = file.getAbsolutePath();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -