spring mvc 项目中技术要点摘要
2013-02-25 09:04
183 查看
1.上传图片
jsp:
javascript:
mvc controller:
2.读取excel
3.
jsp:
<li><input type="file" name="image" id="image" /></li> <li><input type="button" value="保存" onclick="uploadImage();"/></li>
javascript:
function uploadImage(){ var image = document.getElementById("image").value; if(image == "") { alert("请选择上传的图片!"); return; } Ext.Ajax.request({ url: 'informationMaintenance/uploadImage?imagename='+encodeURI(image), method:'post', success: function (response) { var result = response.responseText; alert(result); }, failure: function (response) {} }) }
mvc controller:
//上传图片 @RequestMapping(value="/uploadImage",method=RequestMethod.POST) public @ResponseBody String uploadImage(HttpServletRequest request, HttpServletResponse response){ String imageName = request.getParameter("imagename").toString(); String result = ""; try { imageName = new String(imageName.getBytes("iso-8859-1"), "utf-8"); String uploadPath =request.getSession().getServletContext().getRealPath("/")+"images/"; UploadImage ui = new UploadImage(); result = ui.saveImage(imageName, uploadPath); } catch (UnsupportedEncodingException e) { result = e.getMessage(); } return result; }
public class UploadImage { public String saveImage(String imageName, String uploadPath){ String result = ""; try{ FileInputStream inStream = new FileInputStream(imageName); byte[] inOutb = new byte[inStream.available()]; inStream.read(inOutb); File outFile = new File(uploadPath + "right_index_tu_01.jpg"); FileOutputStream outStream = new FileOutputStream(outFile); outStream.write(inOutb); inStream.close(); outStream.close(); result = "上传成功"; } catch(Exception e) { result = e.getMessage(); } return result; } }
2.读取excel
sb.append("<tr><th>导入excel:</th><td><input type='file' class='inputbg_td' id='image'/></td>"); sb.append("<td><input type='button' value='上传' onclick='importExcel();'/>");
//excel导入// function importExcel(){ var excelname = document.getElementById("image").value; if(excelname == "") { alert("请选择要导入的excel!"); return; } if(confirm('确定要导入吗?')){ Ext.Ajax.request({ url: 'fileMaintenance/importExcel?excelname='+encodeURI(excelname), method:'post', success: function (response) { alert(response.responseText); }, failure: function (response) {} }) } }
//导入excel @RequestMapping(value="/importExcel",method=RequestMethod.POST) public @ResponseBody String importExcel(HttpServletRequest request, HttpServletResponse response){ String excelName = request.getParameter("excelname").toString(); String result = ""; try { excelName = new String(excelName.getBytes("iso-8859-1"), "utf-8"); result = importExcelService.ImportExcel(excelName); } catch (UnsupportedEncodingException e) { result = e.getMessage(); } return result; }
public class ImportExcelService { @Autowired private InsertUserService insertUserService; public String ImportExcel(String excelName){ String result = ""; OperatorExcel oe = new OperatorExcel(); Workbook rwb = oe.ReadExcel(excelName); if(rwb == null){ result = "读取excel失败"; return result; } try{ Sheet rs = (Sheet) rwb.getSheet(0); int rowNum = rs.getRows();//行 String user_id = rs.getCell(1,1).getContents(),//用户编号 quarterID = "",//小区编号 name = "",//用户名称 address = "",//用户地址 area = "",//用户面积 buildingCode = "",//幢 unitCode = "",//单元 doorplate = "",//楼层 cellphone = "",//手机 equipmentType = "",//设备类型 billing = "",//计费方式 hot_comp_type = "",//计费类别 equip_id = rs.getCell(13,1).getContents(),//采集点编号 meter_id = "",//仪表编号 pstart = "",//开通日期 collector_id = "";//集中器编号 BigDecimal b1,b2,b3; for(int i = 1;i < rowNum; i++){ quarterID = rs.getCell(2,i).getContents(); name = rs.getCell(3,i).getContents(); address = rs.getCell(4,i).getContents(); area = rs.getCell(5,i).getContents(); buildingCode = rs.getCell(6,i).getContents(); unitCode = rs.getCell(7,i).getContents(); doorplate = rs.getCell(8,i).getContents(); cellphone = rs.getCell(9,i).getContents(); equipmentType = rs.getCell(10,i).getContents(); billing = rs.getCell(11,i).getContents(); hot_comp_type = rs.getCell(12,i).getContents(); meter_id = rs.getCell(14,i).getContents(); pstart = rs.getCell(15,i).getContents(); collector_id = rs.getCell(16,i).getContents(); //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); //pstart = format.format(pstart); String[] strs = {"",user_id,quarterID,name,address,area,buildingCode, unitCode,doorplate,cellphone,equipmentType,billing,hot_comp_type, equip_id,meter_id,pstart,collector_id}; insertUserService.insertUser(strs); b1 = new BigDecimal(user_id); b2 = new BigDecimal(equip_id); b3 = new BigDecimal(1); user_id = b1.add(b3).toString();//用户编号+1 equip_id = b2.add(b3).toString();//采集点编号+1 } rwb.close(); result = "导入成功!"; }catch(Exception e){ result = e.getMessage(); } return result; } }
public class OperatorExcel { public Workbook ReadExcel(String excelName){ Workbook rwb = null; try{ rwb = Workbook.getWorkbook(new FileInputStream(excelName)); } catch (Exception e) { rwb = null; } return rwb; } }
3.
相关文章推荐
- 外包项目管理技术要点
- HBase学习、项目模块技术要点总结
- 项目管理-技术论证会的组织实施注意要点
- 浅谈vue项目重构技术要点和总结
- Java面试--Spring技术要点--Spring MVC
- extjs 项目中技术要点摘录
- 《环信支持千万并发即使通讯的技术要点》阅读摘要
- asp.net 网页技术要点摘要
- 总结之前项目的技术要点
- 关闭 晓K的专栏 我的学习历程 目录视图摘要视图订阅 赠书 | 异步2周年,技术图书免费选 每周荐书:渗透测试、K8s、架构(评论送书) 项目管理+代码托管+文档协作,开发更
- EAI概述,需求,功能,技术要点----4. EAI项目开发过程
- 关闭 晓K的专栏 我的学习历程 目录视图摘要视图订阅 赠书 | 异步2周年,技术图书免费选 每周荐书:渗透测试、K8s、架构(评论送书) 项目管理+代码托管+文档协作,开发更
- 项目技术要点
- ERP项目实施技术要点分析
- 最近项目的技术要点及浏览器内部工作原理
- 项目兼容ie8技术要点
- 项目开发J2EE框架开发原型技术要点
- 2015级WCF项目设计优秀技术要点总结
- Winform技术要点及案例项目开发小结