1.由于页面使用了DWZ框架,所以JSP的实现如下
<textarea name = "eleText" class="editor" rows="40" cols="85">${element.element.text}</textarea>
<input name="elePath" type="hidden" value="${element.element.uniquePath}"/>
2.JS实现
$(document).ready(
function(){
$('.editor').xheditor({
html5Upload:false,
upImgUrl: "general/uploadImgAction",
upImgExt: "jpg,jpeg,gif,bmp,png",
tools:"Cut,Copy,Paste,Pastetext,|,Blocktag,Fontface,FontSize,Bold,Italic,Underline,Strikethrough,Stri,FontColor,kethrough,BackColor,SelectAll,Removeformat,|,Align,List,Outdent,Indent,|,Hr,|,Link,Unlink,Anchor,|,Img,"
//onUpload:insertUpload //指定回调函数,需要自己另外在编写函数实现回调处理.
});
}
);
3.Action 实现
/**
* xheditor上传图片
* @return
* @throws IOException
*/
public String uploadImg() throws IOException
{
Date currentTime = new Date();
SimpleDateFormat timeFormat=new SimpleDateFormat("yyyyMMddHHmmss");
String saveFileName=timeFormat.format(currentTime)+"_"+filedataFileName;
OutputStream os = new FileOutputStream(new File(
request.getRealPath(uploadPath)+"/images/info/",
saveFileName));
InputStream is = new FileInputStream(filedata);
byte[] buf = new byte[1024];
int length = 0;
while (-1 != (length = is.read(buf))) {
os.write(buf, 0, length);
}
is.close();
os.close();
this.imgUploadErr="";
this.setMessage(request.getContextPath()+"/"+uploadPath+"/images/info/"+saveFileName);
return "operationDone";
}
4.struts.xml配置
<!-- xheditor 上传图片 -->
<action name="uploadImgAction" class="pageConfigAction" method="uploadImg">
<result name="operationDone">/jsp/common/imgUploadDone.jsp</result>
<param name="uploadPath">/working/</param>
<param name="allowTypes">image/pjpeg,image/x-png,image/gif,image/jpg,image/png</param>
<!-- 设置允许上传的文件大小(2MB - 2097152) -->
<param name="maximumSize">2097152</param>
</action>
5.imgUploadDone.jsp的内容
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
{
"err":"${err}",
"msg":"${message}"
}