xheditor实现上传

来源:互联网 发布:乔治敦大学知乎 编辑:程序博客网 时间:2024/05/07 05:17
package com.yuxuan.action;import java.io.File;import java.io.IOException;import org.apache.commons.io.FileUtils;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;import com.opensymphony.xwork2.ActionSupport;@Component@Scope("prototype")public class MessageAction extends BaseAction{private File filedata;    public File getFiledata() {return filedata;}public void setFiledata(File filedata) {this.filedata = filedata;}public void upload() throws IOException{String fileName=filedata.getName().replaceFirst(".tmp", ".png");System.out.println(filedata);String pathString = this.httpServletRequest.getServletContext().getRealPath("/kk/")+"/"+fileName;File copyfile = new File(pathString);FileUtils.copyFile(filedata, copyfile);String url = "/BBS_FORJAVA/kk/";//String jsonsString = "{err:'"+errorString+"',msg:'"+url+fileName+"'}";String jsonsString =  "{\"err\":\"\",\"msg\":\""+url+fileName+"\"}";httpServletResponse.getWriter().write(jsonsString);}}



注意

jsonsString格式

  • 返回内容必需是标准的json字符串,结构可以是如下:{"err":"","msg":"200906030521128703.gif"} 或者 {"err":"","msg":{"url":"200906030521128703.jpg","localfile":"test.jpg","id":"1"}} 注:若选择结构2,则url变量是必有

    err:后面是空字符串,否则会出现接口错误。

    url里面是"/项目名/文件夹名",如果只写文件夹名会出现叉子

    不能返回  String jsonsString =  "{\"err\":\"\",\"msg\":\""+pathString+"\"}";
    会无法读取该图片的

    jsp页面

    <%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script type="text/javascript" src="${pageContext.request.contextPath}/xhEditor/jquery/jquery-1.4.4.min.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/xhEditor/xheditor-1.2.1.min.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/xhEditor/xheditor_lang/zh-cn.js"></script><title>我的短信-我的应用</title></head><body><form id="frmDemo" method="post" action="show.php"><s:textfield label="收件人"></s:textfield><br><s:textfield label="主题"></s:textfield><br><s:textarea id="elm1" name="elm1" rows="12" cols="80" style="width: 80%" cssClass="xheditor">test</s:textarea></form><script type="text/javascript">$('#elm1').xheditor({tools:'full',skin:'default',showBlocktag:true,internalScript:false,internalStyle:false,sourceMode:true,forcePtag:true,upImgUrl:"messageAction_upload",upImgExt:"tmp,jpg,jpeg,gif,png",onUpload:insertUpload});//文件上传回调函数   function insertUpload(data){      var msg=data.toString();     alert(data);    $("#elm1").append(msg);  }   </script></body></html>


     

  • 可以参考一下http://blog.csdn.net/youqishini/article/details/8300174
  • 原创粉丝点击