公司要把一些新闻每天备份。想找个FCKeditor能把上传图片按日期分目录。没找到。自己动手改了一下。
java FCKeditor2.4 的上传在 net.fckeditor.connector下的 ConnectorServle类中。用XJad把类反编译了。
修改成下面代码:
package net.fckeditor.connector;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.fckeditor.handlers.CommandHandler;
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 org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ConnectorServlet extends HttpServlet
{
private static final long serialVersionUID = 0xb05049436ea5f077L;
private static final Logger logger = LoggerFactory.getLogger(ConnectorServlet.class);
public ConnectorServlet()
{
}
public void init()
throws ServletException, IllegalArgumentException
{
String s = getServletContext().getRealPath(ConnectorHandler.getDefaultUserFilesPath());
File file = new File(s);
UtilsFile.checkDirAndCreate(file);
logger.info("ConnectorServlet successfully initialized!");
}
public void doGet(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
logger.debug("Entering ConnectorServlet#doGet");
httpservletresponse.setCharacterEncoding("UTF-8");
httpservletresponse.setContentType("application/xml; charset=UTF-8");
httpservletresponse.setHeader("Cache-Control", "no-cache");
PrintWriter printwriter = httpservletresponse.getWriter();
String s = httpservletrequest.getParameter("Command");
String s1 = httpservletrequest.getParameter("Type");
String s2 = httpservletrequest.getParameter("CurrentFolder");
logger.debug("Parameter Command: {}", s);
logger.debug("Parameter Type: {}", s1);
logger.debug("Parameter CurrentFolder: {}", s2);
XmlResponse xmlresponse;
if (!RequestCycleHandler.isEnabledForFileBrowsing(httpservletrequest))
xmlresponse = new XmlResponse(1, "The current user isn't authorized for file browsing!");
else
if (!CommandHandler.isValidForGet(s))
xmlresponse = new XmlResponse(1, "Invalid command specified");
else
if (s1 != null && !ResourceTypeHandler.isValid(s1))
xmlresponse = new XmlResponse(1, "Invalid resource type specified");
else
if (!UtilsFile.isValidPath(s2))
{
xmlresponse = new XmlResponse(1, "Invalid current folder specified");
} else
{
CommandHandler commandhandler = CommandHandler.getCommand(s);
ResourceTypeHandler resourcetypehandler = ResourceTypeHandler.getDefaultResourceType(s1);
String s3 = UtilsFile.constructServerSidePath(httpservletrequest, resourcetypehandler);
s3=s3+"/"+getDate(3);
String s4 = getServletContext().getRealPath(s3);
File file = new File(s4);
UtilsFile.checkDirAndCreate(file);
File file1 = new File(file, s2);
if (!file1.exists())
{
xmlresponse = new XmlResponse(102);
} else
{
xmlresponse = new XmlResponse(commandhandler, resourcetypehandler, s2, UtilsResponse.constructResponseUrl(httpservletrequest, resourcetypehandler, s2, true, ConnectorHandler.isFullUrl()));
if (commandhandler.equals(CommandHandler.GET_FOLDERS))
xmlresponse.setFolders(file1);
else
if (commandhandler.equals(CommandHandler.GET_FOLDERS_AND_FILES))
xmlresponse.setFoldersAndFiles(file1);
else
if (commandhandler.equals(CommandHandler.CREATE_FOLDER))
{
String s5 = UtilsFile.sanitizeFolderName(httpservletrequest.getParameter("NewFolderName"));
logger.debug("Parameter NewFolderName: {}", s5);
File file2 = new File(file1, s5);
byte byte0 = 110;
if (file2.exists())
byte0 = 101;
else
try
{
byte0 = ((byte)(file2.mkdir() ? 0 : 102));
}
catch (SecurityException securityexception)
{
byte0 = 103;
}
xmlresponse.setError(byte0);
}
}
}
printwriter.print(xmlresponse);
printwriter.flush();
printwriter.close();
logger.debug("Exiting ConnectorServlet#doGet");
}
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
throws ServletException, IOException
{
logger.debug("Entering Connector#doPost");
httpservletresponse.setCharacterEncoding("UTF-8");
httpservletresponse.setContentType("text/html; charset=UTF-8");
httpservletresponse.setHeader("Cache-Control", "no-cache");
PrintWriter printwriter = httpservletresponse.getWriter();
String s = httpservletrequest.getParameter("Command");
String s1 = httpservletrequest.getParameter("Type");
String s2 = httpservletrequest.getParameter("CurrentFolder");
logger.debug("Parameter Command: {}", s);
logger.debug("Parameter Type: {}", s1);
logger.debug("Parameter CurrentFolder: {}", s2);
if (Utils.isEmpty(s) && Utils.isEmpty(s2))
{
s = "QuickUpload";
s2 = "/";
}
UploadResponse uploadresponse;
if (!RequestCycleHandler.isEnabledForFileUpload(httpservletrequest))
uploadresponse = new UploadResponse(new Object[] {
Integer.valueOf(203), null, null, "The current user isn't authorized for file upload!"
});
else
if (!CommandHandler.isValidForPost(s))
uploadresponse = new UploadResponse(new Object[] {
Integer.valueOf(1), null, null, "Invalid command specified"
});
else
if (s1 != null && !ResourceTypeHandler.isValid(s1))
uploadresponse = new UploadResponse(new Object[] {
Integer.valueOf(1), null, null, "Invalid resource type specified"
});
else
if (!UtilsFile.isValidPath(s2))
{
uploadresponse = UploadResponse.UR_INVALID_CURRENT_FOLDER;
} else
{
ResourceTypeHandler resourcetypehandler = ResourceTypeHandler.getDefaultResourceType(s1);
String s3 = UtilsFile.constructServerSidePath(httpservletrequest, resourcetypehandler);
s3=s3+"/"+getDate(3);
String s4 = getServletContext().getRealPath(s3);
File file = new File(s4);
UtilsFile.checkDirAndCreate(file);
File file1 = new File(file, s2);
if (!file1.exists())
{
uploadresponse = UploadResponse.UR_INVALID_CURRENT_FOLDER;
} else
{
String s5 = null;
DiskFileItemFactory diskfileitemfactory = new DiskFileItemFactory();
ServletFileUpload servletfileupload = new ServletFileUpload(diskfileitemfactory);
servletfileupload.setHeaderEncoding("UTF-8");
try
{
List list = servletfileupload.parseRequest(httpservletrequest);
FileItem fileitem = (FileItem)list.get(0);
String s6 = UtilsFile.sanitizeFileName(fileitem.getName());
String s7 = FilenameUtils.getName(s6);
String s8 = getDate(0);
String s9 = FilenameUtils.getExtension(s7);
s7 = s8.concat(".").concat(s9.toLowerCase());
if (!ExtensionsHandler.isAllowed(resourcetypehandler, s9))
{
uploadresponse = new UploadResponse(new Object[] {
Integer.valueOf(202)
});
} else
{
File file2 = new File(file1, s7);
for (int i = 1; file2.exists(); i++)
{
s5 = s8.concat("_").concat(String.valueOf(i)).concat(".").concat(s9.toLowerCase());
file2 = new File(file1, s5);
}
if (Utils.isEmpty(s5)){
s2=s2+getDate(3)+"/";
uploadresponse = new UploadResponse(new Object[] {
Integer.valueOf(0), UtilsResponse.constructResponseUrl(httpservletrequest, resourcetypehandler, s2, true, ConnectorHandler.isFullUrl()).concat(s7)
});
String aa=UtilsResponse.constructResponseUrl(httpservletrequest, resourcetypehandler, s2, true, ConnectorHandler.isFullUrl()).concat(s7);
}
else{
uploadresponse = new UploadResponse(new Object[] {
Integer.valueOf(201), UtilsResponse.constructResponseUrl(httpservletrequest, resourcetypehandler, s2, true, ConnectorHandler.isFullUrl()).concat(s5), s5
});
}
if (resourcetypehandler.equals(ResourceTypeHandler.IMAGE) && ConnectorHandler.isSecureImageUploads())
{
if (UtilsFile.isImage(fileitem.getInputStream()))
{
fileitem.write(file2);
} else
{
fileitem.delete();
uploadresponse = new UploadResponse(new Object[] {
Integer.valueOf(202)
});
}
} else
{
fileitem.write(file2);
}
}
}
catch (Exception exception)
{
uploadresponse = new UploadResponse(new Object[] {
Integer.valueOf(203)
});
}
}
}
printwriter.print(uploadresponse);
printwriter.flush();
printwriter.close();
logger.debug("Exiting Connector#doPost");
}
public static final String getDate(int i)
{
String as[] = {
"yyyyMMddHHmmssSSS", "yyyyMMddHHmmss", "yyMMddHHmmss", "yyyyMMdd", "yyyy-MM-dd", "HHmmssSSS", "HHmmss"
};
SimpleDateFormat simpledateformat = null;
try
{
simpledateformat = new SimpleDateFormat(as[i]);
}
catch (Exception exception)
{
exception.printStackTrace();
System.out.print(exception.toString());
return exception.toString();
}
return simpledateformat.format(new Date());
}
}
修改后上传的图片目录是/当天日期/当前时间.jpg 了. 不会出现中文乱码了。
在www.pjwqh.cn基础上修改的。感谢下作者。