struts2+xheditor+上传图片功能

去官网下载http://xheditor.com/

新建index.jsp 代码如下<%@ 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 type="text/javascript" src="${basePath }js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="${basePath }xheditor/xheditor-1.1.13-zh-cn.min.js"></script>
  </head>
  
  <body>
  <textarea id="elm1" name="content"  style="width: 600px;height: 400px;">test</textarea>
  
  
  </body>
  <script type="text/javascript">
  jQuery(document).ready(function(){
  
  /**
  
  ***/
  
  
  
  var editors=$('#elm1').xheditor({tools:'Cut,Copy,Img,Fullscreen,Emot',
    html5Upload:false,
    upImgUrl:"${basePath}/editor/editor.action",
    upImgExt:"jpg,jpeg,gif,png",
    upMultiple:false,
    onUpload:insertUpload
    });
    //$('#elm1').xheditor(false);卸载编辑器
    
    /*jQuery("#ee").click(function(){
    alert(editors.getSource());//获取编辑器数据
    });*/
    
  });
  function insertUpload(arrMsg){
var i,msg;
for(i=0;i<arrMsg.length;i++)
{
msg=arrMsg[i];
//alert(msg);
}

}
  
  
  </script>
</html>

然后新建TestAction 代码如下

package com.test;


import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;


import net.sf.json.util.JSONUtils;


import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.JSONUtil;


import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;




 public class TestAction extends BaseAction{
private File filedata;
public String editor(){
System.out.println(filedata+"哈哈哈哈");
String path=ServletActionContext.getServletContext().getRealPath("e");
long time=System.currentTimeMillis();
String imagePath = path+"\\"+time+".jpg";//大图片路径
try {
if(filedata!=null){
FileUtils.copyFile(filedata, new File(imagePath));
System.out.println("dddddd");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


//PrintWriter p=null;
String err="";
System.out.println(imagePath);
String imagePath2=this.getBasePath()+File.separator+"\\e\\"+File.separator+time+".jpg";
String newFileName=time+".jpg";
//this.printMessage("{\"err\":\"\",\"msg\":\""+imagePath+"\"} ");
this.printMessage("{\"err\":\"" + err + "\",\"msg\":\"" + imagePath2+ "\"}");

  // p.print("{\"err\":\"\",\"msg\":\"200906030521128703.gif\"} ");
   
 
return null;
}
public File getFiledata() {
return filedata;
}
public void setFiledata(File filedata) {
this.filedata = filedata;
}




}

配置文件 struts。xml 我想只要稍微有点基础的人都能配置出来

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值