先说怎么做,再稍微解释一下为什么这么做
1.webwork.properties里面设置
webwork.serve.static=false
webwork.multipart.parser=cos
2.在webapp的根目录下建一个文件夹webwork,把webwork.jar里面的/com/opensymphony/webwork/static和/template里面的文件和复制到自己建的webwork文件夹里面,在/webwork/richtexteditor里面建文件夹data(可能会自动建,没测试过)
3.写一个RichtexteditorConnector
package test;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.opensymphony.webwork.components.DefaultRichtexteditorConnector;
public class RichtexteditorConnector extends DefaultRichtexteditorConnector {
public static final Log _log = LogFactory
.getLog(RichtexteditorConnector.class);
private ServletContext servletContext;
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
protected String calculateActualServerPath(String actualServerPath,
String type, String folderPath) throws Exception {
String path = servletContext.getRealPath(actualServerPath);
path = path.replace('\\', '/');
makeDirIfNotExists(path);
path = path.endsWith("/") ? path : path + "/";
return path + type + folderPath;
}
protected Folder[] getFolders(String virtualFolderPath, String type)
throws Exception {
String path = calculateActualServerPath(getActualServerPath(), type,
virtualFolderPath);
makeDirIfNotExists(path);
java.io.File f = new java.io.File(path);
java.io.File[] children = f.listFiles(new FileFilter() {
public boolean accept(java.io.File pathname) {
if (!pathname.isFile()) {
return true;
}
return false;
}
});
List tmpFolders = new ArrayList();
for (int a = 0; a < children.length; a++) {
tmpFolders.add(new Folder(children[a].getName()));
}
return (Folder[]) tmpFolders.toArray(new Folder[0]);
}
protected FoldersAndFiles getFoldersAndFiles(String virtualFolderPath,
String type) throws Exception {
String path = calculateActualServerPath(getActualServerPath(), type,
virtualFolderPath);
makeDirIfNotExists(path);
java.io.File f = new java.io.File(path);
java.io.File[] children = f.listFiles();
List directories = new ArrayList();
List files = new ArrayList();
for (int a = 0; a < children.length; a++) {
if (children[a].isDirectory()) {
directories.add(new Folder(children[a].getName()));
} else {
try {
files.add(new File(children[a].getName(),
fileSizeInKBytes(children[a])));
} catch (Exception e) {
_log.error("cannot deal with file " + children[a], e);
}
}
}
return new FoldersAndFiles((Folder[]) directories
.toArray(new Folder[0]), (File[]) files.toArray(new File[0]));
}
protected FileUploadResult fileUpload(String virtualFolderPath,
String type, String filename, String contentType,
java.io.File newFile) {
try {
String tmpDir = calculateActualServerPath(getActualServerPath(),
type, virtualFolderPath);
makeDirIfNotExists(tmpDir);
String tmpFile = tmpDir + filename;
if (makeFileIfNotExists(tmpFile)) {
// already exists
int a = 0;
String ext = String.valueOf(a);
tmpFile = calculateActualServerPath(getActualServerPath(),
type, virtualFolderPath)
+ filename + ext;
while (makeFileIfNotExists(tmpFile)) {
a = a + 1;
ext = String.valueOf(a);
if (a > 100) {
return FileUploadResult.invalidFile();
}
}
copyFile(newFile, new java.io.File(tmpFile));
return FileUploadResult
.uploadCompleteWithFilenamChanged(filename + ext);
} else {
copyFile(newFile, new java.io.File(tmpFile));
return FileUploadResult.uploadComplete();
}
} catch (Exception e) {
_log.error(e.toString(), e);
e.printStackTrace();
return FileUploadResult.invalidFile();
}
}
protected void unknownCommand(String command, String virtualFolderPath,
String type, String filename, String contentType,
java.io.File newFile) {
throw new RuntimeException("unknown command " + command);
}
/**
*
* @param path
* @return true if file already exists, false otherwise.
*/
protected boolean makeDirIfNotExists(String path) {
java.io.File dir = new java.io.File(path);
if (!dir.exists()) {
_log.debug("make directory " + dir);
boolean ok = dir.mkdirs();
if (!ok) {
throw new RuntimeException("cannot make directory " + dir);
}
return false;
}
return true;
}
protected boolean makeFileIfNotExists(String filePath) throws IOException {
java.io.File f = new java.io.File(filePath);
if (!f.exists()) {
_log.debug("creating file " + filePath);
boolean ok = f.createNewFile();
if (!ok) {
throw new RuntimeException("cannot create file " + filePath);
}
return false;
}
return true;
}
}
4.xwork.xml加上
<package name="richtexteditor-browse" extends="webwork-default"
namespace="/webwork/richtexteditor/editor/filemanager/browser/default/connectors/jsp">
<action name="connector"
class="test.RichtexteditorConnector"
method="browse">
<param name="actualServerPath">
/webwork/richtexteditor/data
</param>
<result name="getFolders" type="richtexteditorGetFolders" />
<result name="getFoldersAndFiles"
type="richtexteditorGetFoldersAndFiles" />
<result name="createFolder"
type="richtexteditorCreateFolder" />
<result name="fileUpload" type="richtexteditorFileUpload" />
</action>
</package>
<package name="richtexteditor-upload" extends="webwork-default"
namespace="/webwork/richtexteditor/editor/filemanager/upload">
<action name="uploader"
class="test.RichtexteditorConnector"
method="upload">
<param name="actualServerPath">
/webwork/richtexteditor/data
</param>
<result name="richtexteditorFileUpload" />
</action>
</package>
5.配置标签
<ww:form action="test" method="post">
<%request.setAttribute("contextPath",request.getContextPath());%>
<ww:richtexteditor basePath="%{#request.contextPath}/webwork/richtexteditor/" toolbarCanCollapse="false" width="700" label="description" name="content" defaultLanguage="zh-cn" />
<ww:submit value="submit" />
</ww:form>
6.服务器端口设置为80
原因
1.
webwork.serve.static=false
/webwork/*这样的URL是可以直接访问不需要通过webwork,这样做是为了自己可以在里面建文件,并且可以方便的访问这些文件
webwork.multipart.parser=cos
设置为jakarta上传文件不成功,com.opensymphony.webwork.interceptor.FileUploadInterceptor解析MultiPartRequestWrapper不成功,原因不清楚,反正用cos就可以了,记得加上cos.jar
2.因为设置了webwork.serve.static=false需要这样做
3.覆盖webwork的DefaultRichtexteditorConnector,最关键的是覆盖calculateActualServerPath()方法,默认是把文件放到/WEB-INF/classes/com/opensymphony/webwork/static/richtexteditor/data/,我们需要放到/webwork/richtexteditor/data/里面,覆盖其他方法是因为默认对文件的访问都是通过
new File(new URI("file://"+filePath);来访问的,会有些问题,直接new File(filePath)就可以了
4.使用自己的RichtexteditorConnector,并且设置参数actualServerPath,其他参数不要改,webwork默认的是这样
5.basePath必须自己指定不能用默认的,虽然指定的值和默认的是一样,但是不自己指定的话它自动加上jsessionid,如/webwork/richtexteditor/;jsessionid=301gs94034pki/editor/fckeditor.html,因为设置了webwork.serve.static=false,所以服务器不能解析这个url
defaultLanguage="zh-cn",不指定的话中文默认是繁体
6.服务器端口必须为80不能为8080,因为fckeditor链接你上传的文件的时候,不会把端口加上去