ajaxfileupload实现上传SSH框架

第一步引入相应的JS文件

<!--引入ajaxfileupload -->
 <script type="text/javascript" src="${pageContext.request.contextpath}/jslib/ajaxfileupload.js"></script>
 <script src='" + contextpath + "/jslib/jquery-1.9.1.js' type='text/javascript' charset='utf-8'></script>

第二步配置好ajaxfileupload

<script type="text/javascript"> function ajaxFileUpload() { $("#loading") .ajaxStart(function(){ $(this).show(); })//开始上传文件显示一个图片 .ajaxComplete(function(){ $(this).hide(); });//文件上传完成将图片隐藏起来 $.ajaxFileUpload ( { url:'<%=basePath%>fileUploadAction',//用于文件上传的服务器端请求地址 secureuri:false,//一般设置为false fileElementId:'file',//文件上传间的id属性 <input type="file" id="file" name="file" /> dataType: 'json',//返回值类型 一般设置为json success: function (data,status) //服务器成功响应处理函数 { //alert(data.message);//从服务器返回的json中取出message中的数据,其中message为在struts2中action中定义的成员变量 $('#data_imgurl').val(data.message); if(typeof(data.error) != 'undefined') { if(data.error != '') { alert(data.error); }else { alert(data.message); } } },error: function (data,status,e)//服务器响应失败处理函数 { alert(e); } } ); return false; } </script>

第三步编写HTML代码

<td colspan="3"><input type="file" id="file" name="file" /></td>
              <td><a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="ajaxFileUpload()">上传</a>  </td>
              <td><img src="<%=basePath%>image/loading.gif" id="loading" style="display: none;"></td>

第四步编写好相应的action

package bea.com.Action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import bea.com.util.DateParse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class FileAction extends ActionSupport {

    private File file;
    private String fileFileName;
    private String fileFileContentType;

    private String message = "你已成功上传文件";

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    public String getFileFileContentType() {
        return fileFileContentType;
    }

    public void setFileFileContentType(String fileFileContentType) {
        this.fileFileContentType = fileFileContentType;
    }

    @SuppressWarnings("deprecation")
    @Override
    public String execute() throws Exception {

        //String path = ServletActionContext.getRequest().getRealPath("/upload");
         String path=ServletActionContext.getServletContext().getRealPath("/upload");
         String pathuri=ServletActionContext.getRequest().getcontextpath();

        try {
            File f = this.getFile();
            if(this.getFileFileName().endsWith(".exe")){
                message="对不起,你上传文件格式不允许!!!";
                return ERROR;
            }
            FileInputStream inputStream = new FileInputStream(f);
            String s=DateParse.date4String(new Date());
            fileFileName=s+this.getFileFileName();
            FileOutputStream outputStream = new FileOutputStream(path + "/"+fileFileName);
            byte[] buf = new byte[1024];
            int length = 0;
            while ((length = inputStream.read(buf)) != -1) {
                outputStream.write(buf,0,length);
            }
            inputStream.close();
            outputStream.flush();
            message=pathuri + "/upload/"+fileFileName;
        } catch (Exception e) {
            e.printstacktrace();
            message = "对不起,文件上传失败了!!!!";
        }
        return SUCCESS;
    }

}

第五步配置好struts2

<action name="fileUploadAction" class="bea.com.Action.FileAction">
            <result type="json" name="success">
                <param name="contentType">
                    text/html
                </param>
            </result>
            <result type="json" name="error">
                <param name="contentType">
                    text/html
                </param>
            </result>
        </action>

相关文章

IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...