xheditor+struts 实现图片上传

本文介绍了如何结合xheditor编辑器和Struts框架来实现在网页中上传图片的功能。首先展示了JSP页面中编辑器的设置,接着通过JS配置上传参数,然后详细讲解了Action的实现过程,包括文件保存、日期格式化和返回路径。最后,展示了struts.xml的配置以及上传完成后的响应内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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}"
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值