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;
- 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
阅读全文
0 0
- xheditor struts2 上传文件
- struts2+xheditor+上传图片功能
- xhEditor struts2实现图片上传
- dwz xheditor编辑器文件上传
- dwz xheditor编辑器文件上传
- struts2中添加xheditor编辑器,上传图片
- xheditor-文件上传-java-支…
- xheditor-文件上传-java-支…
- Java 实现 xhEditor 文件上传处理
- xhEditor支持HTML5的文件上传解决application/octet-stream
- 万能表单解析工具在xheditor上传文件中的应用
- .Net MVC后台接收xheditor上传文件,FTP文件传输
- struts2 配置 xheditor
- xheditor实现上传
- xheditor图片上传
- struts2上传文件(三) struts2上传文件
- xhEditor在线编辑器学习笔记(1)---用Struts2+ajax实现图片上传
- [Struts2]Struts2的文件上传
- Python的requests软件包详解
- 美团 Robust热更新使用图解
- C6678多核DSP开发——vlib应用之连通域标记
- Hive on Tez部署及验证测试
- mac终端操作mysql
- xheditor struts2 上传文件
- 二叉查找树(通过外排思想处理大数据)
- (OK) Sample IPv6 Quagga config
- [command]脚本基本知识
- Android Studio中各种jdk和sdk的类找不到,提示setup jdk
- asdas
- AOJ-problem-874
- MyFlag Step9 :Flag功能相关的设计
- 文章标题