ajaxfileupload实现上传SSH框架

前端之家收集整理的这篇文章主要介绍了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>
原文链接:https://www.f2er.com/ajax/163295.html

猜你在找的Ajax相关文章