Today in the development of the project encountered a demand, a simple file upload. However, after uploading, you need to do some uploading through the callback function. It is found that file upload can be done through <form><file><input submit>, but the callback function is not very well implemented. So I'm going to use Ajax as a way to commit. Here's the code for the implementation:
JSP page: (CSS style and tags introduced to their own custom, not related to this article, directly to the good);
<% @page language= "java" contenttype= "text/html; Charset=utf-8 "%> <% @include file="/tagdeclare.jsp "%> <% @include file="/headdeclare.jsp "%>Form Action= "${basepath}market/contractdocumentaction!fileupload.action"name= "Aform" method= "post" id= "Actionform"ENCTYPE= "Multipart/form-data" > <div class= "dialogtop" > <table width= "All" border= "0" align= "Center" cellpadding= "0"cellspacing= "Ten" > <tr> <td> Please select File </td> <td> ; <input name= "Upload" type= "FILE" id= "Attach" size= "ten" ></td> <input type= "hidden" n Ame= "Conid" id= "CID" value= "${contractid}"/> </tr> </table> & lt;/div> <div class= "Dialogbottom" > <div class= "btns" > <inp UT type= "button" class= "Ldbtngreen" value= "Submit" onclick= "sub ()"/> <input type= "button" class= "LdB Tngray "value=" Off "onclick= "Lddialog.close ();"/> </div> </div> </form> </body> Javascript:
<script type= "Text/javascript" >functionSub () {varFileobj = document.getElementById ("Attach"). Files[0];//js Get File Object IE8 the following does not support files if(typeof(fileobj) = = "Undefined" | | Fileobj.size <= 0) {Lddialog.alert ("Please select File"); return; } if(Fileobj.size > (1024*1024*10) {Lddialog.alert ("Cannot upload more than 10Mb of files!" "); return; } varFormfile =NewFormData (); Formfile.append ("File", fileobj);//Adding a File objectFormfile.append ("Conids", $ ("#cid"). Val ());//Additional parameter 1 because the form cannot be serialized, the value of the non-file field of the form is obtained after it is submitted and assembled here. The method of retrieving parameters in the background does not changeFormfile.append ("filename", fileobj.name);//Additional parameter 2vardata =Formfile; $.ajax ({URL:"${basepath}market/contractdocumentaction!fileupload.action?", Data:data, type:"Post", DataType:"JSON", Cache:false,//uploading files without cachingProcessData:false,//used to serialize the data parameter this must be false if serialization is processed then the file in the form cannot be committed. ContentType:false,//must beSuccess:function(Result) {Lddialog.close ("Upload Contract file Success"); return; } }) } </script>
STRUTS2 Action Code
Public class Contractdocumentaction extends Actionsupport {//encapsulate upload file propertiesprivate file file; //Package upload file nameprivate String filename; Public String GetFileName () {returnfilename; } Publicvoidsetfilename (String filename) { This. FileName =filename; }public File GetFile () {returnfile; } Publicvoidsetfile (file file) { This. File =file; }publicvoidfileUpload () {String filenames= Sdf.format (NewDate ()) + "_" +contract.getserialnum () + "_" +filename;//file name String serverpath= System.getproperty ("Catalina.home"); InputStream is; Try{ is=Newfileinputstream (file); String Fpath= Serverpath + File.separator +"file";//Files save path tomcat under the file folder OutputStream os=NewFileOutputStream (NewFile (Fpath, filenames)); byte[] buffer =New byte[Is.available ()];//try not to use new bytenotation like [1024]. May cause the file to be damaged after the Word document is uploaded. intLength = 0; while( -1! = (length = is.read (buffer, 0, Buffer.length))) {os.write (buffer); } os.close (); Is.close (); } Catch(Exception e) {e.printstacktrace (); } }}
java+jsp File Upload (Ajax) way