UEditor使用细节注意

原理:先加载配置,后上传,最后回显。

UEditor给出了controller.jsp来进行加载配置,并上传,但是这些不是我们想要的。我们需要重写加载配置和上传回显操作。

serverUrl是我们需要修改的地方,它的作用是让我们加载配置。

这里我将serverUrl设置为:serverUrl: ROOT_PATH + "ueditor/upload"

upload方法(给出整段代码):

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.google.common.collect.Maps;

@Controller
@RequestMapping("/ueditor")
public class UeditorController extends BasicController{
	
	@RequestMapping
	public String index(){
		System.out.println(1);
		return "/ueditor/index";
	}
	
	@RequestMapping(value = "/upload", method = RequestMethod.GET)  
    @ResponseBody  
    public void upload(HttpServletRequest request, HttpServletResponse response) throws IOException {  
        String action = request.getParameter("action");  
        if ("config".equals(action)) {  
            OutputStream os = response.getOutputStream();  
            IOUtils.copy(UeditorController.class.getClassLoader().getResourceAsStream("config.json"), os);  
        }
    }
    
    @RequestMapping(value = "/upload", method = RequestMethod.POST)  
    @ResponseBody  
    public Map<String, String> upload(HttpServletRequest request,@RequestParam CommonsMultipartFile upfile) throws IOException {  
    	Map<String, String> result = Maps.newHashMap();
    	System.out.println(upfile.getFileItem().getFieldName());
    	String path = getFilePath(upfile);
    	File file = new File(path);
    	System.out.println(path);
    	String state = "SUCCESS";
        //返回类型  
    	String rootPath = request.getContextPath();
    	result.put("url", rootPath + "/ueditor/show?filePath=" + path);
        result.put("size", String.valueOf(file.length()));  
        result.put("type", file.getName().substring(file.getName().lastIndexOf(".")));  
        result.put("state", state);  
        return result;  
    }
    
    @RequestMapping(value = "/show", method = RequestMethod.GET)
    public void show(String filePath, HttpServletResponse response) throws IOException {
        
        File file = getFile(filePath);

        response.setDateHeader("Expires", System.currentTimeMillis() + 1000 * 60 * 60 * 24);
        response.setHeader("Cache-Control", "max-age=60");
        OutputStream os = response.getOutputStream();

        FileInputStream is = null;
        try {
            is = new FileInputStream(file);
            IOUtils.copy(is, os);
        } catch (FileNotFoundException e) {
            response.setStatus(404);
            return;
        } finally {
            if (null != is) {
                is.close();
            }
            if (null != os) {
                os.flush();
                os.close();
            }
        }
    }
    
    protected String getFilePath(CommonsMultipartFile uploadFile){
    	String absolutePath = "D:/upload";
    	File folder = new File(absolutePath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        String rawName = uploadFile.getFileItem().getName();
        String fileExt = rawName.substring(rawName.lastIndexOf("."));
        String newName = System.currentTimeMillis() + UUID.randomUUID().toString() + fileExt;
        File saveFile = new File(absolutePath + File.separator + newName);
        try {
            uploadFile.getFileItem().write(saveFile);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return absolutePath + "/" + newName;
    }
    
    protected File getFile(String path){
    	File file = new File(path);
    	return file;
    	
    }
    
}
config.json,将这个配置放在resources里面。

需要注意的是,上传的那段代码,由于config.json中:
 "imageFieldName": "upfile", /* 提交的图片表单名称 */

iageFiledName给的是upfile,所以我这里要写upfile,不然就会出错。

其实只要我们好好的理解ActionEnter具体是怎么实现的就行。

你可能感兴趣的:(ueditor)