浅谈struts1 & jquery form 文件异步上传
1.概述
还在用struts1?是的,在地球的没写地方,落后的生产方式还在运转(老项目).
从继承org.apache.struts.action.Action,继承org.apache.struts.action.ActionForm开始吧
2.代码
2.1html页面
网页上传 本地文件网页上传
文件信息列表
选择文件: 标题:
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.3MyUploadAction.java(继承自Action)
packagecom.rocky.console.action; importjava.io.File; importjava.io.FileOutputStream; importjava.io.InputStream; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importorg.apache.struts.action.Action; importorg.apache.struts.action.ActionForm; importorg.apache.struts.action.ActionForward; importorg.apache.struts.action.ActionMapping; importorg.apache.struts.upload.FormFile; importcom.rocky.console.form.MyUploadForm; importcom.rocky.console.service.ResponseUtil; publicclassMyUploadActionextendsAction{ publicActionForwardexecute(ActionMappingmapping,ActionFormform,HttpServletRequestrequest, HttpServletResponseresponse)throwsException{ MyUploadFormmyUploadForm=(MyUploadForm)form; FormFileuploadFile=myUploadForm.getUploadFile(); Stringfiletitle=myUploadForm.getFiletitle(); System.out.println("111111"+filetitle); intfileSize=uploadFile.getFileSize(); InputStreaminputStream=uploadFile.getInputStream(); System.out.println("fileSize::::::::"+fileSize); Stringpath="x:"; Stringfilename=path+File.separator+uploadFile.getFileName(); FileOutputStreamfos=newFileOutputStream(filename); byte[]b=newbyte[1024]; intlen=-1; while((len=inputStream.read(b))!=-1){ fos.write(b,0,len); } fos.close(); Stringresult="success"; ResponseUtil.write(response,result,null); returnnull; } }
2.4MyUploadForm.java(继承自ActionForm)
packagecom.rocky.console.form; importjavax.servlet.http.HttpServletRequest; importorg.apache.struts.action.ActionErrors; importorg.apache.struts.action.ActionForm; importorg.apache.struts.action.ActionMapping; importorg.apache.struts.upload.FormFile; publicclassMyUploadFormextendsActionForm{ /** * */ privatestaticfinallongserialVersionUID=6650496540449458586L; privateFormFileuploadFile=null; privateStringfiletitle; publicStringgetFiletitle(){ returnfiletitle; } publicvoidsetFiletitle(Stringfiletitle){ this.filetitle=filetitle; } publicActionErrorsvalidate(ActionMappingmapping, HttpServletRequestrequest){ returnnull; } publicvoidreset(ActionMappingmapping,HttpServletRequestrequest){ } publicFormFilegetUploadFile(){ returnuploadFile; } publicvoidsetUploadFile(FormFileuploadFile){ this.uploadFile=uploadFile; } }
2.5struts-config.xml
2.6说明
2.6.1jquery.form.js网上可以下载
使用varformData=newFormData(),然后formData.append("myfile",document.getElementById("myfile").files[0]);form.append...
当form表单field较多时写很多append很麻烦,显然ajaxSubmit方便很多
2.6.2前端过来的数据通过ActionForm直接封装到其子类(MyActionForm)对象中,用FormFile接收file文件用String等接收其他类型数据
当然都是根据HTML标签的name属性一一对应来注入的
2.6.3ActionForm是怎么和自定义实现的bean(MyUploadForm)对上的?
在struts-config.xml中form-bean设置自己的那个bean,通过 来完成这种映射 以上这篇浅谈struts1&jqueryform文件异步上传就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。