FCKeditor中文文件上传乱码解决方案
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
FCKeditor文件上传乱码解决方案
1.找到Connector
找到ConnectorServlet,千万不要弄错了,不是FCKeditor-2.3.jar 当中的ConnectorServlet,而是java-core-2.4.1.jar 中的,
net.fckeditor.connector.ConnectorServlet文件夹下找到ConnectorServlet,反编译,并修正错误。
2.修改web.xml
在web.xml中声明ConnectorServlet,如下,这个就是图片上传的Servlet方法,反编译后的ConnectorServlet可自行选择包来放置
3.增加fckeditor.properties
创建fckeditor.properties文件在你的src目录下面,并且添加以下内容,分别对应“路径”,“方法”以及“FLASH支持格式”
4.解决上传中文名出现乱码问题
在ConnectorServlet找到这行
在下面添加upload.setHeaderEncoding("UTF-8");这句即可(图片中已经是添加过后正确的写法)
5.解决创建中文名的目录时会出现乱码
修改tomcat的server.xml配置
如图所示,在后面加上URIEncoding=”UTF-8”即可
在ConnectorServlet 中找到newFolderStr,对其进行编码转换,如下所示即可
6.解决引用的中文名字的图片不能正常显示
在ConnectorServlet 中找到filename,获取到后缀之后只要用UUID 来对其重命名即可,如下所示即可
以上即为解决Fckeditor图片中文出现乱码问题的解决方案。
------------------------------------------------------------- 附上更正后的ConnectorServlet源代码-------------------------------------------------------------
package com.telehot.fckeditor.connector;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mandHandler;
import net.fckeditor.handlers.ConnectorHandler;
import net.fckeditor.handlers.ExtensionsHandler;
import net.fckeditor.handlers.RequestCycleHandler;
import net.fckeditor.handlers.ResourceTypeHandler;
import net.fckeditor.response.UploadResponse;
import net.fckeditor.response.XmlResponse;
import net.fckeditor.tool.Utils;
import net.fckeditor.tool.UtilsFile;
import net.fckeditor.tool.UtilsResponse;
import mons.fileupload.FileItem;
import mons.fileupload.FileItemFactory;
import mons.fileupload.disk.DiskFileItemFactory;
import mons.fileupload.servlet.ServletFileUpload;
import mons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ConnectorServlet extends HttpServlet
{
private static final long serialVersionUID = -5742008970929377161L;
private static final Logger logger = LoggerFactory.getLogger(ConnectorServlet.class);
public void init()
throws ServletException, IllegalArgumentException
{
String realDefaultUserFilesPath = getServletContext().getRealPath(ConnectorHandler.getDefaultUserFilesPath());
File defaultUserFilesDir = new File(realDefaultUserFilesPath);
UtilsFile.checkDirAndCreate(defaultUserFilesDir);
("ConnectorServlet successfully initialized!");
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
logger.debug("Entering ConnectorServlet#doGet");
response.setCharacterEncoding("UTF-8");
response.setContentType("application/xml; charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
String commandStr = request.getParameter("Command");
String typeStr = request.getParameter("Type");
String currentFolderStr = request.getParameter("CurrentFolder");
logger.debug("Parameter Command: {}", commandStr);
logger.debug("Parameter Type: {}", typeStr);
logger.debug("Parameter CurrentFolder: {}", currentFolderStr);
XmlResponse xr;
if (!RequestCycleHandler.isEnabledForFileBrowsing(request)) {
xr = new XmlResponse(1, "The current user isn't authorized for file browsing!");
}
else
{
if (!CommandHandler.isValidForGet(commandStr)) {
xr = new XmlResponse(1, "Invalid command specified");
}
else
{
if ((typeStr != null) && (!ResourceTypeHandler.isValid(typeStr))) {
xr = new XmlResponse(1, "Invalid resource type specified");
}
else
{
if (!UtilsFile.isValidPath(currentFolderStr)) {
xr = new XmlResponse(1, "Invalid current folder specified");
} else {
CommandHandler command = CommandHandler.getCommand(commandStr);
ResourceTypeHandler resourceType = ResourceTypeHandler.getDefaultResourceType(typeStr);
String typePath = UtilsFile.constructServerSidePath(request, resourceType);
String typeDirPath = getServletContext().getRealPath(typePath);
File typeDir = new File(typeDirPath);
UtilsFile.checkDirAndCreate(typeDir);
File currentDir = new File(typeDir, currentFolderStr);
if (!currentDir.exists()) {
xr = new XmlResponse(102);
}
else {
xr = new XmlResponse(command, resourceType, currentFolderStr, UtilsResponse.constructResponseUrl(request, resourceType, currentFolderStr, true, ConnectorHandler.isFullUrl()));
if (command.equals(CommandHandler.GET_FOLDERS)) {
xr.setFolders(currentDir);
} else if (command.equals(CommandHandler.GET_FOLDERS_AND_FILES)) {
xr.setFoldersAndFiles(currentDir);
} else if (command.equals(CommandHandler.CREATE_FOLDER)) {
String tempStr = request.getParameter("NewFolderName");
tempStr = new String(tempStr.getBytes("ISO8859-1"), "UTF-8");
String newFolderStr = UtilsFile.sanitizeFolderName(tempStr);
logger.debug("Parameter NewFolderName: {}", newFolderStr);
File newFolder = new File(currentDir, newFolderStr);
int errorNumber = 110;
if (newFolder.exists())
errorNumber = 101;
else {
try {
errorNumber = newFolder.mkdir() ? 0 : 102;
}
catch (SecurityException e) {
errorNumber = 103;
}
}
xr.setError(errorNumber);
}
}
}
}
}
}
out.print(xr);
out.flush();
out.close();
logger.debug("Exiting ConnectorServlet#doGet");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
logger.debug("Entering Connector#doPost");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
String commandStr = request.getParameter("Command");
String typeStr = request.getParameter("Type");
String currentFolderStr = request.getParameter("CurrentFolder");
logger.debug("Parameter Command: {}", commandStr);
logger.debug("Parameter Type: {}", typeStr);
logger.debug("Parameter CurrentFolder: {}", currentFolderStr);
if ((Utils.isEmpty(commandStr)) && (Utils.isEmpty(currentFolderStr))) {
commandStr = "QuickUpload";
currentFolderStr = "/";
}
UploadResponse ur;
if (!RequestCycleHandler.isEnabledForFileUpload(request)) {
ur = new UploadResponse(new Object[] { new Integer(203), null, null, "The current user isn't authorized for file upload!" });
}
else
{
if (!CommandHandler.isValidForPost(commandStr)) {
ur = new UploadResponse(new Object[] { new Integer(1), null, null, "Invalid command specified" });
}
else
{
if ((typeStr != null) && (!ResourceTypeHandler.isValid(typeStr))) {
ur = new UploadResponse(new Object[] { new Integer(1), null, null, "Invalid resource type specified" });
}
else
{
if (!UtilsFile.isValidPath(currentFolderStr)) {
ur = UploadResponse.UR_INVALID_CURRENT_FOLDER;
} else {
ResourceTypeHandler resourceType = ResourceTypeHandler.getDefaultResourceType(typeStr);
String typePath = UtilsFile.constructServerSidePath(request, resourceType);
String typeDirPath = getServletContext().getRealPath(typePath);
File typeDir = new File(typeDirPath);
UtilsFile.checkDirAndCreate(typeDir);
File currentDir = new File(typeDir, currentFolderStr);
if (!currentDir.exists()) {
ur = UploadResponse.UR_INVALID_CURRENT_FOLDER;
}
else {
String newFilename = null;
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
try
{
List items = upload.parseRequest(request);
FileItem uplFile = (FileItem)items.get(0);
String rawName = UtilsFile.sanitizeFileName(uplFile.getName());
String filename = FilenameUtils.getName(rawName);
String baseName = FilenameUtils.removeExtension(filename);
String extension = FilenameUtils.getExtension(filename);
filename = UUID.randomUUID().toString() + "." + extension;
if (!ExtensionsHandler.isAllowed(resourceType, extension)) {
ur = new UploadResponse(new Object[] { new Integer(202) });
}
else
{
File pathToSave = new File(currentDir, filename);
int counter = 1;
while (pathToSave.exists()) {
newFilename = baseName.concat("(").concat(String.valueOf(counter)).concat(")").concat(".").concat(extension);
pathToSave = new File(currentDir, newFilename);
counter++;
}
if (Utils.isEmpty(newFilename)) {
ur = new UploadResponse(new Object[] { new Integer(0), UtilsResponse.constructResponseUrl(request, resourceType, currentFolderStr, true, ConnectorHandler.isFullUrl()).concat(filename) });
}
else
{
ur = new UploadResponse(new Object[] { new Integer(201), UtilsResponse.constructResponseUrl(request, resourceType, currentFolderStr, true,
ConnectorHandler.isFullUrl()).concat(newFilename), newFilename });
}
if ((resourceType.equals(ResourceTypeHandler.IMAGE)) && (ConnectorHandler.isSecureImageUploads()))
{
if (UtilsFile.isImage(uplFile.getInputStream())) {
uplFile.write(pathToSave);
} else {
uplFile.delete();
ur = new UploadResponse(new Object[] { new Integer(202) });
}
}
else uplFile.write(pathToSave);
}
}
catch (Exception e)
{
ur = new UploadResponse(new Object[] { new Integer(203) });
}
}
}
}
}
}
out.print(ur);
out.flush();
out.close();
logger.debug("Exiting Connector#doPost");
}
}。