接上篇文章,本章節(jié)講述使用java語言的ckeditor服務(wù)器文件瀏覽功能。
// 雖然兩篇文章的代碼都帶有文件類型參數(shù),但是此處不進(jìn)行判斷非法格式,僅以圖片類型為主介紹控件功能的開發(fā)過程。
上篇文章中,在CKEDITOR的圖片選擇頁面,我們增加了一個(gè)瀏覽按鈕
我們希望點(diǎn)擊瀏覽服務(wù)器可以去服務(wù)器直接選擇已經(jīng)通過其他方式上傳的圖片,依舊很痛苦的是,我們沒有那么多的銀子去購買圖片服務(wù)器,所以在應(yīng)用中開辟了特定的區(qū)域來存放圖片,并且采用分文件夾的方式。所以,瀏覽服務(wù)器,就涉及到多級目錄的問題,我們希望如果當(dāng)前的目錄下存在子目錄,那么可以直接進(jìn)入子目錄挑選圖片,并且可以自由的前后操作。
先看看效果:
這樣用戶可以直接在界面上操作圖片,進(jìn)行富文本的編輯。
看看代碼:
- package com.normandy.position.web;
-
- import java.io.File;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.net.URLDecoder;
- import java.net.URLEncoder;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.apache.commons.lang.StringUtils;
- import org.apache.log4j.Logger;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.ModelMap;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
-
- import com.normandy.position.common.NorStringUtils;
-
-
-
-
-
-
-
-
-
-
-
-
- @Controller
- @RequestMapping("/browerServer.do")
- public class FileBrowerController {
- protected final Logger logger = Logger
- .getLogger(FileBrowerController.class);
-
- private static final String FILE_UPLOAD_DIR = "upload";
-
- private static final String FILE_UPLOAD_SUB_IMG_DIR = "img";
-
- private static final String FOR_FREEMARKER_LOAD_DIR = "freemarker";
-
- @RequestMapping(method = RequestMethod.GET)
- public void processBrower(ModelMap modelMap, HttpServletRequest request,
- HttpServletResponse response) {
- processBrowerPost(modelMap, request, response);
- return;
- }
-
- @SuppressWarnings("deprecation")
- @RequestMapping(method = RequestMethod.POST)
- public void processBrowerPost(ModelMap modelMap,
- HttpServletRequest request, HttpServletResponse response) {
-
- String typeStr = request.getParameter("type");
- String floderName = request.getParameter("fo");
-
- if (logger.isDebugEnabled()) {
- logger.debug("瀏覽文件,文件格式:" + typeStr);
- }
-
-
- String realPath = "";
- if(StringUtils.isNotBlank(floderName)){
- floderName = URLDecoder.decode(floderName);
-
- realPath = request.getSession().getServletContext().getRealPath(floderName);
- if(logger.isInfoEnabled()){
- logger.info("sub floder:"+realPath);
- }
- }else if(StringUtils.equalsIgnoreCase(typeStr, "Image")){
-
- realPath = request.getSession().getServletContext().getRealPath(FOR_FREEMARKER_LOAD_DIR+ File.separator+FILE_UPLOAD_DIR+ File.separator+FILE_UPLOAD_SUB_IMG_DIR);
- if(logger.isInfoEnabled()){
- logger.info("default floder:"+realPath);
- }
- }
-
- File folder = new File(realPath);
- if(!folder.exists()){
- return;
- }
-
-
- List<String> subFolderSet = new ArrayList<String>();
-
- List<String> subFileerSet = new ArrayList<String>();
-
- File[] subFiles = folder.listFiles();
- if(null != subFiles && 0 < subFiles.length){
- for(int i=0;i < subFiles.length; i++){
- File _file = subFiles[i];
- if(_file.isDirectory()){
- subFolderSet.add(getDefaultFolderFromFreemarker(_file));
- } else {
- subFileerSet.add(NorStringUtils.getFileName(_file.getName()));
- }
- }
- }
-
- String callback = request.getParameter("CKEditorFuncNum");
- PrintWriter out;
-
- response.setContentType("text/html");
- response.setCharacterEncoding("GB2312");
- try {
- out = response.getWriter();
- out.println("<mce:script type='text/javascript'><!--
- ");
-
-
- out.println("function choose(obj){");
- out.println("window.opener.CKEDITOR.tools.callFunction(" + callback
- + ",obj)");
- out.println("window.close();");
- out.println("}");
-
-
- out.println("function view(obj){window.location.href='browerServer.do?type=image&CKEditorFuncNum="+callback+"&fo='+obj;}");
- out.println("
-
-
-
- out.print("<div style='width:100%;float:left;word-break:break-all;' onclick =view('" + URLEncoder.encode("/freemarker/upload") + "')>");
- out.print("<span>[根目錄]<span/>");
- out.print("</div>");
-
-
- if(StringUtils.isNotBlank(floderName) && !checkIsRoot(folder)){
- String parent = getDefaultFolderFromFreemarker(folder.getParentFile());
- out.print("<div style='width:100%;float:left;word-break:break-all;' onclick =view('" + URLEncoder.encode(parent) + "')>");
- out.print("<span>[上級目錄]"+parent+"<span/>");
- out.print("</div>");
- if(logger.isDebugEnabled()){
- logger.debug("發(fā)現(xiàn)上級目錄:"+ parent);
- }
- }
-
-
- if(0 < subFolderSet.size()){
- Iterator<String> subFolderSetIndex = subFolderSet.iterator();
- while(subFolderSetIndex.hasNext()){
- String ftemp = subFolderSetIndex.next();
-
- out.print("<div style='width:100%;float:left;word-break:break-all;' onclick =view('" + URLEncoder.encode(ftemp) + "')>");
- out.print("<span>[下級目錄]"+ftemp+"</span>");
- out.print("</div>");
-
- if(logger.isDebugEnabled()){
- logger.debug("添加子目錄:"+ftemp);
- }
- }
- }
-
-
- if(0 < subFileerSet.size()){
- Iterator<String> subFileerSetIndex = subFileerSet.iterator();
- while(subFileerSetIndex.hasNext()){
- String ftemp = subFileerSetIndex.next();
- String f = getDefaultFolderFromFreemarker(folder);
- String fileUrl = f + File.separator + ftemp;
- fileUrl = StringUtils.replace(fileUrl, "http://", "/");
-
- out.print("<div style='width:150px;height:150px;float:left;word-break:break-all;padding:5px;background:#666699;margin:5px;'>");
- out.print("<a href="javascript:void(0)" mce_href="javascript:void(0)" onclick=choose('"+fileUrl+"')><img style='border:none;width:145px;height:145px;' src=""+fileUrl+"" mce_src=""+fileUrl+"" title='"+fileUrl+"'/></a>");
- out.print("</div>");
-
- if(logger.isDebugEnabled()){
- logger.debug("添加文件:"+fileUrl);
- }
- }
- }
-
- out.flush();
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
-
-
-
-
- private static String getDefaultFolderFromFreemarker(File folder){
- String path = folder.getPath();
- path = path.substring(path.indexOf(FOR_FREEMARKER_LOAD_DIR));
- return path;
- }
-
-
-
-
-
-
- private static boolean checkIsRoot(File folder){
- String name = NorStringUtils.getFileName(folder.getName());
- return StringUtils.equalsIgnoreCase(name, FILE_UPLOAD_DIR);
- }
- }
這里注意,如果你沒有成功,看第一篇最后部分的部署配置介紹,很簡單的事兒
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點(diǎn)擊舉報(bào)。