您的位置:首页 > 产品设计 > UI/UE

SSM整合百度富文本编辑器ueditor二

2017-12-01 19:33 375 查看
传动门

http://blog.csdn.net/qq_34371461/article/details/78679993

正题:

修改config.json中的文件上传配置

"imageUrlPrefix": "http://images.test.com", /* 图片访问路径前缀(图片服务器地址) */
"imagePathFormat": "/{yyyy}{mm}{dd}/{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
"physicsPath":"D:/www/images", /* 文件存放的物理路径,window服务器 */


ConfigManager类修改如下:

/**
* 通过一个给定的路径构建一个配置管理器,该管理器要求地址路径必须存在config.properties文件中
*
* @param rootPath
* @param contextPath
* @param uri
* @throws FileNotFoundException
* @throws IOException
*/
private ConfigManager(String rootPath, String contextPath, String uri)
throws FileNotFoundException, IOException {
rootPath = rootPath.replace("\\", "/");
this.rootPath = rootPath;
this.contextPath = contextPath;

//注释原有的读取配置文件方式
//        if (contextPath.length() > 0)
//          this.originalPath = (this.rootPath + "WEB-INF/classes/config.json");
//        else {
//          this.originalPath = (this.rootPath + uri);
//        }
/**指定新的配置文件读取路径,确保ueditor在初始化能够正确的加载配置文件*/
this.originalPath = this.rootPath + "WEB-INF/classes/config.json";

initEnv();
}


其中文件上传用了BinaryUploader类,一开始上传图片并没有成功,debug模式下跟踪发现问题所在,

在BinaryUploader中:

/**debug到这里发现从request解析数据为空*/
FileItemIterator iterator = upload.getItemIterator(request);


一番研究,原因是因为项目中用的SpringMVC的缘故,spring mvc会把request中的文件封装为对象MultipartFile,所以springmvc会先对request进行解析,这里你再进行解析就获取不到了。解决办法,找到springmvc中关于文件上传的配置:

<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="104857600" />
<property name="maxInMemorySize" value="4096" />
<property name="defaultEncoding" value="UTF-8"></property>
</bean>


可以看到上传用到了CommonsMultipartResolver类,我们自己写个类继承它:

public class CommonsMultipartResolverUeditor extends CommonsMultipartResolver {
@Override
public boolean isMultipart(HttpServletRequest request)
{
String url = request.getRequestURI();
//对所有请求的上传路径做个匹配,如果是编辑器的请求那么不用springmvc的上传处理
if (url != null && url.contains("/sys/ueditor/exec"))
{
return false;
}
else
{
//springmvc的上传处理
return super.isMultipart(request);
}
}
}


修改配置为:

<!-- 配置springMVC处理上传文件的信息 -->
<bean id="multipartResolver"
class="com.hooenergy.operator.api.model.entity.ueditor.CommonsMultipartResolverUedito">
<property name="maxUploadSize" value="104857600" />
<property name="maxInMemorySize" value="4096" />
<property name="defaultEncoding" value="UTF-8"></property>
</bean>


这样就能正常上传图片了。

其中上传类还有一些修改如下:

public class BinaryUploader {
public static final State save(HttpServletRequest request, Map<String, Object> conf) {
FileItemStream fileStream = null;
boolean isAjaxUpload = request.getHeader("X_Requested_With") != null;

/**
* 请求中是否包含内容
*/
if (!ServletFileUpload.isMultipartContent(request)) {
return new BaseState(false, AppInfo.NOT_MULTIPART_CONTENT);
}

/**FileItem--每个FileItem的实例都包含一个文件以及该文件的其他一些属性(文件名、大小等)*/
/**DiskFileItemFactory——工厂类*/
/**ServletFileUpload--用于解析resquest,能从resquest中解析出装有FileItem对象的一个List*/
ServletFileUpload upload = new ServletFileUpload(
new DiskFileItemFactory());

if (isAjaxUpload) {
upload.setHeaderEncoding("UTF-8");
}
try {

//解析request请求 返回FileItemStream的iterator实例,spring mvc会把request中的文件封装为对象MultipartFile
FileItemIterator iterator = upload.getItemIterator(request);

while (iterator.hasNext()) {
fileStream = iterator.next();

if (!fileStream.isFormField())
break;
fileStream = null;
}

if (fileStream == null) {
return new BaseState(false, 7);
}

/** 上传保存路径,可以自定义保存路径和文件名格式 config.json: filePathFormat */
String savePath = (String) conf.get("savePath");
String originFileName = fileStream.getName(); // 111.jpg
String suffix = FileType.getSuffixByFilename(originFileName); // .jpg 后缀

originFileName = originFileName.substring(0,
originFileName.length() - suffix.length()); // 111 文件名
savePath = savePath + suffix; // /ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{rand:6}.png 可以统一采用时间戳方法命名

long maxSize = ((Long) conf.get("maxSize")).longValue();

if (!validType(suffix, (String[]) conf.get("allowFiles"))) {
return new BaseState(false, 8);
}

savePath = PathFormat.parse(savePath, originFileName);

/** 类似 D:/Program Files/apache-tomcat-7.0.64/wtpwebapps//ueditor/jsp/upload/image/20151116/1447675575987060166.png */
//            String physicalPath = (String) conf.get("rootPath") + savePath;  // 默认配置图片上传地址是在项目路径下,项目重新部署图片就没有了,所以这里我们采用自己配置的保存路径,保存到服务器的其他盘下

/****************修改文件存放路径为自己配置的存放路径*****************/
String physicalPath = (String) conf.get("physicsPath");
if (physicalPath != null && !"".equals(physicalPath)) {
physicalPath += savePath;
} else {
physicalPath = (String) conf.get("rootPath") + savePath;
}
/*************************************************************/

InputStream is = fileStream.openStream();
//State这个类很重要,是一个接口,它是返回到前端的数据
State storageState = StorageManager.saveFileByInputStream(is,
physicalPath, maxSize);
is.close();

if (storageState.isSuccess()) {
storageState.putInfo("url", PathFormat.format(savePath));
storageState.putInfo("type", suffix);
storageState.putInfo("original", originFileName + suffix);
}

return storageState;
} catch (FileUploadException e) {
return new BaseState(false, 6);
} catch (IOException localIOException) {
}
return new BaseState(false, 4);
}

private static boolean validType(String type, String[] allowTypes) {
List list = Arrays.asList(allowTypes);

return list.contains(type);
}
}


到这里已经大功告成了,如果你还有什么问题或不明白的地方,欢迎评论留言。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: