xheditor struts2 上传文件

来源:互联网 发布:云计算为什么这么火 编辑:程序博客网 时间:2024/05/19 16:35

注意,上传图片可能会显示不出来(如果你没放在tomcat的webapps目录下),这是由于上传图片时是默认上传到tomcat的目录下!!!

1. jsp页面

<textarea name="editor1" style="width:100%;min-height: 400px;" id="xheditor"></textarea>                           <script type="text/javascript">            $(function(){                $('#xheditor').xheditor({                    tools:'Img',                    skin:'vista',                    html5Upload : false, //此属性必须为false 否则无法上传图片                     onUpload : uploadImg,                       upImgUrl:"${pageContext.request.contextPath}/plan/preplan/preplan_picture_uploadImg.action",//action请自己配置                    upImgExt:"jpg,jpeg,gif,png"                });                 function uploadImg(data) {                      //...回调函数                  }             })  </script>             

2. strust.xml配置(action的名字请自己配置)

        <!-- Picture拦截和结果配置 -->        <action name="preplan_picture_*" class="com.gsafety.plan.module.actions.PictureAction" method="{1}">            <result name="jsonObject" type="json">                <!-- 为该Result指定参数 -->                <param name="noCache">true</param>                <param name="contentType">text/html</param>                <!-- 设置只序列Action的map属性 -->                <param name="root">jsonObject</param>            </result>                       <result name="jsonArray" type="json">                <!-- 为该Result指定参数 -->                <param name="noCache">true</param>                <param name="contentType">text/html</param>                <!-- 设置只序列Action的map属性 -->                <param name="root">jsonArray</param>            </result>                               </action>

3. jar包

import java.io.File;import java.io.IOException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONArray;import net.sf.json.JSONObject;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;
  1. Action方法
public class PictureAction extends ActionSupport{    private int id;    private String imgUrl;//图片链接    private String err = "";      private String msg;              //返回信息      private File filedata;           //上传文件      private String filedataFileName; //文件名     private JSONObject jsonObject = new JSONObject();    private JSONArray jsonArray = new JSONArray();    public String uploadImg(){          //获取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=utf-8");  //        PrintWriter out = null;  //        try {  //            out = response.getWriter();  //        } catch (IOException e1) {  //            e1.printStackTrace();  //        }          /*这里的路径设置,在我的电脑上传路径是:D:\apache-tomcat-7063\wtpwebapps\plan\WEB-INF\img。        */        String saveRealFilePath = ServletActionContext.getServletContext().getRealPath("/WEB-INF/img");          System.out.println(saveRealFilePath );//这里是上传的路径        File fileDir = new File(saveRealFilePath);          if (!fileDir.exists()) { //如果不存在 则创建               fileDir.mkdirs();          }          File savefile = new File(saveRealFilePath , filedataFileName);          try {              FileUtils.copyFile(filedata, savefile);          } catch (IOException e) {              err = "错误"+e.getMessage();              e.printStackTrace();          }          String fileName = request.getContextPath() + "/img/" + filedataFileName;          //msg = "{\"err\":\"" + err + "\",\"msg\":\"" + fileName + "\"}";          jsonObject.put("err", err);        jsonObject.put("msg", fileName);                 //返回msg信息          return "jsonObject";     }    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getImgUrl() {        return imgUrl;    }    public void setImgUrl(String imgUrl) {        this.imgUrl = imgUrl;    }    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;    }    public JSONObject getJsonObject() {        return jsonObject;    }    public void setJsonObject(JSONObject jsonObject) {        this.jsonObject = jsonObject;    }    public JSONArray getJsonArray() {        return jsonArray;    }    public void setJsonArray(JSONArray jsonArray) {        this.jsonArray = jsonArray;    }}

参考链接http://blog.csdn.net/itmyhome1990/article/details/38491205

原创粉丝点击