UEditor保存图片时直接通过FTP上传到文件服务器

本文档介绍了如何在UEditor中配置并实现图片上传时直接通过FTP将图片上传到远程文件服务器,避免了因服务器部署环境导致的图片无法显示的问题。通过修改UEditor的配置文件、控制器代码和创建FTP上传类,实现了图片的FTP上传功能。
摘要由CSDN通过智能技术生成

         最近公司的项目使用到百度的UEditor上传图片,开发时候都没有问题,部署的时候用到NGINX,由于UEditor是将图片存储到tomcat下,导致反显时候调用的tomcat并不一定是存储图片的tomcat,图片无法反显,并且将图片上传到服务器的时候也会出现这样的问题。 还好UEditor提供源码下载,分析了整个上传流程,最后决定自己增加选中图片后直接通过FTP上传到文件服务器上。具体步骤如下:

1.在webapp/utf8-jsp/jsp目录下找到config.json文件,在其中增加红色字体部分。


         /* 上传图片配置项 */
"useFtpUpload": "true", /* 是否使用FTP上传 */
"keepLocalFile": "false", /* 使用FTP上传后本地服务器是否保存 */

"imageActionName": "uploadimage", /* 执行上传图片的action名称 */
"imageFieldName": "upfile", /* 提交的图片表单名称 */
"imageMaxSize": 2048000, /* 上传大小限制,单位B */
"imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 上传图片格式显示 */
"imageCompressEnable": true, /* 是否压缩图片,默认是true */
"imageCompressBorder": 1600, /* 图片压缩最长边限制 */
"imageInsertAlign": "none", /* 插入的图片浮动方式 */
"imageUrlPrefix": "http://文件服务器域名:端口/项目名称",/* 图片访问路径前缀 */
"imagePathFormat": "/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
/* {filename} 会替换成原文件名,配置这项需要注意中文乱码问题 */
/* {rand:6} 会替换成随机数,后面的数字是随机数的位数 */
/* {time} 会替换成时间戳 */
/* {yyyy} 会替换成四位年份 */
/* {yy} 会替换成两位年份 */
/* {mm} 会替换成两位月份 */
/* {dd} 会替换成两位日期 */
/* {hh} 会替换成两位小时 */
/* {ii} 会替换成两位分钟 */
/* {ss} 会替换成两位秒 */
/* 非法字符 \ : * ? " < > | */
/* 具请体看线上文档: fex.baidu.com/ueditor/#use-format_upload_filename */

2.在config.json同级目录下找到controller.jsp文件,修改其中红色字体部分,指向自己的Control。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    import="xxx.xxx.xxx.ActionEnter"
    pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%

    request.setCharacterEncoding( "utf-8" );
    response.setHeader("Content-Type" , "text/html");
    
    String rootPath = application.getRealPath( "/" );
    
    out.write( new ActionEnter( request, rootPath ).exec() );
    
%>


3.自己模仿UEditor写一个Control,命名为第2步引入的类。在其中加入红色字体部分,将我们自己定义的参数传入,并且调用我们自己的Uploader类。

package xxx.xxx.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.baidu.ueditor.ConfigManager;
import com.baidu.ueditor.define.ActionMap;
import com.baidu.ueditor.define.AppInfo;
import com.baidu.ueditor.define.BaseState;
import com.baidu.ueditor.define.State;
import com.baidu.ueditor.hunter.FileManager;
import com.baidu.ueditor.hunter.ImageHunter;

public class ActionEnter {
    
    private HttpServletRequest request = null;
    
    private String rootPath = null;
    private String contextPath = null;
    
    private String actionType = null;
    
    private ConfigManager configManager = null;

    public ActionEnter ( HttpServletRequest request, String rootPath ) {
        
        this.request = request;
        this.rootPath = rootPath;
        this.actionType = request.getParameter( "action" );
        this.contextPath = request.getContextPath();
        this.configManager = ConfigManager.getInstance( this.rootPath, this.contextPath, request.getRequestURI() );
        
    }
    
    public String exec () {
        
        String callbackName = this.request.getParameter("callback");
        
        if ( callbackName != null ) {

            if ( !validCallbackName( callbackName ) ) {
                return new BaseState( false, AppInfo.ILLEGAL ).toJSONString();
            }
            
            return callbackName+"("+this.invoke()+");";
            
        } else {
            return this.invoke();
        }

    }
    
    public String invoke() {
        
        if ( actionType == null || !ActionMap.mapping.containsKey( actionType ) ) {
            return new BaseState( false, AppInfo.INVALID_ACTION ).toJSONString();
        }
        
        if ( this.configManager == null || !this.configManager.valid() ) {
            return new BaseState( false, AppInfo.CONFIG_ERROR ).toJSONString();
        }
        
        State state = null;
        
        int actionCode = ActionMap.getType( this.actionType );
        
        Map<String, Object> conf = null;
        
        switch ( actionCode ) {
        
            case ActionMap.CONFIG:
                return this.configManager.getAllConfig().toString();
                
            case ActionMap.UPLOAD_IMAGE:
                conf = this.configManager.getConfig(actionCode);
                conf.put("useFtpUpload",this.configManager.getAllConfig().getString("useFtpUpload"));
                conf.put("keepLocalFile",this.configManager.getAllConfig().getString("keepLocalFile"));
                state

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值