| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
11年前发布

JQuery、ajaxFileUpload、Struts2和注解异步上传文件

一个很好的组件ajaxFileUpload,利用此组件成功实现了异步文件上传!

第一步:需要导入jquery-1.7.2.min.js、ajaxfileupload.js两个文件,在jsp页面引入的顺序必须是jquery-1.7.2.min.js在先,ajaxfileupload.js在后,因为ajaxfileupload.js依赖与jquery包,所以你懂得!         jsp页面代码:  <!-- lang: html -->  <button id="uploadSubmit" type="submit" class="btn btn-sm btn-info col-md-1 col-md-offset-1" onclick="FileUpload('uploadSubmit');">                      <span class="glyphicon glyphicon-upload"></span>Upload                  </button>    js代码:  <!-- lang: js -->  function FileUpload(buttonId) {  $.ajaxFileUpload({      url : 'fileUpload!upload',// 用于文件上传的服务器端请求地址      type : "post",      dataType : "json",      timeout : 1000,      secureuri : false,// 一般设置为false      fileElementId : uploadId,// 文件上传空间的id属性 <input type="file" id="uploadId" />      error : function(XMLHttpRequest, textStatus, errorThrown) {        },      success : function(data) {        }    });
<!-- lang: java -->    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 org.apache.struts2.ServletActionContext;  import org.apache.struts2.convention.annotation.Action;  import org.apache.struts2.convention.annotation.InterceptorRef;  import org.apache.struts2.convention.annotation.InterceptorRefs;  import org.apache.struts2.convention.annotation.Result;  import org.apache.struts2.convention.annotation.Results;  import org.springframework.beans.factory.annotation.Autowired;    import cn.caculate.service.upload.IFileUploadService;    import com.opensymphony.xwork2.ActionSupport;    @Action("fileUpload")  @InterceptorRefs(value = { @InterceptorRef("fileUploadStack") })  @Results({ @Result(name = "jsonType", type = "json") })  public class CopyOfFileUploadAction extends ActionSupport {    private static final long serialVersionUID = 1L;  private static final int BUFFER_SIZE = 16 * 1024;      /**   * 需要上传的文件   */  private File upload;    /**   * 上传文件的类型   */  private String uploadContentType;    /**   * 文件名   */  private String uploadFileName;    /**   * 上传之后的文件名   */  private String storageFileName;    /**   * 文件上传的路径   */  public String path = ServletActionContext.getServletContext().getRealPath(          File.separator + "WEB-INF" + File.separator + "file");    /**   * 新文件上传   *    * @return   */  public String upload() {      try {          // 将Struts2自动封装的文件名赋给要写入的文件          storageFileName = uploadFileName;          // 创建要写入的文件          File storageFile = new File(path + "//" + storageFileName);          copy(upload, storageFile);          return "jsonType";      } catch (Exception e) {          e.printStackTrace();      }      return null;  }    /**   * 上传文件的主要方法   *    * @param src   * @param dst   * @return   */  public boolean copy(File src, File dst) {      try {          InputStream in = null;          OutputStream out = null;          try {              in = new BufferedInputStream(new FileInputStream(src),                      BUFFER_SIZE);              out = new BufferedOutputStream(new FileOutputStream(dst),                      BUFFER_SIZE);              byte[] buffer = new byte[BUFFER_SIZE];              while (in.read(buffer) > 0) {                  out.write(buffer);              }          } finally {              if (null != in) {                  in.close();              }              if (null != out) {                  out.close();              }          }      } catch (Exception e) {          e.printStackTrace();      }      return true;  }    public File getUpload() {      return upload;  }    public void setUpload(File upload) {      this.upload = upload;  }    public String getUploadContentType() {      return uploadContentType;  }    public void setUploadContentType(String uploadContentType) {      this.uploadContentType = uploadContentType;  }    public String getUploadFileName() {      return uploadFileName;  }    public void setUploadFileName(String uploadFileName) {      this.uploadFileName = uploadFileName;  }    public String getStorageFileName() {      return storageFileName;  }    public void setStorageFileName(String storageFileName) {      this.storageFileName = storageFileName;  }      public String getPath() {      return path;  }    public void setPath(String path) {      this.path = path;  }

 本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1385600822328.html
上传文件 文件上传