xheditor实现上传

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值