jar包链接:http://pan.baidu.com/s/1boSLFNT 密码:imgo
链接:http://pan.baidu.com/s/1slI0e5J 密码:ul4x
目标:选择文件、上传,存session,读取
选择
1.环境:sturts+action
上传:
<form action="BannerAction.action" method="post" enctype="multipart/form-data"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td>选择文件:</td><td><input type="file" name="myFile" class="myFile" /></td> </tr> <tr> <td ><input type="submit" value="上传" /> <input type="reset" value="重置" /></td> </tr> </table> </form>
2.action 存取目录,并存session 值
package com.action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; import com.util.OSSUtils; /** * 首页轮回图片上传 * @author caipei * */ public class BannerAction extends ActionSupport{ // myFile属性用来封装上传的文件 private File myFile; // myFileContentType属性用来封装上传文件的类型 private String myFileContentType; // myFileFileName属性用来封装上传文件的文件名 private String myFileFileName; //获得myFile值 public File getMyFile() { return myFile; } //设置myFile值 public void setMyFile(File myFile) { this.myFile = myFile; } //获得myFileContentType值 public String getMyFileContentType() { return myFileContentType; } //设置myFileContentType值 public void setMyFileContentType(String myFileContentType) { this.myFileContentType = myFileContentType; } //获得myFileFileName值 public String getMyFileFileName() { return myFileFileName; } //设置myFileFileName值 public void setMyFileFileName(String myFileFileName) { this.myFileFileName = myFileFileName; } public String execute() throws Exception { HttpServletResponse response=null; response=ServletActionContext.getResponse(); response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); PrintWriter out=response.getWriter(); //创建session对象 HttpSession session=ServletActionContext.getRequest().getSession(); if (session.getAttribute("id")==null) { out.print("<script lanaguage='javascript'>alert('请重新登录');window.location='login.jsp';></script>"); out.flush(); out.close(); return null; } //基于myFile创建一个文件输入流 InputStream is = new FileInputStream(myFile); // 设置上传文件目录 String uploadPath = ServletActionContext.getServletContext() .getRealPath("/upload"); // 设置目标文件 File toFile = new File(uploadPath, this.getMyFileFileName()); // 创建一个输出流 OutputStream os = new FileOutputStream(toFile); //设置缓存 byte[] buffer = new byte[1024]; int length = 0; //读取myFile文件输出到toFile文件中 while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); } System.out.println("上传文件名"+myFileFileName); System.out.println("上传文件类型"+myFileContentType); //关闭输入流 is.close(); //关闭输出流 os.close(); //设置session 存储文件目录 session.setAttribute("filePath", uploadPath+"/"+myFileFileName); /*try { // 获取原始文件名 String originalName = this.getMyFileFileName(); // 获取文件的后缀 //String suffix = originalName.substring(originalName.lastIndexOf(".") + 1); String path=OSSUtils.uploadImage(is, "yxServer/" + StringUtils.replace(originalName, "." + suffix, "") + UUID.randomUUID().toString().replaceAll("-", "").toString() + "." + suffix, ""); String url = OSSUtils.uploadImage(FileUtils.openInputStream(new File(uploadPath+"/"+originalName)),"yxServer/"+originalName, ""); System.out.println(url); } catch (Exception e) { e.printStackTrace(); }*/ return SUCCESS; } }
3.struts配置
<action name="BannerAction" class="com.action.BannerAction"> <result name="success" type="chain">MachineDeliverAction</result> </action>
4.选择权限,分配机器
js:代码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>后台系统</title> <base href="<%=basePath%>"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link href="Style/Style.css" rel="stylesheet" type="text/css" /> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <script type="text/javascript" src="http://121.41.47.176/tbmj/js/param.js"></script> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.6.4/jquery.js"></script> </head> <script type="text/javascript"> function selectUser() { //获取选项 var type = document.getElementById('usertype'); /* //省 var provience = document.getElementById('provience').value; //市 var city = document.getElementById('city').value; */ //var ins=type.selectedIndex ; //var index=myselect.options[ins]; //0:经销商:1:代理商 var index = type.selectedIndex; /* alert("省:" + provience); alert("市:" + city); */ /* if(index == 1){ var accountSel = document.getElementById("accountNum"); var opt = document.createElement("OPTION"); //创建option对象 opt.text = "哈哈"; //opt.text = 哈哈+"--"+1234; //alert("opt.text:"+opt.text); //指定新创建元素的text属性值 opt.value = "67"; //opt.value = 1; //指定新创建元素的value属性值 accountSel.add(opt); //为select元素添加option } else { */ $ .ajax({ /* url : "http://114.55.74.89/dapail/worker/accountListByArea.action?usertype=" + index + "&provience="+provience+"&city="+city+"", */ url : "agentList.action?usertype=" + index + "", type : "POST", dataType : "json", data : {}, success : function(data) { if (true == true) { var dataArray = eval(data.data.agentItems); var accountSel = document .getElementById("accountNum"); //alert("_citySel:"+_citySel); //alert("长度:" + dataArray.length); for ( var i = 0; i < dataArray.length; i++) { //遍历XML数据并给select元素添加选项 /* alert(dataArray[i].ACCOUNT_PROVINCE); alert(dataArray[i].ACCOUNT); */ var opt = document.createElement("OPTION"); //创建option对象 opt.text = dataArray[i].ACCOUNT_PROVINCE+ ":" + dataArray[i].ACCOUNT_CITY+":"+dataArray[i].NAME + ":" + dataArray[i].ACCOUNT; //opt.text = 哈哈+"--"+1234; //alert("opt.text:"+opt.text); //指定新创建元素的text属性值 opt.value = dataArray[i].ACCOUNT_ID; //opt.value = 1; //指定新创建元素的value属性值 accountSel.add(opt); //为select元素添加option } } else { alert(data.msg); } }, }); document.getElementById("setfocus").value = ""; } function newChg() { document.getElementById("setfocus").value = "请输入关键字"; } </script> <body style="background-image:url(【 )"> <center> <table width="900" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="250" background="Images/mid-background.jpg" style="color: #06F; font-size: 19px; font-weight: bolder; padding-left: 50px;"></td> </tr> <tr> <td height="30" background="Images/MenuBg.jpg"> </td> </tr> <tr> <td height="500" align="center" valign="top"><table width="900" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="191" height="500" align="center" valign="top" background="Images/leftbg.jpg"><%@ include file="left.jsp"%> </td> <td width="709" align="center" valign="top" bgcolor="#F6F9FE"><table width="709" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="30" background="Images/mainMenuBg.jpg" style="padding-left: 25px;">分配机器</td> </tr> <tr> <td height="470" align="center" valign="top" bgcolor="#F6F9FE"> <form action="BannerAction.action" method="post" enctype="multipart/form-data"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td>选择文件:</td><td><input type="file" name="myFile" class="myFile" /></td> </tr> <tr> <td ><input type="submit" value="上传" /> <input type="reset" value="重置" /></td> </tr> </table> </form> <form name="form1" method="post" action="MachineDeliverAction.action?"> <table width="100%%" border="0" cellspacing="0" cellpadding="0"> <tr> <td > 查询机器编号:</td> <td>机器id: <input name="searchName" type="text" class="text1" id=searchName> <input type="submit" name="button" id="button" value="查询"></td> </tr> </table> </form> <form name="form" method="post" action="MaTieAccountAction.action"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr align="center" class="t1"> <td height="25" bgcolor="#D5E4F4"><strong>机器id</strong></td> <td bgcolor="#D5E4F4"><strong>分配权限</strong></td> <td bgcolor="#D5E4F4"><strong>分配状态</strong></td> <td bgcolor="#D5E4F4"><strong>省</strong></td> <td bgcolor="#D5E4F4"><strong>市</strong></td> <td bgcolor="#D5E4F4"><strong>区</strong></td> <td bgcolor="#D5E4F4"><strong>姓名</strong></td> <td bgcolor="#D5E4F4"><strong>手机号</strong></td> <td bgcolor="#D5E4F4"><strong>绑定时间</strong></td> <td bgcolor="#D5E4F4"><strong>操作</strong></td> </tr> <s:iterator id="aa" value="machineDeliver"> <tr align="center"> <td height="25" align="center">${MACHINE_ID}</td> <td>${isAgent}</td> <td>${deliver_state}</td> <td>${provience}</td> <td>${city}</td> <td>${area}</td> <td>${name}</td> <td>${account}</td> <td>${dig_time}</td> <td align="center"> <c:if test="${deliver_state=='已分配'}"> <a href="UnBundQpsAction.action?MACHINE_ID=${MACHINE_ID}&isAgent=${isAgent}">解绑</a> </c:if> </td> </tr> </s:iterator> </table> <table> <tr> <td height="30" align="right" class="STYLE2">身份:</td> <td align="left"><select name="usertype" id="usertype" onblur="selectUser()"> <option value="2">代理商</option> <option value="3">经销商</option> </select></td> <!-- <td>省:</td><td><input name="provience" type="text" class="text1" id="provience"></td> <td>市:</td><td><input name="city" type="text" class="text1" id="city" ></td> --> <td>账户列表:</td> <td><select name="accountNum" id="accountNum"> <option value="0">---请选择--</option> </select></td> <td><input type="submit" name="button" id="button" value="分配机器"></td> </tr> </table> </form> </td> </tr> <%-- <tr align="center" class="t1"> <td> <% if (!session.getAttribute("setNum").toString().equals("0")) { %> <a href="MachineAction.action?page=1">上一页</a> <%}%> <a href="MachineAction.action?page=2">下一页</a> </td> </tr> --%> </table></td> </tr> </table></td> </tr> <tr> <td height="35" background="Images/bootBg.jpg"> </td> </tr> </table> </center> </body> </html>
3.绑定action
package com.action; import java.io.PrintWriter; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import net.sf.json.JSONObject; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; import com.util.ExcelUtil; import com.util.UrlUtil; //绑定机器 public class MaTieAccountAction extends ActionSupport{ private String ACCOUNT_ID; //账户id public String getACCOUNT_ID() { return ACCOUNT_ID; } public void setACCOUNT_ID(String aCCOUNT_ID) { ACCOUNT_ID = aCCOUNT_ID; } private String accountNum; public String getAccountNum() { return accountNum; } public void setAccountNum(String accountNum) { this.accountNum = accountNum; } @Override public String execute() throws Exception { // TODO Auto-generated method stub HttpServletResponse response=null; response=ServletActionContext.getResponse(); response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); PrintWriter out=response.getWriter(); //创建session对象 HttpSession session=ServletActionContext.getRequest().getSession(); if (session.getAttribute("id")==null) { out.print("<script lanaguage='javascript'>alert('请重新登录');window.location='login.jsp';></script>"); out.flush(); out.close(); return null; } //excel路径 String excelPath=session.getAttribute("filePath").toString(); System.out.println(excelPath); String maId=""; //读取excel文件 String[][] result = ExcelUtil.getData(excelPath, 1); int rowLength = result.length; for(int i=0;i<rowLength;i++) { for(int j=0;j<result[i].length;j++) { if (j==0) { maId=result[i][j]; }else { maId=maId+";"+result[i][j]; } } } String url="dapail/worker/deliverMa.action?accountId="+accountNum+"&maId="+maId+""; JSONObject salerJson=JSONObject.fromObject(UrlUtil.getUrl(url)); String msg=salerJson.get("msg").toString(); if (msg.contains("成功")) { return SUCCESS; } return msg; } }