现在的位置: 首页 > 综合 > 正文

Strust2+jquery+xheditor实现图片上传(转)

2013年01月13日 ⁄ 综合 ⁄ 共 3605字 ⁄ 字号 评论关闭

第一步:先配好struct2项目!xheditor和struct或外部框架是没有多大关系的

 

Struct2项目的配置我就不多说了。这里用到了struct2的Ajax文件上传。

lib包给你说下:

<!--StartFragment -->

 

 struct action配置:

 

<!--StartFragment -->

文件类FileUploadaction:
/**
 *  
 */
package fileupload;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
/**
 * @author 逯辉
 *文件上传的action
 */
public class FileUploadAction extends ActionSupport
{
 /**
  * 
  */
 private static final long serialVersionUID = 1L;
 private static Logger log = LoggerFactory.getLogger(FileUploadAction.class);
 
 private static final int BUFFER_SIZE = 16 * 1024;
 
 private File filedata;
 private String filedataContentType;
 private String filedataFileName;
 private String err;
 private String msg;
 
 
 private boolean success;//和form表单里回调函数success和failure对应,一定要有。
 private String info;  //上传成功与否的提示信息 
 
 
 
 
 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;
 }
 @JSON(serialize =false)
 public String getInfo()
 {
  return info;
 }
 public void setInfo(String info)
 {
  this.info = info;
 }
 public boolean isSuccess()
 {
  return success;
 }
 public void setSuccess(boolean success)
 {
  this.success = success;
 }
 
 @JSON(serialize =false)
 public File getFiledata() {
  return filedata;
 }
 public void setFiledata(File filedata) {
  this.filedata = filedata;
 }
 @JSON(serialize =false)
 public String getFiledataContentType() {
  return filedataContentType;
 }
 public void setFiledataContentType(String filedataContentType) {
  this.filedataContentType = filedataContentType;
 }
 @JSON(serialize =false)
 public String getFiledataFileName() {
  return filedataFileName;
 }
 @JSON(serialize =false)
 public void setFiledataFileName(String filedataFileName) {
  this.filedataFileName = filedataFileName;
 }
 private  void writeFile(File src,File dest)
 {
  log.info("-----文件开始写入-----");
  InputStream in = null;
  OutputStream  out = null;
  try
  {
    in = new BufferedInputStream(new FileInputStream(src),BUFFER_SIZE);
    out  = new BufferedOutputStream(new FileOutputStream(dest),BUFFER_SIZE);
    byte[] buffer = new byte[BUFFER_SIZE];
    
    //开始写入
    while(in.read(buffer)>0)
    {
     out.write(buffer);
    }
  }
  catch(Exception e)
  {
   e.printStackTrace();
   success=false;
   info = "上传失败!";
   log.info("-----写入失败!IO异常-----");
  }
  finally
  {
   try
   {
   if(null!=in)
   {
    in.close();
   }
   if(null!=out)
   {
    out.close();
   }
   }
   catch(Exception e)
   {
    e.printStackTrace();
    success=false;
    info = "上传失败";
    log.info("-----写入失败!-----");
   }
  }
  log.info("-----写入成功!-----");
  
 }
 public String execute() throws Exception
 {
  log.info("-----要上传的文件名:"+filedataFileName);
  
  //获得wapps的路径,uploader 指的放在文件的地方
   String toSrc = ServletActionContext.getServletContext().getRealPath("image")+"/"+filedataFileName;
  
   log.info("-----文件保存的路径:"+toSrc);
   msg="/Xheditor/image/"+filedataFileName;
   err="";
   File toFile = new File(toSrc);
   writeFile(this.filedata,toFile);
   success = true;
   info = "上传成功!";
   
   //必须加否则会出现下载框
   HttpServletResponse response = ServletActionContext.getResponse();
  response.setContentType("text/html;charset=utf-8");
    return NONE;//写成SUCCESS就不能调用JS的回调函数 success,暂时还不知道为什么。
 }
 
 
}
注意的是返回json数据{err:"如果是空字符串,文件上传成功,如果有字符返回错误信息",msg:"新文件名的路径图片链接的时候要用"}
解压包 把<!--StartFragment -->

 包拷贝在webroot下
页面引入两个js文件:<!--StartFragment -->


 
<!--StartFragment -->

 
注意:upImgUrl是请求的action

抱歉!评论已关闭.