1. Overview
Still using the struts1? Yes, there is no place to write in the earth, the backward mode of production is still in operation (old project).
Inherit the Org.apache.struts.action.Action, inherit the Org.apache.struts.action.ActionForm and start.
2. Code
2.1 HTML page
2.2 _fileupload.js
/** *_fileupload.js * * */window.onload = function () { $ ("#mysubmit"). Bind ("click", Function () { $ ("# MyForm "). Ajaxsubmit ({ URL:" myupload.do ", type:" Post ", success:function (data) { Console.log ( 11111111); Console.log (data) , error:function (responseerror) { console.log (22222222222); Console.log (Responseerror);});} );
2.3 Myuploadaction.java (Inherit from Action)
Package Com.rocky.console.action;import Java.io.file;import Java.io.fileoutputstream;import java.io.InputStream; Import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.apache.struts.action.action;import Org.apache.struts.action.actionform;import Org.apache.struts.action.actionforward;import Org.apache.struts.action.actionmapping;import Org.apache.struts.upload.formfile;import Com.rocky.console.form.myuploadform;import Com.rocky.console.service.responseutil;public class Myuploadaction extends Action {public Actionforward execute (Ac tionmapping mapping, Actionform form, httpservletrequest request, httpservletresponse response) throws Exceptio n{myuploadform myuploadform = (myuploadform) Form; Formfile UploadFile = Myuploadform.getuploadfile (); String filetitle = Myuploadform.getfiletitle (); System.out.println ("111111" +filetitle); int fileSize = Uploadfile.getfilesize (); InputStream InputStream = Uploadfile.getinputstream (); System.out.println ("FileSize.:::::::" +filesize); String path = "x:"; String filename = path + file.separator + uploadfile.getfilename (); FileOutputStream fos = new FileOutputStream (filename); Byte[] B = new byte[1024]; int len =-1; while (len = Inputstream.read (b))!=-1) {fos.write (b, 0, Len); } fos.close (); String result = "Success"; Responseutil.write (response, result, NULL); return null; }}
2.4 Myuploadform.java (inherited from Actionform)
Package Com.rocky.console.form;import Javax.servlet.http.httpservletrequest;import Org.apache.struts.action.actionerrors;import Org.apache.struts.action.actionform;import Org.apache.struts.action.actionmapping;import Org.apache.struts.upload.formfile;public class MyUploadForm extends Actionform {/** * */private static final long serialversionuid = 6650496540449458586L; Private Formfile uploadfile = null; Private String Filetitle; Public String GetFileTitle () {return filetitle; } public void Setfiletitle (String filetitle) {this.filetitle = Filetitle; } public Actionerrors Validate (actionmapping mapping, HttpServletRequest request) {return null; } public void Reset (actionmapping mapping, HttpServletRequest request) {} public formfile Getuploadfile () { return uploadfile; } public void Setuploadfile (Formfile uploadfile) {this.uploadfile = UploadFile; }}
2.5 struts-config.xml
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts-config Public "-//apache software foundation//dtd struts Configuration 1.2//en" "Struts-config_1_2.dtd" & gt;<struts-config> <data-sources/> <form-beans> <form-bean name= "Myuploadform" Type= "Com.rocky.console.form.MyUploadForm"/> </form-beans> <global-exceptions/> <global-forwards/> <action-mappings> <!--Rocky Myupload-to <action path= "/ Myupload "attribute=" Myuploadform "name=" Myuploadform "type=" com.rocky.console.action.MyUploadAction "/> </action-mappings> <message-resources parameter= "Applicationresources"/></struts-config>
2.6 Description
2.6.1 jquery.form.js Online can download http://malsup.com/jquery/form/#download
Use var formData = new FormData (), and then Formdata.append ("MyFile", document.getElementById ("MyFile"). Files[0]); form.append ...
When form form field more write many append very troublesome, obviously ajaxsubmit convenient many
2.6.2 front-end data is encapsulated directly into its subclass (Myactionform) object via Actionform and receives other types of data using string, such as a file received by Formfile.
Of course, they are injected according to the name attribute one by one of the HTML tag.
2.6.3 Actionform How is the custom implemented Bean (Myuploadform) pair?
In Struts-config.xml Form-bean set your own bean, through <action path= "/myupload" attribute= "Myuploadform" Name= "MyUploadForm "
To complete this mapping
Struts1 & jquery form file asynchronous upload