1.webwork中的rechtexteditor图片上传时在使用过程中可能会出现如下问题:
Error on file upload.Error number:203
Invalid file
XML request error:Not Found(404)
同时在创建中文多级目录时候,文件目录为乱码,上传含中文名称图片为乱码,
需要对webwork源码进行修改配置来解决上述出现的问题。
2.\webwork-2.2.4-sources\com\opensymphony\webwork\static\richtexteditor\拷贝到工程的\webapp\webwork\richtexteditor\目录下面(如果\webwork\richtexteditor\目录不存在,手动建立),同时在webapp目录下面手动建立\upload\Image\文件夹,用来保存上传的图片
主要对\webwork-2.2.4-sources\~\static\richtexteditor\editor\filemanager\browser\default\下面源文件修改,
目标:上传图片名称里面含中文避免乱码
位置:\webapp\webwork\richtexteditor\editor\filemanager\browser\default\frmupload.html
添加代码:<meta http-equiv="content-type" content="text/html; charset=UTF-8">
目标:创建目录名称里面含中文避免乱码
位置:\webapp\webwork\richtexteditor\editor\filemanager\browser\default\frmcreatefolder.html
代码:oConnector.SendCommand( 'CreateFolder', 'NewFolderName=' + escape( sFolderName) , CreateFolderCallBack ) ;
换成:oConnector.SendCommand( 'CreateFolder', 'NewFolderName=' + sFolderName , CreateFolderCallBack ) ;
位置:\webapp\webwork\richtexteditor\editor\filemanager\browser\default\browser.html
代码:
oConnector.SendCommand = function( command, params, callBackFunction )
{
var sUrl = this.ConnectorUrl + 'Command=' + command ;
sUrl += '&Type=' + this.ResourceType ;
sUrl += '&CurrentFolder=' + escape( this.CurrentFolder ) ;
if ( params ) sUrl += '&' + params ;
var oXML = new FCKXml() ;
if ( callBackFunction )
oXML.LoadUrl( sUrl, callBackFunction ) ; // Asynchronous load.
else
return oXML.LoadUrl( sUrl ) ;
}
换成:
oConnector.SendCommand = function( command, params, callBackFunction )
{
var sUrl = this.ConnectorUrl + 'Command=' + command ;
sUrl += '&Type=' + this.ResourceType ;
sUrl += '&CurrentFolder=' + this.CurrentFolder ;
if ( params ) sUrl += '&' + params ;
var oXML = new FCKXml() ;
if ( callBackFunction )
oXML.LoadUrl( sUrl, callBackFunction ) ; // Asynchronous load.
else
return oXML.LoadUrl( sUrl ) ;
}
3.修改webwork.properties文件,使用当前的端口和\webapp\webwork\文件夹
### used to build URLs, such as the UrlTag webwork.url.http.port = 8081 ###false not to read webwork.jar.but to read the webwork folder under ther website webwork.serve.static=false
4.在src目录下面建立4个java文件,如:
org.RichtexteditorConnector,
org.RichtexteditorCreateFolderResultLocal,
org.RichtexteditorGetFoldersAndFilesResultLocal,
org.RichtexteditorGetFoldersResultLocal
代码(org.RichtexteditorConnector):
/** * @author hjl * @date 2009-4-17 * @function * @version */ public class RichtexteditorConnector extends DefaultRichtexteditorConnector { private static final long serialVersionUID = 1L; protected String calculateActualServerPath(String actualServerPath, String type, String folderPath) throws Exception {//解决不能创建目录问题 String path = StringUtils .replaceChars("file:///" + servletContext.getRealPath("/" + actualServerPath), '\\', '/'); makeDirIfNotExists(path); path = path.endsWith("/") ? path : path + "/"; return path + type + folderPath; } /** * 创建中文目录时转码 */ @Override protected CreateFolderResult createFolder(String virtualFolderPath, String type, String newFolderName) { try { String tmpPath = calculateActualServerPath(getActualServerPath(), type, virtualFolderPath); tmpPath = tmpPath+newFolderName; //dir 中文 tmpPath = new String(tmpPath.getBytes("ISO-8859-1"),"gbk"); // System.out.println("tmpPath:"+tmpPath); boolean alreadyExists = makeDirIfNotExists(tmpPath); if (alreadyExists) { return CreateFolderResult.folderAlreadyExists(); } } catch(Exception e) { return CreateFolderResult.unknownError(); } return CreateFolderResult.noErrors(); } /** * 中文文件夹显示转码 */ @Override protected FoldersAndFiles getFoldersAndFiles(String virtualFolderPath, String type) throws Exception { String path = calculateActualServerPath(getActualServerPath(), type, virtualFolderPath); //dir 中文 path = new String(path.getBytes("ISO-8859-1"),"gbk"); makeDirIfNotExists(path); java.io.File f = new java.io.File(new URI(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) { e.printStackTrace(); } } } return new FoldersAndFiles( (Folder[]) directories.toArray(new Folder[0]), (File[]) files.toArray(new File[0]) ); } @Override protected Folder[] getFolders(String virtualFolderPath, String type) throws Exception { String path = calculateActualServerPath(getActualServerPath(), type, virtualFolderPath); //dir 中文 path = new String(path.getBytes("ISO-8859-1"),"gbk"); makeDirIfNotExists(path); java.io.File f = new java.io.File(new URI(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]); } @Override protected FileUploadResult fileUpload(String virtualFolderPath, String type, String filename, String contentType, java.io.File newFile) { try { virtualFolderPath = new String(virtualFolderPath.getBytes("ISO-8859-1"),"UTF-8"); 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(new URI(tmpFile))); return FileUploadResult.uploadCompleteWithFilenamChanged(filename+ext); } else { copyFile(newFile, new java.io.File(new URI(tmpFile))); return FileUploadResult.uploadComplete(); } } catch(Exception e) { return FileUploadResult.invalidFile(); } } private ServletContext servletContext; public void setServletContext(ServletContext servletContext) { this.servletContext = servletContext; } }
代码(org.RichtexteditorCreateFolderResultLocal):
/** * @author hjl * @date 2009-4-20 * @function * @version */ public class RichtexteditorCreateFolderResultLocal extends RichtexteditorCreateFolderResult { @Override public void execute(ActionInvocation invocation) throws Exception { HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("text/xml; charset=UTF-8"); response.setHeader("Cache-Control","no-cache"); OutputStream os = response.getOutputStream(); CreateFolderResult createFolderResult = richtexteditorCreateFolderResult(invocation); // CurrentFolder问题,转换为中文 String currentFolder_path = getFolderPath(invocation); currentFolder_path = new String(currentFolder_path.getBytes("ISO-8859-1"),"GBK"); String currentFolder_url = getServerPath(invocation); currentFolder_url = new String(currentFolder_url.getBytes("ISO-8859-1"),"GBK"); Document document = buildDocument(); Element root = buildCommonResponseXml(document, getCommand(invocation), getType(invocation), currentFolder_path, currentFolder_url); Element errorElement = document.createElement("Error"); errorElement.setAttribute("number", createFolderResult.getCode()); root.appendChild(errorElement); // if (_log.isDebugEnabled()) { // String result = stringFromDocument(document); // _log.debug(result); // } writeDocumentToStream(document, os); os.flush(); os.close(); } }
代码(org.RichtexteditorGetFoldersAndFilesResultLocal):
/** * @author hjl * @date 2009-4-20 * @function * @version */ public class RichtexteditorGetFoldersAndFilesResultLocal extends RichtexteditorGetFoldersAndFilesResult { /** * */ private static final long serialVersionUID = 1L; @Override public void execute(ActionInvocation invocation) throws Exception { // System.out.println("====本地===="); HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("text/xml; charset=UTF-8"); response.setHeader("Cache-Control","no-cache"); OutputStream os = response.getOutputStream(); Document document = buildDocument(); //CurrentFolder问题,转换为中文 String currentFolder_path = getFolderPath(invocation); currentFolder_path = new String(currentFolder_path.getBytes("ISO-8859-1"),"GBK"); String currentFolder_url = getServerPath(invocation); currentFolder_url = new String(currentFolder_url.getBytes("ISO-8859-1"),"GBK"); // Element root = buildCommonResponseXml(document, // getCommand(invocation), getType(invocation), // getFolderPath(invocation), getServerPath(invocation)); //buildCommonResponseXml这个方法是关键 MAIN() Element root = buildCommonResponseXml(document, getCommand(invocation), getType(invocation), currentFolder_path, currentFolder_url); FoldersAndFiles foldersAndFiles = richtexteditorFoldersAndFiles(invocation); Folder[] folders = foldersAndFiles.getFolders(); File[] files = foldersAndFiles.getFiles(); Element foldersElement = document.createElement("Folders"); if (folders != null) { for (int a=0; a< folders.length; a++) { Element folderElement = document.createElement("Folder"); folderElement.setAttribute("name", folders[a].getFoldername()); foldersElement.appendChild(folderElement); } } root.appendChild(foldersElement); Element filesElement = document.createElement("Files"); if (files != null) { for (int a=0; a< files.length; a++) { Element fileElement = document.createElement("File"); fileElement.setAttribute("name", files[a].getFilename()); fileElement.setAttribute("size", String.valueOf(files[a].getSizeInKb())); filesElement.appendChild(fileElement); } } root.appendChild(filesElement); // if (_log.isDebugEnabled()) { // String result = stringFromDocument(document); // _log.debug(result); // } writeDocumentToStream(document, os); os.flush(); os.close(); } }
代码(org.RichtexteditorGetFoldersResultLocal):
/** * @author hjl * @date 2009-4-20 * @function * @version */ public class RichtexteditorGetFoldersResultLocal extends RichtexteditorGetFoldersResult { @Override public void execute(ActionInvocation invocation) throws Exception { Folder[] folders = richtexteditorFolders(invocation); HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("text/xml; charset=UTF-8"); response.setHeader("Cache-Control","no-cache"); OutputStream os = response.getOutputStream(); Document document = buildDocument(); // CurrentFolder问题,转换为中文 String currentFolder_path = getFolderPath(invocation); currentFolder_path = new String(currentFolder_path.getBytes("ISO-8859-1"),"GBK"); String currentFolder_url = getServerPath(invocation); currentFolder_url = new String(currentFolder_url.getBytes("ISO-8859-1"),"GBK"); Element root = buildCommonResponseXml(document, getCommand(invocation), getType(invocation), currentFolder_path, currentFolder_url); Element foldersElement = document.createElement("Folders"); if (folders != null) { for (int a=0; a< folders.length; a++) { Element folderElement = document.createElement("Folder"); folderElement.setAttribute("name", folders[a].getFoldername()); foldersElement.appendChild(folderElement); } } root.appendChild(foldersElement); // if (_log.isDebugEnabled()) { // String result = stringFromDocument(document); // _log.debug(result); // } writeDocumentToStream(document, os); os.flush(); os.close(); } }
5.在xwork.xml文件中添加如下代码:
<package name="richtexteditor-browse" extends="webwork-default" namespace="/webwork/richtexteditor/editor/filemanager/browser/default/connectors/jsp"> <result-types> <result-type name="richtexteditorGetFolders" class="org.RichtexteditorGetFoldersResultLocal" /> <result-type name="richtexteditorGetFoldersAndFiles" class="org.RichtexteditorGetFoldersAndFilesResultLocal" /> <result-type name="richtexteditorCreateFolder" class="org.RichtexteditorCreateFolderResultLocal" /> </result-types> <action name="connector" class="org.RichtexteditorConnector" method="browse"> <param name="actualServerPath"> /upload/ </param> <param name="serverPath"> /upload/ </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="org.RichtexteditorConnector" method="upload"> <param name="actualServerPath"> /upload/ </param> <param name="serverPath"> /upload/ </param> <result name="richtexteditorFileUpload" /> </action> </package>
6.同时pom.xml jar必须有:
<dependencies> <dependency> <groupId>opensymphony</groupId> <artifactId>webwork</artifactId> <version>2.2.4</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.0-rc2</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.1</version> </dependency> </dependencies>
最后在ftl页面中显示出来:
<@ww.richtexteditor defaultLanguage ="zh-cn" toolbarCanCollapse="false" label="内容(content):" height ="360" name="model.content"/>
一步一步配置就可以将问题解决了。