FCKeditor2.6中servlet有了一些变化,控制器ConnectorServlet.java和Dispatcher.java是我们解决中文问题的主要修改文件。以前版本都在ConnectorServlet.java中,2.6之后都在Dispatcher.java中了。
而且它的doPost方法也给我们进行了编码转换:
@Override
protected void doPost(final HttpServletRequest request,
final HttpServletResponse response) throws ServletException,
IOException {
request.setCharacterEncoding("UTF-8"); //此处设置编码格式
response.setCharacterEncoding("UTF-8"); //此处设置编码格式
response.setContentType("text/html");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
UploadResponse uploadResponse = null;
try {
ThreadLocalData.beginRequest(request);
uploadResponse = dispatcher.doPost(request);
} catch (Exception e) {
throw new ServletException(e);
} finally {
/*
* call this method to prevent detached requests or else the request
* will probably never be garbage collected and will fill your
* memory
*/
ThreadLocalData.endRequest();
}
out.print(uploadResponse);
out.flush();
out.close();
}
也就是说,在request和response之前都设置了编码格式为“UTF-8”。我们就可以不用再设置了。
所以我们修改另一地方的编码格式,即文件夹名称的编码格式:
在Dispatcher。java中修改:
在它的doGet方法中找到如下代码段:
。。。。。
try {
if (command.equals(Command.CREATE_FOLDER)) {
String newFolderNameStr = request
.getParameter("NewFolderName");
/**************添加此句代码(doGet中我们一般先还原在编码)**********/
newFolderNameStr=new String(newFolderNameStr.getBytes("ISO8859-1"),"UTF-8");
/************************end*********************************/
logger.debug("Parameter NewFolderName: {}",
newFolderNameStr);
String sanitizedNewFolderNameStr = UtilsFile
.sanitizeFolderName(newFolderNameStr);
if (Utils.isEmpty(sanitizedNewFolderNameStr))
getResponse = GetResponse
.getInvalidNewFolderNameError();
else {
。。。。。。。
doPost方法中修改如下:
。。。。。。。
else {
String sanitizedFileName = UtilsFile
.sanitizeFileName(fileName);
/*************添加如下代码(将解决上传中文名图片问题)*************/
String extension= FilenameUtils.getExtension(fileName);
sanitizedFileName=UUID.randomUUID().toString()+"."+extension;
/*********************end***********************/
logger.debug("Parameter NewFile (sanitized): {}",
sanitizedFileName);
String newFileName = connector.fileUpload(type, context
.getCurrentFolderStr(), sanitizedFileName, uplFile
.getInputStream());
String fileUrl = UtilsResponse.fileUrl(RequestCycleHandler
.getUserFilesPath(request), type, context
.getCurrentFolderStr(), newFileName);
if
.............................
配置的类中就主要修改这两处就可以了。