FCK图片默认情况下上传到程序目录中,这样在重新发布应用程序的时候就有可能丢掉已经上传的图片文件,这里做了小小的修改,将数据和程序分开。
1.修改web.xml文件,添加查看图片文件的Servlet
<servlet> <servlet-name>fckFileView</servlet-name> <servlet-class> com.fredck.FCKeditor.view.FckViewFileController </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping><!-- ”图像属性“页面预览 --> <servlet-name>fckFileView</servlet-name> <url-pattern>/fckeditor/editor/dialog/fck/fckViewFile</url-pattern> </servlet-mapping> <servlet-mapping><!-- fck编辑器内面显示图像 --> <servlet-name>fckFileView</servlet-name> <url-pattern>/fckeditor/fck/fckViewFile</url-pattern> </servlet-mapping> <servlet-mapping><!-- 提交后查看显示图像 --> <servlet-name>fckFileView</servlet-name> <url-pattern>/fck/fckViewFile</url-pattern> </servlet-mapping>
FCK原来是把ContextPath写死在代码中,我这里采用相对路径的方式来处理,所以将上面3个URL都映射到fckFileView上。
对与第3个映射,需要项目中url格式一致的支持,http://localhost:8080/contextpath/modelname/url.do?...
如果不一致,则还要添加很多,因为使用的是相对路径,不过可以用正则表达式来实现。
2.修改文件/fckeditor/editor/filemanager/browser/default/frmresourceslist.html的OpenFile方法
将原来的window.top.opener.SetUrl( encodeURI( fileUrl ).replace( '#', '%23' ) ) ;
替换为新的URL,如下:
var url = "../fck/fckViewFile?method=view&encoder=URLEncoder&type=Image&filePath=" + encodeURI(encodeURI(fileUrl));
window.top.opener.SetUrl(url);
3.修改上传文件的Servlet:com.fredck.FCKeditor.uploader.SimpleUploaderServlet,将上传的文件保存到web发布目录外。
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
String typeStr = request.getParameter("Type");
String saveToFilePath = baseDir + typeStr;
String retVal = "0";
String newName = "";
String fileUrl = "";
String errorMessage = "";
if (enabled) {
DiskFileUpload upload = new DiskFileUpload();
try {
List items = upload.parseRequest(request);
Map fields = new HashMap();
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
fields.put(item.getFieldName(), item.getString());
} else {
fields.put(item.getFieldName(), item);
}
}
FileItem uplFile = (FileItem) fields.get("NewFile");
String fileNameLong = uplFile.getName();
fileNameLong = fileNameLong.replace('\\', '/');
String[] pathParts = fileNameLong.split("/");
String fileName = pathParts[pathParts.length - 1];
String nameWithoutExt = getNameWithoutExtension(fileName);
String ext = getExtension(fileName);
File pathToSave = new File(saveToFilePath, fileName);
fileUrl = "../fck/fckViewFile?method=view&encoder=URLEncoder&type=" + typeStr + "&filePath=";
if (extIsAllowed(typeStr, ext)) {
int counter = 1;
while (pathToSave.exists()) {
newName = nameWithoutExt + "(" + counter + ")" + "." + ext;
retVal = "201";
pathToSave = new File(saveToFilePath, newName);
counter++;
}
uplFile.write(pathToSave);
String filePathBase64 = getFilePath(fileName, newName, typeStr);
fileUrl += filePathBase64;
} else {
retVal = "202";
errorMessage = "";
if (log.isDebugEnabled())
log.debug("Invalid file type: " + ext);
}
} catch (Exception ex) {
retVal = "203";
}
} else {
retVal = "1";
errorMessage = "This file uploader is disabled. Please check the WEB-INF/web.xml file";
}
out.println("<script type=\"text/javascript\">");
out.println("window.parent.OnUploadCompleted(" + retVal + ",'" + fileUrl + "','" + newName + "','" + errorMessage + "');");
out.println("</script>");
out.flush();
out.close();
}
4.修改浏览文件的Servlet:com.fredck.FCKeditor.connector.ConnectorServlet
将doGet方法中的
Node root = CreateCommonXml(document, commandStr, typeStr, currentFolderStr, request.getContextPath() + currentPath);
改为
Node root = CreateCommonXml(document, commandStr, typeStr, currentFolderStr, currentPath);
doPost方法也需要改动,修改文件的保存路径,去掉代码
String currentDirPath = getServletContext().getRealPath(currentPath);
5.添加查看文件的Servlet:com.fredck.FCKeditor.view.FckViewFileController
package com.fredck.FCKeditor.view;
import java.io.*;
import java.net.URLDecoder;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import sun.misc.BASE64Decoder;
public class FckViewFileController extends HttpServlet {
private static final long serialVersionUID = 5273052908966930616L;
private static final BASE64Decoder decoder = new BASE64Decoder();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String filePath = req.getParameter("filePath");
System.out.println(filePath);
String encodeURL = req.getParameter("encoder");
if ("URLEncoder".equals(encodeURL)) {
filePath = decodeKeyWord(filePath);
} else {
filePath = new String(decoder.decodeBuffer(filePath));
}
InputStream ism = new FileInputStream(filePath);
byte[] bytes = new byte[2048];
int readLenGth = 0;
OutputStream osm = resp.getOutputStream();
while ((readLenGth = ism.read(bytes)) > 0) {
osm.write(bytes, 0, readLenGth); // 向浏览器输出
}
osm.flush();
//super.doPost(req, resp);
}
private String decodeKeyWord(String keyword) throws UnsupportedEncodingException {
return URLDecoder.decode(keyword, "utf-8");
}
}