xhEditor struts2实现图片上传详解编程语言

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

xhEditor struts2实现图片上传详解编程语言

如果想要出现上传按钮,在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.blog.ytso.com.ImgUploadAction"> 
		</action> 
	</package> 
</struts>

效果如图:

xhEditor struts2实现图片上传详解编程语言

项目演示源码下载:http://download.csdn.net/detail/blog.ytso.com/7746271

转载请注明出处:blog.ytso.com/article/details/38491205

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/14477.html

(0)
上一篇 2021年7月19日 16:53
下一篇 2021年7月19日 16:53

相关推荐

发表回复

登录后才能评论