要想在struts下实现无刷新上传文件,有很多种方式,但我这里用的是ajaxfileupload这个js库,感觉还比较好用,建议从ajaxfileupload官网下载它的包,里面有比较完整的例子,不过是php的,如果是jsp开发,可以参考我的代码,好了,废话不多说,直接上代码(什么struts配置我就不啰嗦了,直接附上上传相关的代码)
需要的东西:struts2-json-plugin-2.2.1.jar、jquery.js和ajaxfileupload.js(附件可下载)
上传文件的fileUpload.jsp
<%@ page language="java" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <base href="<%=basePath%>" /> <title>Excel导入</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- div(文件上传)css --> <link href="css/fileDiv.css" rel="stylesheet" type="text/css" /> <!-- 一定要注意js加载的前后顺序,先加载jquery再加载ajaxfileupload --> <script type="text/javascript" src="jscript/jquery.js"></script> <script type="text/javascript" src="jscript/ajaxfileupload.js"></script> <!-- div(文件上传)js --> <script type="text/javascript" src="jscript/fileDiv.js"></script> <script type="text/javascript"> function ajaxFileUpload() { //starting setting some animation when the ajax starts and completes $("#loading") .ajaxStart(function(){ $(this).show(); }) .ajaxComplete(function(){ $(this).hide(); }); /* prepareing ajax file upload url: the url of script file handling the uploaded files fileElementId: the file type of input element id and it will be the index of $_FILES Array() dataType: it support json, xml secureuri:use secure protocol success: call back function when the ajax complete error: callback function when the ajax failed */ $.ajaxFileUpload ( { url:'management/excel_Operation!importExcelWms.jspx', secureuri:false, fileElementId:'file', dataType: 'json', data: {//加入的文本参数 module_number: "${module_number}", right: "${right}" }, success: function (data) { alert(data.message); }, error: function (data, e) { alert("错误信息:"+e); } } ) } </script> </head> <body> <!-- 文件上传div --> <div class="fileDiv" id="fileDiv"> <div class="fileDiv-top">数据导入</div> <form action="" id="fileForm" method="post" enctype="multipart/form-data"> <div class="fileDiv-search"> 请选择文件:<input type="file" name="file" id="file" class="input-normal"/><br /> <div class="fileDiv-button"> <img alt="" src="img/loading.gif" id="loading" style="display: none; width: 50px; height: 50px"/> <a class="button-white" href="javascript:operate('management/excel_Operation!downloadExcel.jspx');"><span>模板下载</span></a> <a class="btn-lit" id="btn_upload" name="btn_upload" href="javascript:ajaxFileUpload();"><span>开始导入</span></a> <a class="button-white" href="${backUrl }"><span>返回</span></a> </div> </div> <input type="hidden" name="module_number" id="module_number" value="${module_number }"/> </form> <div class="fileDiv-content"> 为减少错误,请在导入数据前下载最新Excel模板!<br /> 说明<br /> 1、淡黄色代表该字段(必填)<br /> 2、淡橙色代表该字段有代码表(必填)<br /> 3、淡绿色代表该字段有代码表(选填) </div> </body> </html>
Excel_OperationAction部分处理方法
// 模块导入-wms public void importExcelWms() { JSONObject jsonObj = new JSONObject(); String module_number = getRequest().getParameter("module_number"); String right = getRequest().getParameter("right"); HttpSession session = getRequest().getSession(false); UserDto userDto = (UserDto) session.getAttribute("USER_INFO"); String table_dm = new ExcelReader().findName("table_dm",module_number);; String view_dm = module_number; String mk_dm = module_number; InputStream is = null; if (file == null) { jsonObj.put("message", "没有选择文件,请选择要导入的文件!"); write(jsonObj.toString()); return ; } try { is = new BufferedInputStream(new FileInputStream(file)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } String msg = new String(); if (!Utils.isValidString(table_dm) || !Utils.isValidString(view_dm) || !Utils.isValidString(mk_dm)) { jsonObj.put("message", "模块信息有误,请重新导入(如果多次不成功,请与管理员联系)!"); write(jsonObj.toString()); return ; } else { msg = new ExcelReader().excelImportWms(table_dm, view_dm, mk_dm, is,userDto); } try { if (is != null) { is.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } jsonObj.put("message", msg); write(jsonObj.toString()); return ; }
write方法:
/** * 功能描述:页面输出 * * @param content * 输出的内容 */ protected void write(String content) { if (content != null) { HttpServletResponse response = getResponse(); response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); try { response.getWriter().println(content); response.getWriter().flush(); response.getWriter().close(); } catch (IOException e) { } } }
有一个需要注意的地方:到底后台的值怎么传到前台去呢,最好的方法就是通过json传值。
需要的js及jar文件附件里有
相关推荐
文件上传几乎是每个项目所必须的,这里介绍Asp.net MVC结合Jquery ajaxfileupload实现文件上传,兼容主流浏览器
ajax无刷新上传 ajax上传,ajaxFileUpload 无刷新无Form上传
jquery ajaxfileupload上传插件,用于ajax的异步文件上传
这是一个无刷新上传插件,网上有很多相关的博客资料,不知道怎么设置的自己搜索相关资料,不需要分数。
使用struts 2 和ajaxFileUpload组件实现图片上传预览,直接在myeclipse和tomcat环境下就可以运行,同时附带了相应的实现说明。
jquery ajaxfileupload.js异步上传插件
jquery+ajaxfileupload+html文件上传,用ajaxfileupload插件做的上传文件
ajaxFileUpload+struts2实现多文件上传(动态添加文件上传框)(项目源码) 博文地址:http://blog.csdn.net/itmyhome1990/article/details/36433621
使用jquery 预览图片并实现无刷新上传 使用了jquery.ajaxfileupload.js插件实现无刷新上传
博文 http://blog.csdn.net/qq_34273222/article/details/54428580 中所需要的js文件,如有问题请在博客留言。
亲测可以,给个好评,如果使用的tomcat服务器,文件默认上传到部署文件夹下的upload文件夹下,上传后,请到此目录查看
jQuery插件AjaxFileUpload用来实现ajax文件上传,该插件使用非常简单,接下来写个demo演示怎么用AjaxFileUpload插件实现文件上传。 1、引入AjaxFileUpload插件相关的js 复制代码 代码如下:[removed]resources/js/...
jQuery插件AjaxFileUpload用来实现ajax文件上传,该插件使用非常简单,接下来写个demo演示怎么用AjaxFileUpload插件实现文件上传。
ajaxfileupload1.0.zipajaxfileupload1.0.zipajaxfileupload1.0.zip
AjaxFileUpload Struts2 多文件上传
NULL 博文链接:https://cui-yh.iteye.com/blog/2310539
AjaxFileUpload实现文件异步上传(功能实现代码).rar AjaxFileUpload实现文件异步上传, 文件域添加change事件,改变文件域时执行上传文件功能 $("#upload-excel-file").bind("change", function() { $....
AjaxFileUpload实现文件上传 一个简单的例子
ajaxFileUpload+struts2实现多文件上传(源码) 博文地址:http://blog.csdn.net/itmyhome1990/article/details/36396291