码迷,mamicode.com
首页 > 其他好文 > 详细

xhEditor struts2实现图片上传

时间:2014-08-12 22:05:14      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:xheditor   struts2   图片上传   

xhEditor的环境搭建请参考http://blog.csdn.net/itmyhome1990/article/details/38422255,这时我们打开图片功能 是没有上传按钮的

bubuko.com,布布扣

如果想要出现上传按钮,在xhEditor设置以下参数:

html5Upload : false                     //此属性必须为false

upImgUrl : "ImgUpload.action" //上传服务器接口

onUpload : insertUpload            //服务器返回信息,JSON格式


一、前台代码

<script type="text/javascript">
	$(function() {
		$(‘#content‘).xheditor( {
			tools : ‘full‘,      //也可自定义tools,这里为full 
			html5Upload : false, //此属性必须为false 否则无法上传图片
			upImgUrl : "ImgUpload.action",
			onUpload : uploadImg
		});
		function uploadImg(data) {
			//...回调函数
		}
	})
</script>
<body>
	<div align="center">
		<textarea rows="20" cols="110" id="content"></textarea>
	</div>
</body>
二、Struts2上传代码

public class ImgUploadAction extends ActionSupport {
	private String err = "";
	private String msg;              //返回信息
	private File filedata;           //上传文件
	private String filedataFileName; //文件名

	public String imgUpload() {
		//获取response、request对象
		ActionContext ac = ActionContext.getContext();
		HttpServletResponse response = (HttpServletResponse) ac.get(ServletActionContext.HTTP_RESPONSE);
		HttpServletRequest request = (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);
		
		response.setContentType("text/html;charset=gbk");
		
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		String saveRealFilePath = ServletActionContext.getServletContext().getRealPath("/upload");
		File fileDir = new File(saveRealFilePath);
		if (!fileDir.exists()) { //如果不存在 则创建 
			fileDir.mkdirs();
		}
		File savefile;
		savefile = new File(saveRealFilePath + "/" + filedataFileName);
		try {
			FileUtils.copyFile(filedata, savefile);
		} catch (IOException e) {
			err = "错误"+e.getMessage();
			e.printStackTrace();
		}
		String fileName = request.getContextPath() + "/upload/" + filedataFileName;
		msg = "{\"err\":\"" + err + "\",\"msg\":\"" + fileName + "\"}";
		out.print(msg); //返回msg信息
		return null;
	}

	public String getErr() {
		return err;
	}

	public void setErr(String err) {
		this.err = err;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public File getFiledata() {
		return filedata;
	}

	public void setFiledata(File filedata) {
		this.filedata = filedata;
	}

	public String getFiledataFileName() {
		return filedataFileName;
	}

	public void setFiledataFileName(String filedataFileName) {
		this.filedataFileName = filedataFileName;
	}


}
三、Struts2配置文件

<struts>
	<package name="build" extends="struts-default">
		<action name="ImgUpload" method="imgUpload"
			class="com.itmyhome.ImgUploadAction">
		</action>
	</package>
</struts>
效果如图:

bubuko.com,布布扣



项目演示源码下载:待显示


xhEditor struts2实现图片上传,布布扣,bubuko.com

xhEditor struts2实现图片上传

标签:xheditor   struts2   图片上传   

原文地址:http://blog.csdn.net/itmyhome1990/article/details/38491205

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!