struts2 多檔案上傳 jquery jquery.form ajax 無重新整理

來源:互聯網
上載者:User

web 開發基於 struts2 相當方便,且也可輕鬆實現 FileUpload 功能,這裡示範了 struts2 在多檔案 form 情況下的上傳操作


struts.xml action 定義

<action name="doUploadMultfile" class="cn.upload1.UploadAction" method="Upload"><result name="success" type="json"><param name="root">ActionResult</param></result></action>


jquery ajax 支援

<script type="text/javascript" src="script/jquery.min.js"></script><script type="text/javascript" src="script/jquery.form.js"></script>


jsp 上傳檔案的 form

<s:form id="uploadExce2" action="doUploadMultfile" method="POST" enctype="multipart/form-data"><s:file label="File (1)" name="upload" /><s:file label="File (2)" name="upload" /><s:file label="FIle (3)" name="upload" /><s:submit cssClass="btn btn-primary" /></s:form><script type="text/javascript">          $('#uploadExce2').ajaxForm({            dataType : 'json',            success : function(data)            {              // alert(data);            },            error : function(data)            {              alert('error');            }          });        </script>
注意 file 控制項的名字 uload,這是 struts2 的約定名,不能改變。


struts2 uploadfile

package cn.test.upload1;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.rmi.RemoteException;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.UUID;import javax.servlet.ServletContext;import org.apache.commons.codec.binary.Base64;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import cn.sharcom.config.ChiliLogin;import cn.sharcom.config.ChiliProduct;import cn.sharcom.config.Constant;public class UploadAction{  private File[] uploads;  private String[] uploadFileNames;  private String[] uploadContentTypes;  private String strActionResult;  public UploadAction()  {  }  public File[] getUpload()  {    return this.uploads;  }  public void setUpload(File[] upload)  {    this.uploads = upload;  }  public String[] getUploadFileName()  {    return this.uploadFileNames;  }  public void setUploadFileName(String[] uploadFileName)  {    this.uploadFileNames = uploadFileName;  }  public String[] getUploadContentType()  {    return this.uploadContentTypes;  }  public void setUploadContentType(String[] uploadContentType)  {    this.uploadContentTypes = uploadContentType;  }  public String Upload()  {    ActionContext ac = ActionContext.getContext();    ServletContext sc = (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT);    // 取一個存檔案的目錄    String realpath = sc.getRealPath("/");    // 獲得實際上傳檔案數量    int length = uploads.length;    // 遍歷所有上載的檔案    for (int i = 0; i < length; i++)    {      // 檔案寫磁盤      try      {        // 新檔案名稱        String strFileName = NewFilename(i);        File savefile = new File(new File(realpath), strFileName);        // 寫入        FileUtils.copyFile(uploads[i], savefile);      }      catch (IOException e)      {        e.printStackTrace();      }    }    return Action.SUCCESS;  }  /**   * 向 form 返回 action 結果   *    * @return   */  public String getActionResult()  {    return strActionResult;  }  /**   * 構建一個新的檔案名稱   *    * @return   */  private String NewFilename(int index)  {    String strResult = null;    UUID uuid = UUID.randomUUID();    String str1 = uuid.toString();    String prefix = uploadFileNames[index].substring(uploadFileNames[index].lastIndexOf("."));    strResult = str1 + prefix;    return strResult;  }  /**   * 將檔案轉化爲 Base64   *    * @param strFilename   * @return   */  private String ApacheBase64FileToString(final String strFilename)  {    String strResult = null;    try    {      // 開啟檔案      File file = new File(strFilename);      // 得到檔案 byte      byte[] byteFile = org.apache.commons.io.FileUtils.readFileToByteArray(file);      // 開始 base64      Base64 base64 = new Base64();      // 得到 base64 byte      byte[] byteText = base64.encode(byteFile);      // 轉存 string      strResult = new String(byteText);      // StringToFile("c:/base64_2.txt", strResult);      // 設定返回    }    catch (IOException e)    {      e.printStackTrace();    }    return strResult;  }}

文本串連 blog.csdn.net/joyous/article/details/51103064

參考資料 https://struts.apache.org/docs/file-upload.html

Q群討論 236201801



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.