JQuery、ajaxFileUpload、Struts2和注解異步上傳文件

jopen 11年前發布 | 48K 次閱讀 上傳文件 文件上傳

一個很好的組件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) {

}

});</pre>

<!-- 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 + "http://" + 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; }</pre></span>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!