本文总结得不好,如果有人和我遇到同样的问题,先别急着看我咋改的,因为,前面的是我昨天写的,写到后面,最后发现是路径问题,这个路径问题我搞了很久,很久,很久,今早起来我都觉得要放弃的时候,我又尝试着改了一下那个controller的代码里面的rootPath,改回原来的,居然,成功了,照片能上传能显示,但是在后台测试输出的路径感觉很奇怪,但是既然能成功了,作为小菜鸟的我也不敢再轻易改动了。
其实网上的大部分的办法都是可行的,如果换在自己项目上,感觉不行,那大部分都是路径问题,建议下载源代码,可以进去看看源代码怎么写的,可以输出一下各种信息,让你更了解在哪出了问题,我就是,不停的sysout,一直翻源代码,我都觉得对这个源码都熟悉了。
我总结了我自己出错的两个重点,一,json文件是否读取到;二:路径!路径!路径!这个不好说,要看你自己的项目,这个得自己摸索。加油,我耗时一周,终于配置成功了。抱歉,学艺不精,求不喷。
我的毕设也要用到富文本编辑器,我选择了Ueditor,网上很多人也用了,有些改配置改得很简单就成功了,有些还要修改源码,我都试过了,按照网上的方式,上传图片终究配置不成功,之前我的配置是按照网上的,在这不详细写了,一搜一大把,这里主要是记录我在网上查不到的解决办法。
以为配置成功了,结果点击上传图片就一直转圈圈。

控制台查看错误:
ueditor.all.js:24557 Uncaught ReferenceError: errorHandler is not defined
at HTMLInputElement.<anonymous> (ueditor.all.js:24557)
(anonymous) @ ueditor.all.js:24557

我按照网上改写的controller代码
@RequestMapping("/config")
public void ueditorConfig(HttpServletRequest request, HttpServletResponse response) {
System.out.println("edite 经过的request");
response.setContentType("application/json");
response.setHeader("ContentType", "text/html");
String action = request.getParameter("action");
System.out.println("action:"+action);
String rootPath = request.getSession().getServletContext().getRealPath("/");
System.out.println("rootPath:"+rootPath);
try {
request.setCharacterEncoding("utf-8");
String exec = new ActionEnter(request, rootPath).exec();
System.out.println("exec"+exec);//输出了错误信息
PrintWriter writer = response.getWriter();
writer.write(exec);
writer.flush();
writer.close();
} catch (IOException e) {
System.out.println("edite 经过的request出错..");
}
}
发现控制台输出:
edite 经过的request
action:config
rootPath:/Users/chenjiafeng/IdeaProjects/TSS/target/TSS-1.0-SNAPSHOT/
exec{"state": "\u914d\u7f6e\u6587\u4ef6\u521d\u59cb\u5316\u5931\u8d25"}
edite 经过的request
\u914d\u7f6e\u6587\u4ef6\u521d\u59cb\u5316\u5931\u8d25。百度这句话的意思是配置文件初始化失败
又百度了很久,我猜测,是不是读取不到config.json,于是我在源码下测试:
(怎么进入方法这应该都懂的,修改源码的前提是把源码下载过来,网上的都有教程)
进入ActionEnter-->

在进入ConfigManager-->

找到该方法,输出一下这个getConfigPath看看,重启服务器,重新运行,点击上传图片,控制台输出这些信息:
edite 经过的request
action:config
rootPath:/Users/chenjiafeng/IdeaProjects/TSS/target/TSS-1.0-SNAPSHOT/
getConfigPath:/Users/chenjiafeng/IdeaProjects/TSS/target/TSS-1.0-SNAPSHOT/config.json
exec{"state": "\u914d\u7f6e\u6587\u4ef6\u521d\u59cb\u5316\u5931\u8d25"}

这问题就出来了,果然。路径出错,我这项目路径没有这个config.json文件,我就说这个rootPath路径咋怪怪的,我找找该怎么改这个路径。 我看看是路径那部分出了错,然后在刚刚那个方法输出得更细一点,酱:
private String getConfigPath () {
System.out.println("getConfigPath:"+this.parentPath + File.separator + ConfigManager.configFileName);
System.out.println("this.parentPath:"+this.parentPath);
System.out.println("File.separator:"+File.separator);
System.out.println("ConfigManager.configFileName:"+ConfigManager.configFileName);
return this.parentPath + File.separator + ConfigManager.configFileName;
}
又重启又运行,控制台输出以下信息:
edite 经过的request
action:config
rootPath:/Users/chenjiafeng/IdeaProjects/TSS/target/TSS-1.0-SNAPSHOT/
getConfigPath:/Users/chenjiafeng/IdeaProjects/TSS/target/TSS-1.0-SNAPSHOT/config.json
this.parentPath:/Users/chenjiafeng/IdeaProjects/TSS/target/TSS-1.0-SNAPSHOT
File.separator:/
ConfigManager.configFileName:config.json
exec{"state": "\u914d\u7f6e\u6587\u4ef6\u521d\u59cb\u5316\u5931\u8d25"}
哦豁,看见没,这个父路径的错 this.parentPath。那这个this.parentPath哪来的,我找找。
.......(此处过了很久,没改成功,但是,我直接在方法上面写了那个文件的所在路径,hehehehehe,像这样:)

(能想到这个办法,我可能是傻了吧。。。)然后,运行,我去,居然可以显示了,


没上传成功,控制台输出:
exec{"state": "\u672a\u627e\u5230\u4e0a\u4f20\u6570\u636e"}. 意思也是“未找到上传数据”
到这真正证实了,就是json文件没读到,现在读到了,又有新的问题了(此处又过了很久)。未找到数据的原因是spring拦截了。所以要写一个继承CommonsMultipartResolver的子类,放行。百度了很久,终于找到一篇是ssm的拦截,超级感谢他!
这是这位作者的链接:https://blog.csdn.net/qq_38350609/article/details/89212496
这个类是这样写的:
package com.mandy.filter;
import javax.servlet.http.HttpServletRequest;
public class CommonMultipartResolver extends org.springframework.web.multipart.commons.CommonsMultipartResolver {
public boolean isMultipart(HttpServletRequest request){
System.out.println("进入isMultipart");
String uri=request.getRequestURI();
System.out.println(uri);
if(uri!=null&&uri.indexOf("/config")>0){
System.out.println("CommonsMultipartResolver放行");
return false;
}
System.out.println("CommonsMultipartResolver拦截");
return super.isMultipart(request);
}
}
并且要在springmvc.xml配置:(其实我之前已经有个multipartResolver,但是为了测试,先注释掉了)
<bean id="multipartResolver"
class="com.mandy.filter.CommonMultipartResolver">
<!-- set the max upload size100MB -->
<property name="maxUploadSize">
<value>104857600</value>
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
<property name="defaultEncoding">
<value>utf-8</value>
</property>
</bean>
然后,再次运行,哈哈哈哈哈
显示上传成功,但是....??????

图片显示不了???好吧,发现我的控制台输出:
进入isMultipart
/TSS/TSS/TSS/upload/20200207/1581058492731010505.jpeg
CommonsMultipartResolver拦截
Failed to load resource: the server responded with a status of 404 () (这是谷歌浏览器的错误信息,也就是说,获取不到图片,那就是路径问题)
这路径一看就不对,而且也没放行。不过,确实成功上传了,虽然不是我想要的路径

这是一条分割线,中间我尝试修改了很久的代码,因为改得太多了,我都不知道要从何说起,但是现在配置成功了,我就把我成功的代码贴上去,大家好参考参考。
controller的代码:

@RequestMapping("/config")
public void ueditorConfig(HttpServletRequest request, HttpServletResponse response) {
response.setContentType("application/json");
response.setHeader("ContentType", "text/html");
String action = request.getParameter("action");
System.out.println("action:" + action);
String rootPath = request.getSession().getServletContext().getRealPath("/");
try {
request.setCharacterEncoding("utf-8");
String exec = new ActionEnter(request, rootPath).exec();
System.out.println("exec" + exec);//输出了错误信息
PrintWriter writer = response.getWriter();
writer.write(exec);
writer.flush();
writer.close();
} catch (IOException e) {
System.out.println("edite 经过的request出错..");
}
}
config.json配置(就是修改了图片访问前缀和保存路径)

其他都和网上教程一样的,大家自行百度,以下是配置好的,能显示图片了。

解决Ueditor图片上传问题,涉及路径配置、json文件读取及spring拦截设置。

1327

被折叠的 条评论
为什么被折叠?



