废话不说了,直接上代码,代码比较乱,紧体现xheditor与struts2上传,
第一步:
到官网下载最新版本xheditor插件,或者从附件中下载
第二步:jsp页面
注意:jsp中 代码 html5Upload:false,upMultiple:'1' 这块很重要,屏蔽掉html5上传,现在大部分是IE6,8。IE9才支持html5,其他浏览器百度去
第三步:java代码
第一步:
到官网下载最新版本xheditor插件,或者从附件中下载
第二步:jsp页面
注意:jsp中 代码 html5Upload:false,upMultiple:'1' 这块很重要,屏蔽掉html5上传,现在大部分是IE6,8。IE9才支持html5,其他浏览器百度去
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
basePath = basePath.substring(0, (basePath.lastIndexOf("/") == basePath.length() - 1) ? basePath.lastIndexOf("/") : basePath.length());
session.setAttribute("basePath",basePath+"/");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script src="<%=basePath %>/scripts/xheditor/jquery/jquery-1.4.4.min.js" type="text/javascript"></script>
<script src="<%=basePath %>/scripts/xheditor/xheditor-1.1.14-zh-cn.min.js" charset="utf-8" type="text/javascript"></script>
</head>
<body>
<textarea id="elm1" name="elm1" style="width:860px;height:180px;background:url(img/demobg.jpg) no-repeat right bottom fixed">Hello xhEditor!<br />拖动你电脑中的图片到这里,体验HTML5批量拖放上传的强大功能</textarea><br /><span style="color:#ccc;">编辑区域中按快捷键Ctrl+Enter提交表单</span><br /><br />
</body>
<script type="text/javascript">
jQuery(document).ready(function(){
$(pageInit);
var editor;
function pageInit()
{
editor=$('#elm1').xheditor({html5Upload:false,upMultiple:'1',upLinkUrl:'demos/upload.php?immediate=1',upImgUrl:'/struts1_xheditor/upload.do?immediate=1',upFlashUrl:'demos/upload.php?immediate=1',upMediaUrl:'demos/upload.php?immediate=1',localUrlTest:/^https?:\/\/[^\/]*?(xheditor\.com)\//i,remoteImgSaveUrl:'demos/saveremoteimg.php',emots:{
msn:{name:'MSN',count:40,width:22,height:22,line:8},
},loadCSS:'<style>pre{margin-left:2em;border-left:3px solid #CCC;padding:0 1em;}</style>',shortcuts:{'ctrl+enter':submitForm}});
}
function submitForm(){$('#frmDemo').submit();}
});
</script>
</html>
第三步:java代码
package com.bjsxt.struts2.path.action;
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
public class PathAction {
protected HttpServletRequest request;
protected HttpServletResponse response;
private File filedata;
private String filedataContentType;
private String filedataFileName;
private String err;
private String msg;
private String message;
private String fileExt = "jpg,jpeg,gif,bmp,png";
public String execute() throws IOException {
ActionContext context = ActionContext.getContext();
HttpServletResponse response = (HttpServletResponse) context
.get(ServletActionContext.HTTP_RESPONSE);
// 这里可以获得file
File file = this.getFiledata();
System.out.print(file);
// 接下来就是上传的事情了,
// 接下来的事情,就是把文件写到服务器,至于怎么写,这应该不用说了吧…………大家多思考,多插资料,多动手
// 这里注意的是,返回的时候,返回json格式的,自己拼或者jsonObject都可以,如果错误就写在err里面,msg写上传的路径,xheditor会接受这个路径然后插入编辑器中
// 如果返回地址有半角 叹号,直接确定
response.getOutputStream().print("{\"err\":\"\",\"msg\":\"!111.jpg\"}");
return null;
}
public File getFiledata() {
return filedata;
}
public void setFiledata(File filedata) {
this.filedata = filedata;
}
public String getFiledataContentType() {
return filedataContentType;
}
public void setFiledataContentType(String filedataContentType) {
this.filedataContentType = filedataContentType;
}
public String getFiledataFileName() {
return filedataFileName;
}
public void setFiledataFileName(String filedataFileName) {
this.filedataFileName = filedataFileName;
}
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 String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getFileExt() {
return fileExt;
}
public void setFileExt(String fileExt) {
this.fileExt = fileExt;
}
public HttpServletRequest getRequest() {
return request;
}
public void setRequest(HttpServletRequest request) {
this.request = request;
}
public HttpServletResponse getResponse() {
return response;
}
public void setResponse(HttpServletResponse response) {
this.response = response;
}
}