最近研究FCKeditor,发现FCKeditor是不错的Html文本编辑器,可以让Web 程序拥有如 word 这样的编辑功能。但人无完人,事无完事,在实际应用中,还需求一些修改。比如,上传的图片,flash 等要按日期生成目录、上传的文件自动生成新的文件名。
(一)ConnectorServlet
(1) public void init() throws ServletException {
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMdd");
baseDir=getInitParameter("baseDir");
debug=(new Boolean(getInitParameter("debug"))).booleanValue();
if(baseDir==null)
//按日期生新目录
baseDir="/UserFiles/"+dateFormat.format(new Date())+"/";
String realBaseDir=getServletContext().getRealPath(baseDir);
File baseFile=new File(realBaseDir);
if(!baseFile.exists()){
baseFile.mkdir();
}
}
(2)
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (debug) System.out.println("--- BEGIN DOPOST ---");
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");
String currentPath=baseDir+typeStr+currentFolderStr;
String currentDirPath=getServletContext().getRealPath(currentPath);
if (debug) System.out.println(currentDirPath);
String retVal="0";
String newName="";
if(!commandStr.equals("FileUpload"))
retVal="203";
else {
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);
//生成新的文件名
fileName=System.currentTimeMillis()+"."+ext;
File pathToSave=new File(currentDirPath,fileName);
//新的文件名不会重复,故删除判断是否重复的代码
// int counter=1;
// while(pathToSave.exists()){
// newName=nameWithoutExt+"("+counter+")"+"."+ext;
// retVal="201";
// pathToSave=new File(currentDirPath,newName);
// counter++;
// }
uplFile.write(pathToSave);
}catch (Exception ex) {
retVal="203";
}
}
out.println("<script type=\"text/javascript\">");
out.println("window.parent.frames['frmUpload'].OnUploadCompleted("+retVal+",'"+newName+"');");
out.println("</script>");
out.flush();
out.close();
if (debug) System.out.println("--- END DOPOST ---");
}
(二)SimpleUploaderServlet
public void init() throws ServletException {
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMdd");
debug=(new Boolean(getInitParameter("debug"))).booleanValue();
if(debug) System.out.println("\r\n---- SimpleUploaderServlet initialization started ----");
baseDir=getInitParameter("baseDir");
enabled=(new Boolean(getInitParameter("enabled"))).booleanValue();
if(baseDir==null)
//按日期生新目录
baseDir="/UserFiles/"+dateFormat.format(new Date())+"/";
String realBaseDir=getServletContext().getRealPath(baseDir);
File baseFile=new File(realBaseDir);
if(!baseFile.exists()){
baseFile.mkdir();
}
allowedExtensions = new Hashtable(3);
deniedExtensions = new Hashtable(3);
allowedExtensions.put("File",stringToArrayList(getInitParameter("AllowedExtensionsFile")));
deniedExtensions.put("File",stringToArrayList(getInitParameter("DeniedExtensionsFile")));
allowedExtensions.put("Image",stringToArrayList(getInitParameter("AllowedExtensionsImage")));
deniedExtensions.put("Image",stringToArrayList(getInitParameter("DeniedExtensionsImage")));
allowedExtensions.put("Flash",stringToArrayList(getInitParameter("AllowedExtensionsFlash")));
deniedExtensions.put("Flash",stringToArrayList(getInitParameter("DeniedExtensionsFlash")));
if(debug) System.out.println("---- SimpleUploaderServlet initialization completed ----\r\n");
}