直接上代码:
jsp页面中:
<div style="margin: 20px;"> <span>当前最新固件 :</span><label style="margin-left: 10px; color: red;" id="currentFirmware"></label> </div> <div style="margin: 20px;"> <p><input type="file" name="firmware" id="firmware" /></p><!-- 注意name的写法 --> <p style="margin: 20px 0px 0px 15px;"> <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-upload'" onclick="doUpload();">上 传 固 件</a> (上传固件到服务器,并不更新网关)</p> <p style="margin: 20px 0px 0px 15px;"> <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-update'" onclick="updateGateway();">更 新 固 件</a> (把服务器上最新的固件远程更新到网关)</p> </div>
js:
var path = '<%=request.getContextPath()%>'; var sessionId = '<%=request.getSession().getId()%>';
$(function() { $('#firmware').uploadify( { 'buttonText' : '选 择 固 件', 'fileObjName' : 'firmware', // 需和input的name,以及struts2中的三个文件上传属性一致 'swf' : path + '/js/uploadify/uploadify.swf', 'uploader' : path + '/doUpload.action', // 必须全路径 uploadPorcessServlet 'multi' : false, 'auto' : false, 'fileTypeDesc' : '固件', 'fileTypeExts' : '*.bin', 'formData' : { 'sessionId' : sessionId },// sessionId用于解决session丢失的问题 'fileSizeLimit' : '10240KB', 'removeCompleted' : false, 'onUploadSuccess' : function(file, data, response) { //alert('The file ' + file.name + ' was successfully uploaded with a response of ' + response + ':' + data); if (data != null && data != '' && data != 'null') { $.messager.alert('提示信息', data, 'info'); return; } else { $.messager.alert('提示信息', '上传成功', 'info'); //得到最新固件信息 getNewest(); } } }); //页面初始化时得到服务器上的最新固件 getNewest(); });
java类:
因为新的jdk已不支持通过sessionId得到session,所以要自己写个hashMap,保存session
package com.mhm.dto; import javax.servlet.http.HttpSession; import java.util.HashMap; public class LedSessionContext { private static LedSessionContext instance; private HashMap<String, HttpSession> sessionMap; private LedSessionContext() { sessionMap = new HashMap<String, HttpSession>(); } public static LedSessionContext getInstance() { if (instance == null) { instance = new LedSessionContext(); } return instance; } public synchronized void AddSession(HttpSession session) { if (session != null) { sessionMap.put(session.getId(), session); } } public synchronized void DelSession(HttpSession session) { if (session != null) { sessionMap.remove(session.getId()); } } public synchronized HttpSession getSession(String sessionId) { if (sessionId == null) return null; return (HttpSession) sessionMap.get(sessionId); } }
监听session的创建以及销毁
package com.mhm.listener; import java.util.HashMap; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; import com.mhm.dto.LedSessionContext; public class SessionListener implements HttpSessionListener { public static HashMap<String, HttpSession> sessionMap = new HashMap<String, HttpSession>(); private LedSessionContext lsc = LedSessionContext.getInstance(); @Override public void sessionCreated(HttpSessionEvent httpSessionEvent) { lsc.AddSession(httpSessionEvent.getSession()); } @Override public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { HttpSession session = httpSessionEvent.getSession(); lsc.DelSession(session); } }
Action:
// =========== 供上传使用的字段,必须和jsp页面一致============= private File firmware; private String firmwareFileName; private String firmwareFileContentType; // =========== 供上传使用的字段=============
@Action(value = "doUpload", results = { @Result(name = Constants.SUCCESS, type = "json") }) public String doUpload() { log.info("method begin: doUpload()"); PrintWriter out = null; try { HttpServletResponse response = getResponse(); response.setContentType("text/html;charset=UTF-8"); out = getResponse().getWriter(); String sessionId = (String)getRequest().getParameter("sessionId"); HttpSession session = LedSessionContext.getInstance().getSession(sessionId); Userinfo user = (Userinfo)session.getAttribute(LedConstants.LOGINUSER); if (user == null) { rtnMsg = "用户未登录或登录已超时。上传失败"; out.print(rtnMsg); out.flush(); out.close(); } else { String path = getRequest().getSession().getServletContext().getRealPath("/upload"); System.out.println("path : " + path); // 对文件类型过滤赞不考虑 if (firmware != null) { StringBuilder newFileName = new StringBuilder(); int dotPos = firmwareFileName.indexOf("."); String fName = firmwareFileName.substring(0, dotPos); newFileName.append(fName) .append("-") .append(UUID.randomUUID()) .append(firmwareFileName.substring(dotPos)); File savefile = new File(new File(path), newFileName.toString()); if (!savefile.getParentFile().exists()) { savefile.getParentFile().mkdirs(); } FileUtils.copyFile(firmware, savefile); // 上传信息存入数据库 Uploadfile entity = new Uploadfile(); entity.setFname(newFileName.toString()); entity.setOriginalName(firmwareFileName); entity.setFilePath(path + "\\" + newFileName.toString()); entity.setType(Constants.INT_VALUE1); entity.setFlag(Constants.INT_VALUE1); entity.setCreateDate(getNowTimestamp()); entity.setCreateUser(user.getUserId()); uploadFileMng.save(entity); out.print(""); out.flush(); out.close(); } } } catch (IOException ex) { log.error("doUpload()", ex); rtnMsg = ex.getLocalizedMessage(); out.print(rtnMsg); out.flush(); out.close(); } log.info("method begin: doUpload()"); return SUCCESS; }
相关推荐
struts2 +jquey uploadify3 2 实现多文件上传 可以运行的myeclipse工程 绝对好用 访问方式http: 127 0 0 1:8080 Struts2Uploadify upload jsp uploadify 使用说明: <a href "javascript:$ "#file ...
uploadify3.1与Struts2配合使用。代码运行完美无错,upload因为版本问题,开发者的方法总是运行不起来,这个与Struts2结合的可以成功运行。
struts1 uploadify 多文件上传
Uploadify结合Struts2上传demo
毕竟是第一次用 不是很熟悉 希望有人发现问题可以交流一下">这是根据uploadify3 2结合struts2搭建的文件上传环境 可以直接导入eclipse运行 每步实现基本都加了注释 以下是我碰到的问题: 1 判断session是否失效 ...
解决uploadify上传火狐浏览器下丢失session Flash上传丢失session
代码使用Struts2框架和uploadify插架实现多文件上传功能。
使用struts2和uploadify(2.1.4)开发的文件上传插件
struts2 + uploadify上传文件示例
这是我自己做的struts2+uploadify的小案例,简单地实现了上传!不好地方请多谅解。
action 已经测试ok能正常上传到指定的位置,页面根据自己的需要复制到代码即可
struts2结合uploadify3.2实现上传进度条,可控文件大小
利用struts2框架实现uploadidfy多文件上传,显示进度条上传
今天在使用uploadify时发现session会发生丢失的情况,经过一番研究发现,其丢失并不是真正的丢失,而是在使用Flash上传控件的时候使用的session机制和asp.net中的不相同。为解决这个问题使用两种方案,下面进行介绍 ...
解决uploadify用法时session发生丢失问题的方法_.docx
程序进过本人亲测,附件中含SQL,用的是MySQL数据库,实现了带进度条的多文件上传,并提供下载功能,解压后可直接导入Eclipse,希望对你有所帮助。
uploadify jsp上传附件火狐浏览器不兼容丢失session问题,Uploadify v3.2
完整的Uploadify+Struts2使用案例,前台用jQuery的DOM技术生成隐藏的input,后台由action负责上传,上传结束后提交前台的form。再由另外一个action完成链接和其他信息的持久化工作。
uploadify是来自国外的一款优秀的jQuery上传插件,主要功能是批量上传文件,带进度显示。