struts2与kindeditor整合

在配置kindeditor的时候要注意以下几点:
而struts2是以filter注册的:

<filter>
<filter-name>strust2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>strust2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

这时我们就要注意<url-pattern>/*</url-pattern>再不能是/*了这样他会对所有的请求进行拦截。

我在整合的过程中是将他的/*改成<url-pattern>*.do</url-pattern>的。这样他就只会对以点do结尾的请求进行拦截。详细struts.xml配置请见[url]http://daoshud1.iteye.com/admin/blogs/1839223[/url]
建立上传action

// 这里说明一下:KindEditor 它上传的所有类型的附件,如果让Strust2来帮你自动装箱,那么只能的用imgFile来接受。
// 也就是说你上传一个123.txt附件这里也是用 imgFile 来接收的。
// 图片对象
private File imgFile;
private String path;
private ServletContext context;

public String uploadImage(){
try{
HttpServletRequest request=getHttpServletRequest();
String prot = request.getContextPath();
//返回图片地址的URL
String sdmpUrl=ReadProperties.getProperties("sdmp_url");
//图片限制的宽度
Integer kind_w=Integer.parseInt(ReadProperties.getProperties("kind_w"));
if(this.checkImgSize(imgFile, kind_w)){
//系统路径
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+prot+"/";
MultiPartRequestWrapper wrapper = (MultiPartRequestWrapper)ServletActionContext.getRequest();
// 获得图片名字
String imgName = wrapper.getFileNames("imgFile")[0];
// 获得图片后缀名
String fileExt = imgName.substring(imgName.lastIndexOf(".")).toLowerCase();
Date date = new Date();
String savePath = "/"+GloabConstants.UPLOAD_FILE_PATH+"attached/"+path+"/image/"+ new SimpleDateFormat("yyyyMMdd").format(date)+"/";
// 图片在服务器上的绝对路径。编辑器并没有提供删除图片功能,此路径以后可以用于后台程序对图片的操作
String serverPath = ServletActionContext.getServletContext().getRealPath("/")+savePath;
//重置图片名称
String newImgName = date.getTime() + fileExt;
//将图片写入服务器
FileUtils.copyFile(imgFile, new File(serverPath + "/" + newImgName));
boolean isMakeResponseOk = makeSuccessRespForKE(ServletActionContext.getResponse(), basePath+sdmpUrl+savePath + newImgName);
//删除临时文件。
if (isMakeResponseOk){
imgFile.delete();
}else {
makeErrorRespForKE(ServletActionContext.getResponse(), 1, "uploadFail");
}
}else{
makeErrorRespForKE(ServletActionContext.getResponse(), 1, UnicodeUtil.gbEncoding("图片宽度不能超过")+kind_w+"px");
}
}catch (Exception e){
e.printStackTrace();
}

return null;
}

/**
* 成功的时候回写KindEditor。
*
* @param imgServerURL
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public boolean makeSuccessRespForKE(HttpServletResponse resp, String imgServerURL){
// 回写给KE页面的返回信息
JSONObject obj = new JSONObject();
obj.put("error", 0);
obj.put("url", imgServerURL);
//resp.setContentType("text/xml;charset=UTF-8");去除 否则会在谷歌火狐下上传失败
PrintWriter out = null;
try{
out = resp.getWriter();
out.write(obj.toJSONString());
}catch (IOException e){
e.printStackTrace();
return false;
}finally{
if (out != null){
out.close();
}
}
return true;
}

/**
* 检测文件长度是否符合
* @param height
* @return
*/
public boolean checkImgSize(File file,Integer kind_w){
HashMap<String, Object> map=new HashMap<String, Object>();
Image src;
try{
src = javax.imageio.ImageIO.read(file);
int w = src.getWidth(null);
int h = src.getHeight(null);
if(w>kind_w){
return false;
}else{
return true;
}
}catch (IOException e){
log.error(e.getMessage());
e.printStackTrace();
return false;
}
}

/**
* 失败的时候回写KindEditor。
* @param resp
* @param errorCode
* @param errorMsg
* @return
*/

public boolean makeErrorRespForKE(HttpServletResponse resp, int errorCode, String errorMsg){
// 回写给KE页面的返回信息
JSONObject obj = new JSONObject();
obj.put("error", 1);
obj.put("msg", errorMsg);
//resp.setContentType("text/xml;charset=UTF-8");去除 否则会在谷歌火狐下上传失败
PrintWriter out = null;
try{
out = resp.getWriter();
out.write(obj.toJSONString());
}catch (IOException e){
e.printStackTrace();
return false;
}finally{
if (out != null){
out.close();
}
}
return true;
}

KindEditor

ke= KindEditor.create('textarea[name="announcement.content"]', {
cssPath : basePath+'js/kindeditor/plugins/code/prettify.css',
uploadJson : basePath+'uploadImage.do?path=announcement',
fileManagerJson : basePath+'js/kindeditor/jsp/file_manager_json.jsp',
allowFileManager : true,
resizeType:1,
imageTabIndex:1,
items : [
'source','bold', 'italic', 'underline', '|', 'image', 'link','|','justifyleft', 'justifycenter', 'justifyright','justifyfull'],
afterChange : function() {
$('#word_count2').html(this.count('text'));
}
});
}

返回内容会造成乱码,可以考虑将汉字转为Unicode编码。修改KindEditor上传后的提示可以修改kindeditor\plugins\image\image.js 197行处,提示前先将Unicode转为汉字
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值