Xheditor+structs2+js+jquery 实现文件上传功能
?第一步:先配好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("-----要上传的文件