STRUTS2 multi-File upload jquery jquery.form ajax no Refresh

Source: Internet
Author: User
Tags base64 uuid

Web development is very convenient based on struts2, and can also easily implement the FileUpload function, here demonstrated struts2 in the case of multiple file form upload operation


Struts.xml Action Definition

<action name= "Douploadmultfile" class= "cn.upload1.UploadAction" method= "Upload" > <result "name="
	Success "Type=" JSON >
		<param name= "root" >ActionResult</param>
	</result>
</ Action>


jquery Ajax Support

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


form for JSP upload file

<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>
Note the name of the file control Uload, which is the STRUTS2 's contract name and cannot be changed.


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;
  String[] Getuploadfilename () {return this.uploadfilenames;
  } public void Setuploadfilename (string[] uploadfilename) {this.uploadfilenames = Uploadfilename;
  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);

    A directory String Realpath = Sc.getrealpath ("/") for a stored file;

    Get the actual number of uploaded files int length = Uploads.length;  All downloaded files for (int i = 0; i < length; i++) {//file-Write Magnetic try {//new file name String
        strFileName = NewFileName (i);
        File SaveFile = new file (new file (Realpath), strFileName); Write to FilEutils.copyfile (Uploads[i], savefile);
      catch (IOException e) {e.printstacktrace ();
  } return action.success; /** * Returns action result * to form @return/public String Getactionresult () {return Stractionresul
  T

    /** * Constructs a new filename * * @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; /** * Convert File to Base64 * * @param strfilename * @return */private String apachebase64filetostring (Fin

    Al string strFileName) {string strresult = null;
      try {//Open file filename = new strFileName;

      Get file byte byte[] bytefile = org.apache.commons.io.FileUtils.readFileToByteArray (file);
 Start Base64     Base64 base64 = new Base64 ();
      Get base64 byte byte[] bytetext = Base64.encode (bytefile);

      Dump String strresult = new string (bytetext);
      Stringtofile ("C:/base64_2.txt", strresult);
    Set return} catch (IOException e) {e.printstacktrace ();
  return strresult;
 }

}

Text Connection blog.csdn.net/joyous/article/details/51103064

Reference https://struts.apache.org/docs/file-upload.html

Q Group Discussion 236201801



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.